<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="人员姓名" prop="persName">
        <el-input v-model="queryParams.persName" placeholder="请输入人员姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="人员类型" prop="persType">
        <el-select v-model="queryParams.persType" placeholder="请选择人员类型" clearable>
          <el-option v-for="dict in ttsa_pers_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="组织GUID" prop="orgGuid">
        <el-input v-model="queryParams.orgGuid" placeholder="请输入组织GUID" clearable @keyup.enter="handleQuery" />
      </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="['ttsa:objtts:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ttsa:objtts:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ttsa:objtts:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ttsa:objtts:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="ttsa-objtts-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="身份证号" prop="citNum" />
      <el-table-column label="人员姓名" prop="persName" />
      <el-table-column label="性别" prop="sex" />
      <el-table-column label="人员类型" prop="persType" width="120">
        <template #default="scope">
          <dict-tag :options="ttsa_pers_type" :value="scope.row.persType" />
        </template>
      </el-table-column>
      <el-table-column label="技术职称" prop="techTit" />
      <el-table-column label="专业" prop="major" />
      <el-table-column label="岗位职务" prop="postDuty" />
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ttsa:objtts:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ttsa:objtts:remove']"></el-button>
          </el-tooltip>
        </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 :title="title" v-model="open" width="750px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="身份证号" prop="citNum">
          <el-input v-model="form.citNum" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="人员GUID" prop="persGuid">
          <el-input v-model="form.persGuid" placeholder="请输入人员GUID" />
        </el-form-item>
        <el-form-item label="组织项目部门GUID" prop="orgProjDeptGuid">
          <el-input v-model="form.orgProjDeptGuid" placeholder="请输入组织项目部门GUID" />
        </el-form-item>
        <el-form-item label="人员姓名" prop="persName">
          <el-input v-model="form.persName" placeholder="请输入人员姓名" />
        </el-form-item>
        <el-form-item label="出生地" prop="bornPlac">
          <el-input v-model="form.bornPlac" placeholder="请输入出生地" />
        </el-form-item>
        <el-form-item label="人员类型" prop="persType">
          <el-select v-model="form.persType" placeholder="请选择人员类型">
            <el-option v-for="dict in ttsa_pers_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="组织GUID" prop="orgGuid">
          <el-input v-model="form.orgGuid" placeholder="请输入组织GUID" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="出生日期" prop="bornDate">
          <el-date-picker v-model="form.bornDate" type="datetime" placeholder="选择出生日期" clearable />
        </el-form-item>
        <el-form-item label="最高学历" prop="highEdu">
          <el-input v-model="form.highEdu" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="技术职称" prop="techTit">
          <el-input v-model="form.techTit" placeholder="请输入技术职称" />
        </el-form-item>
        <el-form-item label="学校名称" prop="schlName">
          <el-input v-model="form.schlName" placeholder="请输入学校名称" />
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="毕业日期" prop="grauDate">
          <el-date-picker v-model="form.grauDate" type="datetime" placeholder="选择毕业日期" clearable />
        </el-form-item>
        <el-form-item label="参加工作日期" prop="workDate">
          <el-date-picker v-model="form.workDate" type="datetime" placeholder="选择参加工作日期" clearable />
        </el-form-item>
        <el-form-item label="任职日期" prop="postDate">
          <el-date-picker v-model="form.postDate" type="datetime" placeholder="选择任职日期" clearable />
        </el-form-item>
        <el-form-item label="B证申请日期" prop="bemoDate">
          <el-date-picker v-model="form.bemoDate" type="datetime" placeholder="选择B证申请日期" clearable />
        </el-form-item>
        <el-form-item label="工作简历" prop="workResum">
          <el-input v-model="form.workResum" type="textarea" placeholder="请输入工作简历" />
        </el-form-item>
        <el-form-item label="注册建造师情况" prop="archEngSm">
          <el-input v-model="form.archEngSm" placeholder="请输入注册建造师情况" />
        </el-form-item>
        <el-form-item label="岗位职务" prop="postDuty">
          <el-input v-model="form.postDuty" placeholder="请输入岗位职务" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ObjTts">
import { listObjTts, getObjTts, addObjTts, updateObjTts, delObjTts } from "@/api/slaj/ttsa";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const { ttsa_pers_type } = proxy.useDict("ttsa_pers_type");

const dataList = 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 columns = ref({ citNum: { visible: true }, persName: { visible: true }, sex: { visible: true }, persType: { visible: true }, techTit: { visible: true }, major: { visible: true }, postDuty: { visible: true } });

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, persName: null, persType: null, orgGuid: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

function getList() {
  loading.value = true;
  listObjTts(queryParams.value).then(r => {
    dataList.value = r.rows;
    total.value = r.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(s) {
  ids.value = s.map(i => i.guid);
  single.value = s.length !== 1;
  multiple.value = !s.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加三类人员信息";
}

function handleUpdate(row) {
  reset();
  getObjTts(row.guid || ids.value[0]).then(r => {
    form.value = r.data;
    open.value = true;
    title.value = "修改三类人员信息";
  });
}

function submitForm() {
  proxy.$refs["formRef"].validate(v => {
    if (v) {
      if (form.value.guid != null) {
        updateObjTts(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addObjTts(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  proxy.$modal.confirm('确认删除？').then(() => delObjTts(row.guid || ids.value.join(","))).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  });
}

function handleExport() {
  proxy.download('/ttsa/objtts/export', { ...queryParams.value }, `三类人员信息_${new Date().getTime()}.xlsx`);
}

function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.value = { guid: null, citNum: null, persGuid: null, orgProjDeptGuid: null, persName: null, bornPlac: null, persType: null, orgGuid: null, sex: null, bornDate: null, highEdu: null, techTit: null, schlName: null, major: null, grauDate: null, workDate: null, postDate: null, bemoDate: null, workResum: null, archEngSm: null, postDuty: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
