<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleForm('add')"
          v-auth="'rcertificate.create'"
        ></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"
        :apiObj="apiObj"
        row-key="id"
        @selection-change="selectionChange"
        stripe
        @resetQuery="resetQuery"
      >
        <el-table-column
          label="#"
          fixed="left"
          type="index"
          width="50"
        ></el-table-column>
        <el-table-column label="证书名称" fixed="left" width="150">
          <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="remployee_name"> </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" width="170">
          <template #default="scope">
              <el-button
                link
                type="warning"
                size="small"
                @click="handleForm('edit', scope.row)"
                v-auth="'rcertificate.update'"
                >编辑</el-button
              >

              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button
                    link
                    type="danger"
                    size="small"
                    v-auth="'rcertificate.delete'"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
          </template>
        </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="24" :sm="24" :xs="24">
          <el-form-item label="选择相关方人员">
            <el-select v-model="form.remployee" placeholder="选择相关方人员" :disabled="mode != 'add'">
              <el-option
                v-for="item in remployeeoptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="证书名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入证书名称"
              clearable
              :disabled="mode != 'add'"
            ></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
              :disabled="mode != 'add'"
            ></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="选择证书类型" :disabled="mode != 'add'">
              <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"
              :multiple="false"
              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-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>
const defaultform = { id: "", name: "", number: "", employee: "" };
export default {
  name: "certificate",
  components: {},
  data() {
    return {
      form: defaultform,
      typeoptions: [
        { id: 10, name: "特种作业证书" },
        { id: 20, name: "特种设备操作证书" },
        { id: 30, name: "安全管理人员证书" },
      ],
      type_: {
        10: "特种作业证书",
        20: "特种设备操作证书",
        30: "安全管理人员证书",
      },
      dialogcart: false,
      apiObj: this.$API.rpm.rcertificate.list,
      query: {},
      selection: [],
      remployeeoptions: "",
      search: {
        keyword: null,
      },
    };
  },
  mounted() {
    this.getUser();
  },
  methods: {
    handleQuery() {
      this.$refs.table.queryData(this.query);
    },
    handleForm(type, row) {
      this.mode = type
      if (type === "add") {
        this.dialogcart = true;
        this.form = Object.assign({}, defaultform);
      } else {
        this.dialogcart = true;
        this.form = row;
      }
    },
    async table_del(row) {
      this.$API.rpm.rcertificate.delete
        .req(row.id)
        .then((res) => {
          this.$message.success("删除成功");

          return res;
        })
        .catch((err) => {
          return err;
        });
    },
    //相关方人员选择
    getUser() {
      this.$API.rpm.remployee.list.req({ page: 0 }).then((res) => {
        this.remployeeoptions = res;
      });
    },
    //人员证书创建
    submitcert() {
      this.form.employee = this.$route.query.id;
      this.$refs.dialogForm.validate((valid) => {
        if (this.form.id == "") {
          this.$API.rpm.rcertificate.create
            .req(this.form)
            .then((res) => {
              this.$message.success("创建成功");
              this.dialogcart = false;
              return res;
            })
            .catch((err) => {
              return err;
            });
        } else {
          this.$API.rpm.rcertificate.update
            .req(this.form.id, this.form)
            .then((res) => {
              this.$message.success("修改成功");
              this.dialogcart = false;
              return res;
              return res;
            })
            .catch((err) => {
              return err;
            });
        }
      });
    },
  },
};
</script>
