<template>
  <div class="app-container pageSediment">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="项目名称" prop="inExamItemName">
        <el-input
          v-model="queryParams.inExamItemName"
          clearable
          placeholder="请输入项目名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          clearable
          placeholder="请输入项目编码"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="检查类型" prop="examTypeId">
        <el-select v-model="queryParams.examTypeId" clearable placeholder="请选择检查类型" size="small">
          <el-option
              v-for="typeItem in examTypeOption"
              :key="typeItem.typeCode"
              :label="typeItem.typeName"
              :value="typeItem.typeCode"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="cyan" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          v-hasPermi="['basis:inExamItem:add']"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增</el-button>
        <el-button
          v-hasPermi="['basis:inExamItem:edit']"
          :disabled="single"
          icon="el-icon-edit"
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改</el-button>
        <el-button
          v-hasPermi="['basis:inExamItem:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="multipleTable" v-loading="loading"
              :data="inExamItemList" border
              stripe tooltip-effect="light"  @row-click="handleRowClick"
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column v-if="false" align="center" label="主键"  prop="id"/>
      <el-table-column align="left" fixed="left" label="项目编码" prop="itemCode" show-overflow-tooltip width="160" />
      <el-table-column align="left" fixed="left" label="项目名称"  min-width="190" prop="inExamItemName" show-overflow-tooltip/>

<!--      <el-table-column label="拼音码" align="left" prop="pyCode"  show-overflow-tooltip width="160"/>-->
      <el-table-column :formatter="examTypeNameFormat" align="center" label="检查类型名称" prop="examTypeId" show-overflow-tooltip width="120"/>
      <el-table-column :formatter="isEmptyStomachFormat" align="center" label="空腹属性" prop="isEmptyStomach" show-overflow-tooltip width="120" />
<!--      <el-table-column label="排尿属性" align="center" prop="isEmptyBladder" :formatter="isEmptyBladderFormat" show-overflow-tooltip width="90" />-->
      <el-table-column align="center" label="部位数" prop="positionCount"  show-overflow-tooltip width="90"/>
      <el-table-column align="left" label="检查时长" prop="examTime" show-overflow-tooltip width="150" />
      <el-table-column align="left" label="项目注意事项" min-width="400" prop="individualRemarks" show-overflow-tooltip />

<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basis:inExamItem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basis:inExamItem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改内部检查项目对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" append-to-body width="550px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="检查类型" prop="examTypeId">
          <el-select v-model="form.examTypeId" placeholder="请选择检查类型编码">
            <el-option
              v-for="typeItem in examTypeOption"
              :key="typeItem.typeCode"
              :label="typeItem.typeName"
              :value="typeItem.typeCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入项目编码" />
        </el-form-item>
        <el-form-item label="项目名称" prop="inExamItemName">
          <el-input v-model="form.inExamItemName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="部位数" prop="positionCount">
          <el-input v-model="form.positionCount" placeholder="请输入部位数" />
        </el-form-item>
        <el-form-item label="空腹属性" prop="isEmptyStomach">
          <el-select v-model="form.isEmptyStomach" placeholder="请选择空腹属性">
            <el-option
              v-for="dict in isEmptyStomachOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="尿液属性" prop="isEmptyBladder">
          <el-select v-model="form.isEmptyBladder" placeholder="请选择尿液属性">
            <el-option
              v-for="dict in isEmptyBladderOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查时长" prop="examTime">
          <el-input v-model="form.examTime" placeholder="请输入检查时长" />
        </el-form-item>
        <el-form-item label="注意事项" prop="individualRemarks">
          <el-input v-model="form.individualRemarks" placeholder="请输入项目注意事项"  type="textarea"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInExamItem, getInExamItem, delInExamItem, addInExamItem, updateInExamItem, exportInExamItem} from "@/api/basis/inExamItem";
import vPinyin from '@/utils/vue-py';
import { listExamtype } from "@/api/basis/examtype";

export default {
  name: "InExamItem",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内部检查项目表格数据
      inExamItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 项目是否空腹，0为不空腹，1为空腹字典
      isEmptyStomachOptions: [],
      // 是否排尿，0为不排尿，1为排尿字典
      isEmptyBladderOptions: [],
      //检查类型下拉
      examTypeOption:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examTypeId: null,
        itemCode: null,
        inExamItemName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examTypeId: [
          { required: true, message: "检查类型编码不能为空", trigger: "change" }
        ],
        itemCode: [
          { required: true, message: "项目编码不能为空", trigger: "blur" }
        ],
        inExamItemName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        positionCount: [
          { required: true, message: "部位数不能为空", trigger: "blur" }
        ],
        isEmptyStomach: [
          { required: true, message: "空腹属性不能为空", trigger: "change" }
        ],
        isEmptyBladder: [
          { required: true, message: "尿液属性不能为空", trigger: "change" }
        ],
      }
    };
  },
  mounted() {
    this.getList();
    this.getDicts("empty_stomach").then(response => {
      this.isEmptyStomachOptions = response.data;
    });
    this.getDicts("empty_bladder").then(response => {
      this.isEmptyBladderOptions = response.data;
    });

     listExamtype({isValid: 1}).then(response => {
      this.examTypeOption = response.rows;
    });
  },
  methods: {
    handleRowClick(	row, column, event){
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    /** 查询内部检查项目列表 */
    getList() {
      this.loading = true;
      listInExamItem(this.queryParams).then(response => {
        this.inExamItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 项目是否空腹，0为不空腹，1为空腹字典翻译
    isEmptyStomachFormat(row, column) {
      return this.selectDictLabel(this.isEmptyStomachOptions, row.isEmptyStomach);
    },
    // 是否排尿，0为不排尿，1为排尿字典翻译
    isEmptyBladderFormat(row, column) {
      return this.selectDictLabel(this.isEmptyBladderOptions, row.isEmptyBladder);
    },
    examTypeNameFormat(row, column){
        let name = '';
      this.examTypeOption.forEach(i =>{
        if(row.examTypeId == i.typeCode){
          name = i.typeName;
        }
      })
      return name;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        examTypeId: null,
        pyCode: null,
        itemCode: null,
        inExamItemName: null,
        examTime: null,
        individualRemarks: null,
        positionCount: null,
        isEmptyStomach: null,
        isEmptyBladder: null,
        createdAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加内部检查项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInExamItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改内部检查项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.inExamItemName !=null && this.form.inExamItemName !=''){
               this.form.pyCode = vPinyin.chineseToPinYin(this.form.inExamItemName);
               console.log(this.form.pyCode)
          }
          if (this.form.id != null) {
            updateInExamItem(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInExamItem(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除内部检查项目编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInExamItem(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有内部检查项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInExamItem(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
