<!--检查项管理-->
<template>
  <div class="app-container checkOption">
    <div class="checkOptionPage" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item style="float: right;">
          <el-col :span="1.5">
            <el-button
              style="width:140px;"
              type="primary"
              plain
              @click="addDialog(1)"
              v-hasPermi="['laboratory:checkOption:add']"
            ><i class="el-icon-plus" style="margin-right:10px;"></i>添加检查项</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              style="width:100px;margin:0 10px;"
              type="primary"
              plain
              @click="importClick"
              v-hasPermi="['laboratory:checkOption:import']"
            >批量导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              style="width:100px;"
              type="primary"
              plain
              @click="handleExport"
              v-hasPermi="['laboratory:checkOption:export']"
            >下载模板</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              style="width:140px;margin:0 0 0 10px;"
              type="primary"
              plain
              @click="goPageType(2)"
              v-hasPermi="['laboratory:checkOption:list']"
            >检查项提交记录</el-button>
          </el-col>
        </el-form-item>

      </el-form>
      <el-table border
        v-loading="loading"
        :data="checkOptionList"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="name" label="检查项名称">
          <template slot-scope="scope">
            <el-tooltip :disabled="scope.row.name.length < 74" class="item" effect="dark" :content="scope.row.name" placement="top-start">
              <div style="display:inline-block;margin:0;">
                <p style="max-width:1100px;overflow: hidden;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;margin:0;">
                  <span>{{scope.row.code}}</span><span>{{scope.row.name}}</span>
                </p>
              </div>
            </el-tooltip>
          </template>
          <!--<template slot-scope="scope" :show-overflow-tooltip="true">{{scope.row.code}}{{scope.row.name}}-->
            <!--<div style="display: inline-block;">-->
              <!--<div style="display: flex;">-->
                <!--<p style="margin:0 10px 0 0;height:17px;">{{scope.row.code}}</p>-->
                <!--<p style="margin:0;height:17px;">{{scope.row.name}}</p>-->
              <!--</div>-->
            <!--</div>-->
            <!--<span style="margin-right:10px;">{{scope.row.code}}</span><span>{{scope.row.name}}</span>-->
          <!--</template>-->
        </el-table-column>
        <el-table-column label="操作" width="240" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="button-box" style="display: flex">
              <p
                v-if="scope.row.tabelLevel < 4"
                class="table-min-button"
                style="margin-right: 10px"
                v-hasPermi="['laboratory:checkOption:add']"
                @click="addDialog(2,scope.row)">新增
              </p>
              <p class="table-min-button"
                style="margin-right: 10px"
                 v-hasPermiAnd="['laboratory:checkOption:add','laboratory:checkOption:edit']"
                @click="editDialog(scope.row)">编辑</p>
              <p class="table-min-button"
                 v-hasPermi="['laboratory:checkOption:remove']"
                @click="handleDelete(scope.row)">删除</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <check-config-list-page v-if="pageType == 2"></check-config-list-page>
    <!--新增/编辑-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="检查项名称" prop="name">
          <el-input type="textarea" maxlength="30" v-model="form.name" placeholder="请输入检查项名称" rows="3" resize="none"></el-input>
        </el-form-item>
        <el-form-item label="条款号" prop="code">
          <el-input v-model="form.code" maxlength="7" placeholder="请输入条款号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--导入-->
    <el-dialog title="批量导入" :visible.sync="importOpen" width="700px" append-to-body>
      <div style="width:660px;">
        <p style="margin:0 0 20px;font-size:20px;text-align: center;">请按格式上传检查项，确保上传的文件内容格式与模版一致</p>
        <p class="reset-button-one" style="display:block;width:400px;margin:0 auto 20px;height:50px;line-height:50px;color:#666;border:1px solid #666;" @click="handleExport">下载模板</p>
        <el-upload
          style="width:400px;margin:0 auto 20px;"
          class="avatar-uploader"
          :action="uploadImgUrl"
          :show-file-list="false"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
          :on-success="handleAvatarSuccess"
          :headers="headers"
          :before-upload="beforeAvatarUpload">
          <p class="inquire-button-one" style="display:block;width:400px;margin:0 auto 20px;height:50px;line-height:50px;">选择文件</p>
        </el-upload>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCheckOption, getCheckOption, delCheckOption, addCheckOption, updateCheckOption } from "@/api/laboratory/checkOption";
  import checkConfigListPage from "./checkConfigListPage.vue"
  import { getToken } from "@/utils/auth";

export default {
  name: "CheckOption",
  components: {
    checkConfigListPage,
  },
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:checkOption:add','laboratory:checkOption:add','laboratory:checkOption:edit','laboratory:checkOption:remove']),
      uploadImgUrl: window.location.href.split('://')[0]+'://' + process.env.VUE_APP_BASE_API + "/laboratory/checkOption/import", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      //页面状态
      pageType:1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查项选项表格数据
      checkOptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        deptName: null,
        userId: null,
        name: null,
        parentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入检查项名称", trigger: "blur" },
          { required: true, message: "请输入检查项名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        code: [
          { required: true, message: "请输入条款号", trigger: "blur" },
          { required: true, message: "请输入条款号", validator: this.spaceJudgment, trigger: "blur" }
        ],
      },
      options:[],
      //导入
      importOpen:false,
      //新增/编辑状态
      dialogType:"",
    };
  },
  created() {
  },
  mounted(){
    this.getList();
  },
  methods: {
    //导入窗口
    importClick(){
      this.importOpen = true;
    },
    // 添加检查项
    addDialog(type,row){
      this.title = "添加检查项";
      if(type == 1){
        let obj = {
          parentId:0,
          name:"",
          level:1,
        }
        this.$set(this,'form',obj)
      }else if(type == 2){
        let obj = {
          parentId:row.id,
          name:"",
          level:row.tabelLevel+1,
        }
        this.$set(this,'form',obj)
      }
      this.dialogType = 1;
      this.open = true;
      this.$refs['form'].clearValidate();
      // this.$refs['form'].clearValidate('name');
      // this.$refs['form'].clearValidate('code');

    },
    // 编辑检查项
    editDialog(row){
      this.title = "编辑检查项";
      let obj = JSON.parse(JSON.stringify(row))
      this.$set(this,'form',obj)
      this.dialogType = 2;
      this.open = true;
    },
    goPageType(type){
      if(type == 2){
        this.pageType = 2;
      }else if(type == 1){
        this.pageType = 1;
      }
    },
    /** 查询检查项选项列表 */
    getList() {
      this.loading = true;
      listCheckOption(this.queryParams).then( response => {
        for(let a=0;a<response.data.length;a++){
          response.data[a].tabelLevel = 1
          for(let b=0;b<response.data[a].children.length;b++){
            response.data[a].children[b].tabelLevel = 2
            for(let c=0;c<response.data[a].children[b].children.length;c++){
              response.data[a].children[b].children[c].tabelLevel = 3
              for(let d=0;d<response.data[a].children[b].children[c].children.length;d++){
                response.data[a].children[b].children[c].children[d].tabelLevel = 4
              }
            }
          }
        }
        this.checkOptionList =  response.data;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        deptName: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        name: null,
        parentId: 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
      getCheckOption(id).then( response => {
        this.form =  response.data;
        this.open = true;
        this.title = "修改检查项选项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogType == 1) {
            addCheckOption(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else if(this.dialogType == 2) {
            updateCheckOption(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除检查项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCheckOption(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/checkOption/export', {
        ...this.queryParams
      }, `检查项模板.xlsx`)
    },
    handleAvatarSuccess(res, file) {
      if(res.code == 200){
        this.msgSuccess("导入成功")
        this.importOpen = false;
        this.getList();
      }else{
        this.msgError(res.msg);
      }
    },
    beforeAvatarUpload(file) {
      let type = false;
      console.log('file',file);
      if (file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || file.type == 'application/vnd.ms-excel') {
        type = true;
      }else{
        this.$message.error('只能上传xls/xlsx格式文件');
        type = false;
      }
      return type;
    },
  }
};
</script>

<style scoped lang="scss">
  .checkOption{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden !important;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    .checkOptionPage{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden !important;
    }
  }
</style>
