<template>
  <div>
    <div class="titleBox">
      <div class="title">专家库管理</div>
      <div class="btnBox">
        <el-button type="primary" @click="add">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
        <el-button
          @click="batchDelete"
          :disabled="ids.length == 0"
          typeof="danger"
        >
          <el-icon style="vertical-align: middle">
            <Delete />
          </el-icon>
          <span style="vertical-align: middle">批量删除</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="专家姓名">
          <el-input
            v-model="listQuery.zjxm"
            placeholder="请输入专家姓名"
            clearable
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="专家级别">
          <el-select
            style="width: 100%"
            v-model="listQuery.zjjb"
            placeholder="请选择专家级别"
          >
            <el-option
              v-for="dict in D_ZDY_ZJJB"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专家类型">
          <el-select
            style="width: 100%"
            placeholder="请选择专家类型"
            v-model="listQuery.zjlx"
          >
            <el-option
              v-for="dict in D_ZDY_ZJLX"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        row-key="id"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"
        :key="keyCount"
        :height="tableHeight"
        v-loading="loadingTable"
        element-loading-background="rgba(0,0,0,0.3)"
        element-loading-text="数据加载中。。"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column label="序号" type="index" align="center" width="60" />
        <el-table-column
          sortable
          label="专家姓名"
          prop="zjxm"
          align="center"
          width="180"
        />
        <el-table-column label="工作单位" prop="gzdw" align="center" />
        <el-table-column label="联系电话" prop="lxdh" align="center" />
        <el-table-column
          show-overflow-tooltip
          label="当前职位"
          prop="dqzw"
          align="center"
        />
        <el-table-column
          show-overflow-tooltip
          label="专家级别"
          prop="zjjb"
          align="center"
        >
          <template #default="{ row }">
            <div>
              <dict-tag
                :options="D_ZDY_ZJJB"
                :value="row.zjjb"
                :tag="false"
              ></dict-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="专家类型"
          prop="zjlx"
          align="center"
        >
          <template #default="{ row }">
            <div v-if="row.zjlx">
              <dict-tag
                :options="D_ZDY_ZJLX"
                :value="JSON.parse(row.zjlx)"
                :tag="true"
              ></dict-tag>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="250px"
        >
          <template #default="{ row }">
            <el-button @click="update(row)" size="small">修改</el-button>
            <el-button @click="delDictItem(row)" type="danger" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div class="dialog" v-if="dialogFormVisible">
      <div class="head_box">
        <span class="title">{{ title }}</span>
        <div>
          <el-button
            :loading="loading"
            type="primary"
            size="small"
            @click="submit"
            >保存</el-button
          >
          <el-button size="small" @click="close">关闭</el-button>
        </div>
      </div>
      <el-form
        ref="elform"
        :model="form"
        :rules="rules"
        :inline="true"
        label-position="top"
      >
        <el-form-item label="专家来源" prop="zjly">
          <el-select
            style="width: 100%"
            v-model="form.zjly"
            placeholder="请选择专家来源"
          >
            <el-option
              v-for="dict in D_ZDY_ZJGL"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专家姓名" prop="zjxm">
          <el-input
            @click="openUser"
            v-model="form.zjxm"
            placeholder="请输入专家姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="专家身份证号" prop="zjsfzh">
          <el-input
            @change="getBirthdayFromIdCard(form.zjsfzh)"
            v-model="form.zjsfzh"
            placeholder="请输入身份证号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="csrq">
          <el-date-picker
            style="width: 100%"
            v-model="form.csrq"
            type="date"
            placeholder="请选择出生日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="民族" prop="mzdm">
          <MOSTY.PackageSelect
            width="100%"
            v-model="form.mzdm"
            placeholder="请选择民族"
            dictEnum="NATION"
            clearable
            filterable
          />
        </el-form-item>
        <el-form-item label="籍贯" prop="jgdm">
          <el-input v-model="form.jg" placeholder="请输入籍贯"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" prop="jtzz">
          <el-input
            v-model="form.jtzz"
            placeholder="请输入家庭住址"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="lxdh">
          <el-input
            v-model="form.lxdh"
            placeholder="请输入联系电话"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="工作单位" prop="gzdw">
          <el-input
            v-model="form.gzdw"
            placeholder="请输入工作单位"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="当前职位" prop="dqzw">
          <el-input
            v-model="form.dqzw"
            placeholder="请输入当前职位"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="专家级别" prop="zjjb">
          <el-select
            style="width: 100%"
            v-model="form.zjjb"
            placeholder="请选择专家级别"
          >
            <el-option
              v-for="dict in D_ZDY_ZJJB"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专家类型" prop="zjlx">
          <el-select
            multiple
            style="width: 100%"
            placeholder="请选择专家类型"
            v-model="form.zjlx"
          >
            <el-option
              v-for="dict in D_ZDY_ZJLX"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="width: 100%" label="专家简介" prop="zjjc">
          <el-input
            v-model="form.zjjc"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
        <el-form-item style="width: 100%" label="荣誉墙" prop="fjdz">
          <MOSTY.Upload width="100%" :limit="2" v-model="form.fjdz" />
        </el-form-item>
        <el-form-item prop="bz" label="备注" style="width: 100%">
          <el-input
            v-model="form.bz"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </div>
    <ChooseUser
      v-model="chooseUserVisible"
      @choosedUsers="hanlderChoose"
    ></ChooseUser>
  </div>
</template>
<!-- 专家库管理 -->
<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import ChooseUser from "@/components/MyComponents/ChooseUser";
import * as rule from "@/utils/rules.js";
import {
  getTbJcglZjk,
  addTbJcglZjk,
  putTbJcglZjk,
  infoTbJcglZjk,
  delTbJcglZjk
} from "@/api/basicsmanage/expertBase.js";
import { ref, getCurrentInstance, onMounted } from "vue";
import { get } from "ol/proj";
const { proxy } = getCurrentInstance();
const { D_ZDY_ZJGL, D_ZDY_ZJJB, D_ZDY_ZJLX } = proxy.$dict(
  "D_ZDY_ZJGL",
  "D_ZDY_ZJJB",
  "D_ZDY_ZJLX"
);
const keyCount = ref(0); //tabel组件刷新值
const form = ref({}); //新增表单
const tableData = ref([]); //表格数据
const total = ref(0);
const chackAdd = ref(false); //新增修改切换
const loading = ref(false); //新增保存按钮截流
const ids = ref([]); //表格选中id集合
const searchBox = ref(null); //搜索盒子
const loadingTable = ref(true); //表格加载状态
const tableHeight = ref(); // 表格高度
const dialogFormVisible = ref(false);
const title = ref("新增专家"); //弹窗标题
const elform = ref(null); //新增表单容器
const chooseUserVisible = ref(false); //用户弹窗
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 10
}); //搜索表单
//表单验证
const rules = ref({
  zjly: [{ required: true, message: "请选择专家来源", trigger: "change" }],
  zjxm: [{ required: true, message: "请输入专家姓名", trigger: "change" }],
  ...rule.identityCardRule(
    {
      require: true,
      validator: true
    },
    "zjsfzh"
  ) //身份证校验
});
//身份证号计算出生日期
function getBirthdayFromIdCard(idCard) {
  var birthday = "";
  if (idCard != null && idCard != "") {
    if (idCard.length == 15) {
      birthday = "19" + idCard.substr(6, 6);
    } else if (idCard.length == 18) {
      birthday = idCard.substr(6, 8);
    }

    birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
  }
  form.value.csrq = birthday;
  form.value.jgdm = idCard.substr(0, 6);
  // return birthday;
}
// 获取列表
function getListData() {
  loadingTable.value = true;
  getTbJcglZjk(listQuery.value).then((res) => {
    tableData.value = res.records;
    total.value = res.total;
    loadingTable.value = false;
  }).catch(()=>{
    loadingTable.value = false;
  });
}
//打开用户弹窗
function openUser() {
  if (form.value.zjly == "1") {
    chooseUserVisible.value = true;
  }
}
//选择用户
const hanlderChoose = (users) => {
  const user = users[0];
  form.value.zjxm = user.userName;
  form.value.zjsfzh = user.idEntityCard;
  form.value.lxdh = user.mobile;
  form.value.gzdw = user.deptName;
  form.value.dqzw = user.positionName;
  form.value.mzdm = user.nation;
  getBirthdayFromIdCard(user.idEntityCard);
};
//打开新增弹窗
function add() {
  title.value = '新增专家'
  chackAdd.value = true;
  dialogFormVisible.value = true;
}
//关闭弹窗
function close() {
  dialogFormVisible.value = false;
  form.value = {};
}
//提交
function submit() {
  elform.value.validate((valid) => {
    if (valid) {
      const params = JSON.parse(JSON.stringify(form.value));
      params.zjlx = JSON.stringify(params.zjlx);
      loading.value = true;
      setTimeout(() => {
        loading.value = false;
      }, 1500);
      if (chackAdd.value) {
        addTbJcglZjk(params).then((res) => {
          proxy.$message({
            type: "success",
            message: "新增成功"
          });
          close();
          getListData();
        });
      } else {
        putTbJcglZjk(params).then((res) => {
          proxy.$message({
            type: "success",
            message: "修改成功"
          });
          close();
          getListData();
        });
      }
    }
  });
}
//修改
function update(row) {
  chackAdd.value = false;
  title.value = "修改专家信息";
  infoTbJcglZjk(row.id).then((res) => {
    form.value = res;
    form.value.zjlx = JSON.parse(res.zjlx);
    dialogFormVisible.value = true;
  });
}
//批量数据
const handleSelectionChange = (val) => {
  ids.value = [];
  if (val) {
    val.forEach((item) => {
      ids.value.push(item.id);
    });
  }
};
//删除
function delDictItem(row) {
  proxy
    .$confirm("确定要删除", "警告", {
      type: "warning"
    })
    .then(() => {
      delTbJcglZjk([row.id]).then(() => {
        proxy.$message({
          type: "success",
          message: "删除成功"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
}
//批量删除
function batchDelete() {
  proxy
    .$confirm("确定要删除", "警告", {
      type: "warning"
    })
    .then(() => {
      delTbJcglZjk(ids.value).then((res) => {
        proxy.$message({
          message: "删除成功",
          type: "success"
        });
        getListData();
      });
    })
    .catch(() => {
      proxy.$message.info("已取消");
    });
}
//点击查询
const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  getListData();
};
//点击重置
const reset = () => {
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 10
  };
  getListData();
};
// 表格高度计算
const tabHeightFn = () => {
 tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
/**
 * pageSize 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
onMounted(() => {
  getListData();
  tabHeightFn();
  window.onresize = function () { tabHeightFn();};
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
</script>

<style lang='scss' scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
</style>