<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          v-auth="'certificate.create'"
          @click="handleForm('add')"
        ></el-button>
        <el-select
        v-model="query.type"
        placeholder="证书类型"
        @change="handleQuery"
        clearable
        style="margin-left: 2px"
      >
        <el-option
          v-for="item in typeoptions"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
      <el-select
        v-model="query.employee__type"
        placeholder="人员类型"
        @change="handleQuery"
        clearable
        style="margin-left: 2px"
      >
        <el-option
          v-for="item in eptypeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="query.search"
            placeholder="证书名称/编号"
            clearable
            @keyup.enter="handleQuery"
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleQuery"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
       
        stripe
       
      >
       
        <el-table-column label="#" fixed="left" type="index" width="50"></el-table-column>
        <el-table-column label="人员" prop="employee_name"></el-table-column>
        <el-table-column label="证书名称" fixed="left" prop="name">
          <template #default="scope">
            <el-link
              v-if="scope.row.file"
              :href="scope.row.file"
              type="primary"
              target="_blank"
              >{{ scope.row.name }}</el-link
            >
            <span v-else>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证书编号" prop="number"> </el-table-column>
        <el-table-column label="证书类型" prop="type"><template #default="scope">{{type_[scope.row.type]}} </template></el-table-column>
        <el-table-column label="发证日期" prop="issue_date"></el-table-column>
         <el-table-column label="有效期" prop="expiration_date"> </el-table-column>
        <el-table-column label="下一次复审日期" prop="review_date"></el-table-column> 
        <el-table-column label="操作" fixed="right" align="left">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="warning"
                size="small"
                 v-auth="'certificate.update'"
                @click="handleForm('edit', scope.row)"
                >编辑</el-button
              >
            
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <el-dialog v-model="dialogcart" draggable title="人员证书">
    <el-form
      ref="dialogForm"
      :model="form"
      :rules="rules"
      :disabled="mode === 'show'"
      label-width="140px"
    >
          <el-form-item label="所属人员" prop="employee">
            <el-input
              v-model="form.employee_name"
              disabled
              style="width:200px"
            ></el-input>
            <ehsEpSelect :multiple="false" @submit="getEmployee" v-if="mode=='add'"/>
          </el-form-item>
          <el-form-item label="证书名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入证书名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="证书编号" prop="number">
            <el-input
              v-model="form.number"
              placeholder="请输入证书编号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="证书类型" prop="type">
            <el-select v-model="form.type" placeholder="选择证书类型">
              <el-option
                v-for="item in typeoptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发证日期" prop="issue_date">
            <el-date-picker
              v-model="form.issue_date"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择发证日期"
            />
          </el-form-item>
          <el-form-item label="有效期" prop="expiration_date">
            <el-date-picker
              v-model="form.expiration_date"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择有效期"
            />
          </el-form-item>
          <el-form-item label="下一次复审日期" prop="review_date">
            <el-date-picker
              v-model="form.review_date"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择下一次复审日期"
            />
          </el-form-item>
          <el-form-item label="上传证书">
            <!-- <sc-upload v-model="form.file" :modelValue="form.file" title="证件图片" :width="300" :height="148"></sc-upload> -->
            <sc-upload-file
              v-model="form.file"
              :multiple="false"
              :limit="1"
              accept="image/png, image/jpeg, image/jpeg, pdf"
            >
              <el-button type="primary" icon="el-icon-upload"
                >上传</el-button
              >
            </sc-upload-file>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogcart = false">取 消</el-button>
      <el-button type="primary" @click="submitcert">确 定</el-button>
    </template>
  </el-dialog>
</template>
    <script>
  const  defaultform=
  { id: "",
        name: "",
        number: "",
        employee: ""
        };
export default {
  name: "certificate",
  components: {},
  data() {
    return {
      apiObj: this.$API.hrm.certificate.list,
      form:defaultform,
      typeoptions: [
        { id: 10, name: "特种作业证书" },
        { id: 20, name: "特种设备操作证书" },
        { id: 30, name: "安全管理人员证书" },
      ],
      eptypeOptions: [
        {label:"正式员工", value:"employee"},
        {label:"相关方", value:"remployee"},
        {label:"访客", value:"visitor"},
        {label:"货车司机", value:"driver"}
      ],
      type_: {
        10: "特种作业证书",
        20: "特种设备操作证书",
        30: "安全管理人员证书",
      },
      dialogcart: false,
      query: {},
      selection: [],
      search: {
        keyword: null,
      },
      mode: 'add',
      rules: {
        employee: [{ required: true, message: "请输入" }],
        name: [{ required: true, message: "请输入" }],
        number: [{ required: true, message: "请输入" }],
        type: [{ required: true, message: "请输入" }],
        issue_date: [{ required: true, message: "请输入" }],
        expiration_date: [{ required: true, message: "请输入" }],
        review_date: [{ required: true, message: "请输入" }]
      },
    };
  },
   mounted() {
  

    // this.getcertificatelist();
  },
  methods: {
    handleForm(mode, row) {
      this.mode = mode;
      if (mode === "add") {
        // this.form = Object.assign({}, defaultform);
        this.dialogcart = true;
        this.$nextTick(()=>{
          this.form = Object.assign({}, defaultform);
        })
      
      } else {
        this.dialogcart = true;
        this.form = row;
        
      }
    },
    getEmployee(data) {
      // 子组件调用父组件的方法并传参
      console.log(data)
      this.form.employee=data.id;
      this.form.employee_name=data.name
    },
    handleQuery(){
      this.$refs.table.queryData(this.query)
    },
    //证书列表
    //  getcertificatelist() {
    //   this.$API.hrm.certificate.list
    //     .req({ employee:this.$route.query.id, page: 0 })
    //     .then((res) => {
    //       this.apiObj = res;
    //     });
    // },
    //人员证书创建
    submitcert() {
        this.$refs.dialogForm.validate((valid) => {
          if (this.form.id == "") {
            this.$API.hrm.certificate.create
              .req(this.form)
              .then((res) => {
                this.$message.success("创建成功");
                this.dialogcart = false;
                return res;
              })
              .catch((err) => {
                return err;
              });
          } else {
            this.$API.hrm.certificate.update
              .req(this.form.id, this.form)
              .then((res) => {
                this.$message.success("修改成功");
                this.dialogcart = false;
                return res;
                return res;
              })
              .catch((err) => {
                return err;
              });
          }
        });
    },
    
  },
};
</script>
