<template>
  <div class="app-container">
    <div style="margin-top: 10px">
      <el-input
        v-model="listQuery.search"
        placeholder="输入关键词进行搜索"
        style="width: 300px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh-left"
        @click="resetFilter"
        >刷新重置</el-button
      >
      <el-button
        type="primary"
        @click="handleAdd"
        v-if="checkPermission(['certificate_create'])"
        >手动创建</el-button
      >
    </div>

    <el-table
      :data="tableData.results"
      style="width: 100%; margin-top: 10px"
      border
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
      row-key="id"
      default-expand-all
    >
      <el-table-column label="证书号">
        <template slot-scope="scope">{{ scope.row.number }}</template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="scope">{{ scope.row.consumer_name }}</template>
      </el-table-column>
      <el-table-column label="身份证号">
        <template slot-scope="scope">{{ scope.row.ID_number }}</template>
      </el-table-column>
      <el-table-column label="工作类别">
        <template slot-scope="scope">{{ scope.row.workscope_name }}</template>
      </el-table-column>
      <el-table-column label="单位">
        <template slot-scope="scope">{{ scope.row.company_name }}</template>
      </el-table-column>
      <el-table-column label="有效期始">
        <template slot-scope="scope">{{ scope.row.start_date }}</template>
      </el-table-column>
      <el-table-column label="有效期止">
        <template slot-scope="scope">{{ scope.row.end_date }}</template>
      </el-table-column>
      <el-table-column label="手动创建">
        <template slot-scope="scope">
          <span v-if="scope.row.is_manual">是</span>
          <span v-else>否</span></template
        >
      </el-table-column>
      <el-table-column label="创建人">
        <template slot-scope="scope">
          <span>{{ scope.row.create_admin_username }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleView(scope)"
            icon="el-icon-more"
            plain="true"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            plain="true"
            @click="handleEdit(scope)"
            icon="el-icon-edit"
            v-if="checkPermission(['certificate_update'])"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.count > 0"
      :total="tableData.count"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'update' ? '编辑' : '新增'"
    >
      <el-form
        :model="candidate"
        label-width="80px"
        label-position="right"
        :rules="candidateRule"
        ref="candidateForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="报告单号" prop="number">
              <el-input v-model="candidate.number" placeholder="报告单号" />
            </el-form-item>
            <el-form-item label="姓名" prop="consumer_name">
              <el-input v-model="candidate.consumer_name" placeholder="姓名" />
            </el-form-item>
            <el-form-item label="身份证号" prop="ID_number">
              <el-input v-model="candidate.ID_number" placeholder="身份证号" />
            </el-form-item>
            <el-form-item label="工作类别" prop="workscope_name">
              <el-input
                v-model="candidate.workscope_name"
                placeholder="工作类别"
              />
            </el-form-item>
            <el-form-item label="单位" prop="company_name">
              <el-input v-model="candidate.company_name" placeholder="单位" />
            </el-form-item>
            <el-form-item label="部门" prop="deptname">
              <el-input v-model="candidate.deptname" placeholder="部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发证日期" prop="issue_date">
              <el-date-picker
                v-model="candidate.issue_date"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="有效期始" prop="start_date">
              <el-date-picker
                v-model="candidate.start_date"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="有效期止" prop="end_date">
              <el-date-picker
                v-model="candidate.end_date"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="考试时间" prop="examtest_date">
              <el-date-picker
                v-model="candidate.examtest_date"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="照片" prop="photo">
              <el-upload
                class="avatar-uploader"
                :action="upUrl"
                accept="image/jpeg, image/gif, image/png, image/bmp"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                :headers="upHeaders"
              >
                <img
                  v-if="candidate.photo"
                  :src="candidate.photo"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmCandidate('candidateForm')"
          >确认</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 120px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 120px;
  display: block;
}
</style>
<script>
import {
  getCandidateList,
  createCandidate,
  updateCandidate,
} from "@/api/candidate";
import { deepClone } from "@/utils";
import { upUrl, upHeaders } from "@/api/file";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";

const listQuery = {
  page: 1,
  limit: 20,
  search: "",
};
export default {
  components: { Pagination },
  data() {
    return {
      upHeaders: upHeaders(),
      upUrl: upUrl(),
      listQuery: listQuery,
      tableData: { count: 0 },
      listLoading: true,
      dialogVisible: false,
      dialogType: "create",
      candidate: { photo: "" },
      candidateRule: {
        number: [{ required: true, message: "请输入编号", trigger: "blur" }],
      },
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    handleAvatarSuccess(res, file) {
      this.candidate.photo = res.data.path;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    getList() {
      this.listLoading = true;
      getCandidateList(this.listQuery).then((response) => {
        this.tableData = response.data;
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        limit: 20,
        search: "",
      };
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleView(scope) {
      window.open(
        "https://apitest.ahctc.cn/crm/candidate/img?id=" + scope.row.id
        // "http://localhost:8000/crm/candidate/img?id=" + scope.row.id
      );
    },
    handleAdd() {
      this.dialogVisible = true;
      this.dialogType = "create";
      this.$nextTick(() => {
        this.$refs["candidateForm"].clearValidate();
      });
    },
    handleEdit(scope) {
      this.candidate = scope.row;
      this.dialogType = "update";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["candidateForm"].clearValidate();
      });
    },
    confirmCandidate(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "update";
          if (isEdit) {
            updateCandidate(this.candidate.id, this.candidate).then(() => {
              this.getList();
              this.dialogVisible = false;
              this.$message.success("成功");
            });
          } else {
            createCandidate(this.candidate).then((res) => {
              this.getList();
              this.dialogVisible = false;
              this.$message.success("成功");
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
