<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-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				<el-button type="primary" plain :disabled="selection.length!=1" @click="permission">权限设置</el-button> -->
      </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"
        :data="apiObj"
        row-key="id"
       
        stripe
       
      >
       
        <el-table-column label="#" fixed="left" type="index" width="50"></el-table-column>
         <el-table-column label="姓名" fixed="left" prop="employee_name"></el-table-column>
        <el-table-column label="证书名称" fixed="left" prop="name"></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="state">
							<template #default="scope">
								<el-tag
									:type="certStateEnum[scope.row.state]?.type"
									>{{
										certStateEnum[scope.row.state]?.text
									}}</el-tag
								>
							</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="证书文件" prop="file"></el-table-column> 
      </scTable>
    </el-main>
  </el-container>

  <sc-dialog v-model="dialogcart" draggable title="创建人员证书">
    <el-form
      ref="dialogForm"
      :model="form"
      :rules="rules"
      :disabled="mode === 'show'"
      label-width="120px"
    >
      <el-row>
        <el-col :md="12" :sm="24">
          <el-form-item label="证书名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入证书名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="证书编号" prop="number">
            <el-input
              v-model="form.number"
              placeholder="请输入证书编号"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <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-col>
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="发证日期">
            <el-date-picker
              v-model="form.issue_date"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择发证日期"
            />
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="有效期">
            <el-date-picker
              v-model="form.expiration_date"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择有效期"
            />
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="下一次复审日期">
            <el-date-picker
              v-model="form.review_date"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择下一次复审日期"
            />
          </el-form-item>
        </el-col>
        <el-col :md="24" :sm="12" :xs="24">
          <el-form-item label="上传证书文件">
            <sc-upload-file
              v-model="form.file"
              :limit="5"
              :data="{ otherData: 'demo' }"
              tip="最多上传3个文件,单个文件不要超过10M,请上传xlsx/docx格式文件"
            >
              <el-button type="primary" icon="el-icon-upload"
                >上传附件</el-button
              >
            </sc-upload-file>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogcart = false">取 消</el-button>
      <el-button type="primary" @click="submitcert">确 定</el-button>
    </template>
  </sc-dialog>
</template>
<script>
import { certStateEnum } from "@/utils/enum.js";
  const  defaultform=
  { id: "",
        name: "",
        number: "",
        employee: ""
        };
export default {
  name: "certificate",
  components: {},
  data() {
    return {
      certStateEnum,
      form:defaultform,
      typeoptions: [
        { id: 10, name: "特种作业证书" },
        { id: 20, name: "特种设备操作证书" },
        { id: 30, name: "安全管理人员证书" },
      ],
      type_: {
        10: "特种作业证书",
        20: "特种设备操作证书",
        30: "安全管理人员证书",
      },
      dialogcart: false,
      apiObj:[],
      query: {},
      selection: [],
      search: {
        keyword: null,
      },
    };
  },
   mounted() {
  

    this.getcertificatelist();
  },
  methods: {
    handleForm(type, row) {
      if (type === "add") {
        this.dialogcart = true;
         this.form = Object.assign({}, defaultform);
        
      
      } else {
        this.dialogcart = true;
        this.form = row;
        
      }
    },
    handleQuery() {
				this.$refs.table.queryData(this.query)
			},
    //证书列表
     getcertificatelist() {
      this.$API.hrm.certificate.list
        .req({  page: 0 })
        .then((res) => {
          this.apiObj = res;
        });
    },
    //人员证书创建
    submitcert() {
      this.form.employee = this.$route.query.id;
        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>
