<!-- @format -->

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="卡号" prop="cardNumber">
        <el-input
          v-model="queryParams.cardNumber"
          placeholder="请输入卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="姓名" prop="chineseName">
        <el-input
          v-model="queryParams.chineseName"
          placeholder="请输入中文名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="证件号" prop="idNum">
        <el-input
          v-model="queryParams.idNum"
          placeholder="请输入证件号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="制卡状态" prop="zhiKaState">
        <el-select
          v-model="queryParams.zhiKaState"
          placeholder="请选择制卡状态"
          clearable
        >
          <el-option
            v-for="dict in zhi_ka_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <!-- 
      <el-form-item label="复核日期">
        <el-date-picker
          v-model="value1"
          type="daterange"
          range-separator="To"
          start-placeholder="Start date"
          end-placeholder="End date"
          :size="size"
        />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['talent:list:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          @click="importDia = true"
          v-hasPermi="['talent:list:export']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['talent:list:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="listData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" type="index" width="100">
        <template #default="scope">
          <span>{{ handleTableIndex(scope.$index, queryParams) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="卡号"
        width="200"
        align="center"
        prop="cardNumber"
      />
      <el-table-column
        width="150"
        label="姓名"
        align="center"
        prop="chineseName"
      />

      <el-table-column width="150" label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.gender" />
        </template>
      </el-table-column>

      <el-table-column label="证件类型" align="center">
        <template #default="scope">
          <dict-tag
            :options="zheng_jian_type"
            :value="scope.row.zhengJianType"
          />
        </template>
      </el-table-column>
      <el-table-column label="证件号" width="200" align="center" prop="idNum">
        <template #default="scope">
          <span
            >{{ maskIdNumber(scope.row.idNum, scope.row.isIdView) }}
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isIdView = true"
              v-if="!scope.row.isIdView"
              ><View />
            </el-icon>
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isIdView = false"
              v-else
              ><Hide
            /></el-icon>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        width="200"
        label="手机号码"
        align="center"
        prop="shouJiNum"
      >
        <template #default="scope">
          <span
            >{{ maskPhoneNumber(scope.row.shouJiNum, scope.row.isPhoneView) }}
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isPhoneView = true"
              v-if="!scope.row.isPhoneView"
              ><View />
            </el-icon>
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isPhoneView = false"
              v-else
              ><Hide
            /></el-icon>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        width="200"
        label="工作单位"
        align="center"
        prop="yrdwName"
      />

      <el-table-column
        label="制卡类型"
        width="150"
        align="center"
        prop="zhiKaType"
      >
        <template #default="scope">
          <dict-tag :options="zhi_ka_type" :value="scope.row.zhiKaType" />
        </template>
      </el-table-column>
      <el-table-column
        label="制卡状态"
        width="150"
        align="center"
        prop="zhiKaState"
      >
        <template #default="scope">
          <dict-tag :options="zhi_ka_state" :value="scope.row.zhiKaState" />
        </template>
      </el-table-column>
      <el-table-column
        label="制卡时间"
        align="center"
        prop="zhiKaDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.zhiKaDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="出卡时间"
        align="center"
        prop="chuKaDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.chuKaDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="复核截止日期"
        align="center"
        prop="fuheJiezhiDate"
        width="200"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.fuheJiezhiDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否超过复核截止日期"
        align="center"
        prop="fuheJiezhiDate"
        width="200"
      >
        <template #default="scope">
          <el-tag
            v-if="scope.row.fuheJiezhiDate"
            :type="
              fuheJiezhiDate(
                parseTime(scope.row.fuheJiezhiDate, '{y}-{m}-{d}')
              ) > 0
                ? 'danger'
                : 'primary'
            "
            >{{
              fuheJiezhiDate(
                parseTime(scope.row.fuheJiezhiDate, "{y}-{m}-{d}")
              ) > 0
                ? "是"
                : "否"
            }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="英才卡有效日期"
        align="center"
        prop="cardYouxiaoDate"
        width="200"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cardYouxiaoDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否超过英才卡有效日期"
        align="center"
        prop="fuheJiezhiDate"
        width="200"
      >
        <template #default="scope">
          <el-tag
            v-show="scope.row.cardYouxiaoDate"
            :type="
              cardYouxiaoDate(
                parseTime(scope.row.cardYouxiaoDate, '{y}-{m}-{d}')
              ) > 0
                ? 'danger'
                : 'primary'
            "
            >{{
              cardYouxiaoDate(
                parseTime(scope.row.cardYouxiaoDate, "{y}-{m}-{d}")
              ) > 0
                ? "是"
                : "否"
            }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="230"
      >
        <template #default="scope">
          <el-button
            size="small"
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['talent:list:edit']"
            >修改</el-button
          >
          <el-button
            size="small"
            link
            type="primary"
            icon="Refresh"
            @click="
              activeItem = { ...scope.row };
              logoutDia = true;
            "
            v-hasPermi="['talent:list:remove']"
            >注销</el-button
          >
          <el-button
            size="small"
            link
            type="primary"
            icon="Back"
            @click="
              activeItem = { ...scope.row };
              ReviewDia = true;
            "
            v-hasPermi="['talent:list:remove']"
            >复核</el-button
          >
          <el-button
            size="small"
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['talent:list:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      v-model="importDia"
      title="人才导入"
      width="40%"
      :before-close="
        () => {
          importDia = false;
        }
      "
    >
      <el-form>
        <el-form-item label="导入文件">
          <ImportMaterials
            :limit="1"
            :isRequired="false"
            v-model="perosnMaterialFile"
            baseUrl="/talent/list/importDataA"
          >
          </ImportMaterials>
        </el-form-item>
        <el-form-item label="材料上传">
          <ImportMaterials
            :limit="100"
            baseUrl="/talent/list/importDataMaterialsA"
            v-model:perosnMaterialFile="perosnMaterialFile"
            v-model="personMaterialUpload"
            :isRequired="true"
            :fileType="['png', 'jpg']"
          >
          </ImportMaterials>
        </el-form-item>
      </el-form>
      <div style="display: flex; align-items: center">
        A类英才卡导入模板:

        <span
          @click="handleDownload"
          style="color: #0e62fe; cursor: pointer"
          class="el-icon-document"
        >
          A类英才卡导入模板
        </span>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="importDia = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      v-model="logoutDia"
      title="人员注销"
      width="30%"
      :before-close="
        () => {
          logoutDia = false;
        }
      "
    >
      <el-form label-width="120px">
        <el-form-item label="姓名：">
          <el-input
            style="width: 270px"
            readonly
            v-model="activeItem.chineseName"
          ></el-input>
        </el-form-item>
        <el-form-item label="注销原因：">
          <el-input
            type="textarea"
            style="width: 270px"
            :rows="3"
            v-model="activeItem.zhuxiaoReason"
            placeholder="请输入注销原因"
          >
          </el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="logoutDia = false">取消</el-button>
          <el-button type="primary" @click="handleLogout">提交</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      v-model="ReviewDia"
      title="人员复核"
      width="30%"
      :before-close="
        () => {
          ReviewDia = false;
        }
      "
    >
      <el-form
        ref="reviewRef"
        label-width="120px"
        :rules="reviewRules"
        :model="activeItem"
      >
        <el-form-item label="姓名">
          <el-input
            disabled
            style="width: 270px"
            readonly
            v-model="activeItem.chineseName"
          ></el-input>
        </el-form-item>
        <el-form-item label="证件类型">
          <el-select
            disabled
            style="width: 270px"
            v-model="activeItem.zhengJianType"
          >
            <el-option
              v-for="item of zheng_jian_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号">
          <el-input
            disabled
            style="width: 270px"
            readonly
            v-model="activeItem.idNum"
          ></el-input>
        </el-form-item>
        <el-form-item label="英才卡卡号">
          <el-input
            disabled
            style="width: 270px"
            readonly
            v-model="activeItem.cardNumber"
          ></el-input>
        </el-form-item>
        <el-form-item label="复核截止日期" prop="fuheJiezhiDate">
          <el-date-picker
            style="width: 270px"
            v-model="activeItem.fuheJiezhiDate"
            type="date"
            placeholder="请选择复核截止日期"
          />
        </el-form-item>
        <el-form-item label="英才卡有效日期">
          <el-date-picker
            disabled
            style="width: 270px"
            readonly
            :value="parseTime(activeItem.cardYouxiaoDate, '{y}-{m}-{d}')"
            type="date"
          />
        </el-form-item>
        <el-form-item label="单位名称">
          <el-input
            disabled
            style="width: 270px"
            readonly
            v-model="activeItem.yrdwName"
          ></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="ReviewDia = false">取消</el-button>
          <el-button type="primary" @click="handleReview(reviewRef)"
            >提交</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="List">
import {
  listList,
  getListData,
  delList,
  addList,
  updateList,
  saveLogout,
  saveReview,
} from "@/api/talentManagement/category";
import { computed } from "vue";
import { download } from "@/utils/request";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const router = useRouter();
const { proxy } = getCurrentInstance();
const {
  zhi_ka_type,
  is_zhuxiao,
  zhi_ka_state,
  card_type,
  yjrc_type,
  politics_status,
  zheng_jian_type,
  sys_user_sex,
} = proxy.useDict(
  "sys_user_sex",
  "zhi_ka_type",
  "is_zhuxiao",
  "zhi_ka_state",
  "card_type",
  "yjrc_type",
  "politics_status",
  "zheng_jian_type"
);

const listData = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const importDia = ref(false);
const logoutDia = ref(false);
const activeItem = {};
const ReviewDia = ref(false);
const reviewRef = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sehnqingrenId: null,
    cardType: "A",
    cardNumber: null,
    sehnqingNumber: null,
    isZhuxiao: "0",
  },
  rules: {},
});

const reviewRules = {
  fuheJiezhiDate: [
    {
      required: true,
      message: "复核截止日期不能为空",
      trigger: "blur",
    },
  ],
};

const { queryParams, form, rules } = toRefs(data);

const perosnMaterialFile = ref("");
const personMaterialUpload = ref("");

/** 查询人才库列列表 */
function getList() {
  loading.value = true;
  listList(queryParams.value).then((response) => {
    listData.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
//下载模板
const handleDownload = () => {
  download("/talent/list/importTemplateA", {}, "A类英才卡导入模板.xlsx");
};

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

//是否超过复核日期
const fuheJiezhiDate = computed(() => {
  return (date) => {
    return new Date().getTime() - new Date(date).getTime();
  };
});

//是否超过英才卡有效日期
const cardYouxiaoDate = computed(() => {
  return (date) => {
    return new Date().getTime() - new Date(date).getTime();
  };
});

//复核提交
const handleReview = (formRef) => {
  formRef.validate((valid) => {
    if (valid) {
      saveReview({
        id: activeItem.value.id,
        fuheJiezhiDate: activeItem.value.fuheJiezhiDate,
      }).then((res) => {
        ElMessage({
          type: "success",
          message: "提交成功",
        });
        ReviewDia.value = false;
        getList();
      });
    }
  });
};

//材料保存
const onSubmitMaterial = () => {};

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  router.push({
    path: "/materialApplication",
    query: {
      methodType: "post",
    },
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  router.push({
    path: "/materialApplication",
    query: {
      type: "A",
      materialCalssId: row.id,
      methodType: "put",
    },
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["listRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateList(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addList(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除选中的数据项？")
    .then(function () {
      return delList(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

//注销人才
const handleLogout = (row) => {
  saveLogout({
    isZhuxiao: "1",
    zhuxiaoReason: activeItem.value.zhuxiaoReason,
    id: activeItem.value.id,
  }).then((res) => {
    ElMessage({
      type: "success",
      message: "提交成功",
    });
    logoutDia.value = false;
    getList();
  });
};

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "talent/list/export",
    {
      ...queryParams.value,
    },
    `list_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
