<template>
  <MOSTY.PageTree :pageTitle="'上岗证管理'" :searchType="'03'" @getDeptId="getdeptId">
    <template #serach>
      <el-divider />
      <MOSTY.Search :searchArr="searchArr" @submit="addUserHander" />
      <el-divider />
      <section class="button">
        <!-- 只有车管员才能申请 v-if="showklc"  -->
        <el-button type="primary" v-if="showklc || isLeaderOrAqbm" @click="addInfo">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>申办</span>
        </el-button>
        <el-button type="primary" @click="addSyinfo" v-if="showklc || isLeaderOrAqbm">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>新增</span>
        </el-button>
        <el-button type=" primary" v-loading="loadings" @click="onClickDownload">
          <el-icon>
            <Download />
          </el-icon>
          <span>导出</span>
        </el-button>
      </section>
    </template>
    <template #table>
      <MOSTY.ElTabs v-model="activeName" :titleList="editObj.tableTabls" @handleChange="onTabsChange" />
      <!-- 表格 -->
      <div class="tabBox">
        <!-- 申办  表格 -->
        <SbTable :dict="{ D_TB_DRIVELEVEL, D_BZ_AQRYWHCD, D_BZ_XB }" @clickBut="onClickOperation"
          :params="syTableParams" v-if="activeName == 'sblb'" ref="sbTable" />
        <!-- 审验  表格 -->
        <SyTable :dict="{ D_TB_DRIVELEVEL, D_BZ_AQRYWHCD, D_BZ_XB }" @clickBut="onClickOperation"
          :params="syTableParams" :showklc='showklc || isLeaderOrAqbm' v-if="activeName == 'sylb'" ref="syTable" />
        <!-- 审验记录 -->
        <SyjlTable :dict="{ D_BZ_XB }" :params="syTableParams" v-if="activeName == 'syjllb'" ref="syjlTable" />
      </div>
    </template>
  </MOSTY.PageTree>
  <div class="dialog-box" v-if="comprehensiveShow">
    <Sgzsbxq :saveBtnShow="saveBtnShow" :editObj="editObj" :showklc='showklc || isLeaderOrAqbm'
      :dict="{ D_TB_DRIVELEVEL, D_BZ_AQRYWHCD, D_BZ_XB, D_BZ_AQRYZZMM }" @close="close" :title="editObj.title"
      :examineshow="examineshow" :type="syType" />
  </div>
  <div class="dialog-box" v-if="SYInfoShow">
    <addjsysy :saveBtnShow="saveBtnShow" :editObj="editObj" :showklc='showklc || isLeaderOrAqbm'
      :dict="{ D_TB_DRIVELEVEL, D_BZ_AQRYWHCD, D_BZ_XB, D_BZ_AQRYZZMM }" @close="close3" :title="editObj.title"
      :examineshow="examineshow" :type="syType" />
  </div>
  <!-- 查看审核状态 -->
  <div v-if="showShzt" class="dialog-box">
    <Zbapxq :editObj="editObj" @close="close2" :title="editObj.title"
      :dict="{ D_TB_DRIVELEVEL, D_BZ_AQRYWHCD, D_BZ_XB, D_BZ_AQRYZZMM }" />
  </div>
</template>

<script setup>
// 引入
import SyjlTable from "@/views/yxkzgl/jsygl/components/SyjlTable.vue"
import SbTable from "@/views/yxkzgl/jsygl/components/SbTable.vue"
import SyTable from "@/views/yxkzgl/jsygl/components/SyTable.vue"
import { filterYear, filterqx } from "@/utils/auth.js";
import { reactive, onMounted, ref, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
import Zbapxq from "@/views/yxkzgl/jsygl/zbapxq.vue";
import Sgzsbxq from "@/views/yxkzgl/jsygl/Sgzsbxq.vue";
import addjsysy from "@/views/yxkzgl/jsygl/addjsysy.vue";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import * as MOSTY from "@/components/MyComponents/index";
import { getItem } from "@/utils/storage";
const { proxy } = getCurrentInstance();
// 数据
const { D_TB_DRIVELEVEL, D_BZ_AQRYWHCD, D_BZ_XB, D_BZ_AQRYZZMM } = proxy.$dict(
  "D_TB_DRIVELEVEL", "D_BZ_AQRYWHCD", "D_BZ_XB", "D_BZ_AQRYZZMM");
const syjlTable = ref()
const searchArr = reactive([
  {
    showType: "input",
    prop: "drivername",
    width: '250px',
    placeholder: "请输入驾驶员名称进行查询"
  },
  // {
  //   showType: "select",
  //   prop: "type",
  //   width: '250px',
  //   options: [{ label: '上岗证 ', value: '1' }, { label: '准驾证 ', value: '2' }],
  //   placeholder: "请选择证件类型进行查询"
  // },
]);

/** 是否安全部门 或者领 导班子 */
const isLeaderOrAqbm = filterqx('isaqbm') == '1' || filterqx('isldbz') == '1'
const syTable = ref()
const sbTable = ref()
const comprehensiveShow = ref(false);
const SYInfoShow = ref(false);
const showShzt = ref(false);
const loadings = ref(false);
const showsb = ref(false);
const showklc = ref(false);
const activeName = ref("sblb")
const syType = ref('')
const orgLevel = ref(JSON.parse(
  window.localStorage.getItem("deptId")
).orgLevel.substring(0, 1))
const citycode = ref(getItem("deptId").citycode)
const orgBizType = ref(getItem("deptId").orgBizType);
const examineshow = ref(false);
const oggid = ref('')
const editObj = reactive({
  title: "",
  savebtn: true,
  formData: {},
  tableTabls: [{ name: "申办列表", value: "sblb" }, { name: "审验列表", value: "sylb" }, { name: "审验记录列表", value: "syjllb" }]
});
// 操作参数
const listQuery = reactive({
  drivername: ""
});
const syTableParams = ref();//审验列表参数
onMounted(() => {
  // getIscgy()
  //	查询 是否为车管员（车管员才有操作权限）
  getApi({}, '/mosty-jcgl/sprole/judgeSprole').then(res => {
    if (res) {
      showklc.value = res;
    }
  })
  // // 根据citycode来判断是否为这四类公司(贵安新区、省局公司、进出口、烟科院)，是的话，安全员也可以有操作权限
  // if ((['529010529001', '5277520157', '52191520115'].includes(citycode.value) || ['1'].includes(orgLevel.value)) && ['04', '52'].includes(orgBizType.value)) {
  //   showklc.value = true
  // } else {
  //   showklc.value = false
  // }
});
const searchFn = () => { }
//表格导出
const onClickDownload = () => {
  loadings.value = true
  switch (activeName.value) {
    case "sylb":
      syTable.value.export2Excel()
      loadings.value = false
      break
    case "sblb":
      sbTable.value.export2Excel()
      loadings.value = false
      break
    case "syjllb":
      syjlTable.value.export2Excel()
      loadings.value = false
      break
  }
}
// v-if="activeName != 'sblb'"
//表格类型选择
const onTabsChange = (val) => {
  // 如果当前 Tab 是 sylb 或 syjllb，就添加年份项
  if (val.name === 'sylb' || val.name === 'syjllb') {
    // 判断是否已存在 year 字段，避免重复添加
    const hasYear = searchArr.some(item => item.prop === 'year')
    if (!hasYear) {
      searchArr.push({
        showType: "select",
        prop: "year",
        width: '250px',
        options: filterYear(7, true),
        placeholder: "请选择年份"
      })
    }
  } else {
    // 其他 tab，移除 year 字段
    const last = searchArr.at(-1)
    if (last && last.prop === 'year') {
      searchArr.pop()
    }
  }

  // 更新查询参数
  syTableParams.value.orgcode = oggid.value
  listQuery.orgcode = oggid.value
  listQuery.year = ""
  listQuery.drivername = ""
  listQuery.type = ''
  activeName.value = val.name
}

//	查询 是否为车管员
function getIscgy() {
  getApi({}, '/mosty-jcgl/sprole/judgeSprole').then(res => {
    if (res) {
      showsb.value = res;
    }
  })
}
//获取部门数据
const getdeptId = (val) => {
  oggid.value = val.id
  syTableParams.value = { ...syTableParams.value, orgcode: val.id }
};


//查询
function addUserHander(obj) {
  listQuery.year = obj.year ? obj.year : '';
  listQuery.drivername = obj.drivername;
  listQuery.type = obj.type;
  syTableParams.value = { ...syTableParams.value, ...listQuery }
}

//弹窗关闭
const close = () => {
  comprehensiveShow.value = false;
  examineshow.value = false;
  syTableParams.value = { ...syTableParams.value, ...listQuery }
};
const close3 = () => {
  SYInfoShow.value = false
  examineshow.value = false;
  syTableParams.value = { ...syTableParams.value, ...listQuery }
  activeName.value = 'sylb'
}
const close2 = () => {
  showShzt.value = false;
};
//表格操作
const onClickOperation = (val) => {
  editObj.rowId = val.id
  comprehensiveShow.value = false;
  saveBtnShow.value = false;
  showShzt.value = false;
  syType.value = activeName.value == 'sylb' ? 'sy' : ''

  switch (val.type) {
    case "audit":
      editObj.title = "驾驶人上岗证查看";
      showShzt.value = true;
      break
    case "detail":
      editObj.title = "驾驶员上岗证详情";
      comprehensiveShow.value = true;
      break
    case "anewSubmit":
      saveBtnShow.value = true;
      editObj.title = "驾驶员上岗证修改";
      comprehensiveShow.value = true;
      break
    case "delete":
      handledel(val.id)
      break
  }
}
const saveBtnShow = ref(true);
//新增
function addInfo() {
  editObj.rowId = null
  saveBtnShow.value = true;
  editObj.title = "驾驶员上岗证申请";
  comprehensiveShow.value = true;
  syType.value = ''
}
//新增审验
function addSyinfo() {
  editObj.rowId = null
  // 展示
  SYInfoShow.value = true;
  saveBtnShow.value = true;
  editObj.title = "驾驶员上岗证新增";
  syType.value = 'sy'
}
//删除
function handledel(id) {
  postApi({}, `/mosty-jcgl/driverposts/deleteDriverpostsOne/${id}`).then((res) => {
    ElMessage.success("删除成功");
  });
}


</script>

<style lang="scss" scoped>
.button {
  display: flex;
  align-items: center;
  justify-content: end;
  margin-bottom: 12px;
}
</style>
