<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <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="major">
        <el-input v-model="queryParams.major" placeholder="请输入专业" 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="['sins:objexpert:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['sins:objexpert:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['sins:objexpert:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['sins:objexpert:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="sins-objexpert-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="姓名" align="center" prop="persName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="职称" align="center" prop="techTit" />
      <el-table-column label="专家类型" align="center" prop="sinsExpertType" />
      <el-table-column label="专家等级" align="center" prop="expertGrad" />
      <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="['sins:objexpert:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sins:objexpert: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="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="persName">
          <el-input v-model="form.persName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" 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="curMajor">
          <el-input v-model="form.curMajor" 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="orgGuid">
          <el-input v-model="form.orgGuid" placeholder="请输入所属单位" />
        </el-form-item>
        <el-form-item label="办公电话" prop="offiTel">
          <el-input v-model="form.offiTel" placeholder="请输入办公电话" />
        </el-form-item>
        <el-form-item label="手机" prop="moph">
          <el-input v-model="form.moph" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="住宅电话" prop="homeTel">
          <el-input v-model="form.homeTel" placeholder="请输入住宅电话" />
        </el-form-item>
        <el-form-item label="专家类型" prop="sinsExpertType">
          <el-input v-model="form.sinsExpertType" placeholder="请输入专家类型" />
        </el-form-item>
        <el-form-item label="专家等级" prop="expertGrad">
          <el-input v-model="form.expertGrad" placeholder="请输入专家等级" />
        </el-form-item>
        <el-form-item label="身份证号" prop="citNum">
          <el-input v-model="form.citNum" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="出生日期" prop="bornDate">
          <el-date-picker v-model="form.bornDate" type="date" placeholder="选择出生日期" />
        </el-form-item>
        <el-form-item label="行政职务" prop="admDuty">
          <el-input v-model="form.admDuty" placeholder="请输入行政职务" />
        </el-form-item>
        <el-form-item label="政治面貌" prop="poafCode">
          <el-input v-model="form.poafCode" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="邮编" prop="zip">
          <el-input v-model="form.zip" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="专家简历" prop="expertResid">
          <el-input v-model="form.expertResid" type="textarea" placeholder="请输入专家简历" />
        </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="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="SinsExpert">
import { listObjExpert, getObjExpert, addObjExpert, updateObjExpert, delObjExpert } from "@/api/slaj/sins";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();

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({
  persName: { label: "姓名", visible: true },
  sex: { label: "性别", visible: true },
  major: { label: "专业", visible: true },
  techTit: { label: "职称", visible: true },
  sinsExpertType: { label: "专家类型", visible: true },
  expertGrad: { label: "专家等级", visible: true }
});

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

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

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

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

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

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加专家信息";
}

function handleUpdate(row) {
  reset();
  getObjExpert(row.guid || ids.value[0]).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改专家信息";
  });
}

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

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

function handleExport() {
  proxy.download('/sins/objexpert/export', { ...queryParams.value }, `专家信息_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { persName: null, sex: null, major: null, curMajor: null, techTit: null, orgGuid: null, offiTel: null, moph: null, homeTel: null, sinsExpertType: null, expertGrad: null, citNum: null, bornDate: null, admDuty: null, poafCode: null, addr: null, zip: null, expertResid: null, workResum: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
