<template>
  <section class="eltab3">
    <MOSTY.Table :table-columns="pageData.tableColumns" :tableData="pageData.tableData" :tableloading="tableloading"
      :tableHeight="'100%'" :isSelect="false" style="width: 100%;" :isSort="false" :actionsW="350">
      <template #sex="{ row }">
        <dict-tag v-if="row.sex" :options="props.dict.D_BZ_XB" :value="row.sex" :tag="false" />
        <span v-else>无</span>
      </template>
      <template #carstyle="{ row }">
        <dict-tag v-if="row.carstyle" :options="props.dict.D_TB_DRIVELEVEL" :value="row.carstyle" :tag="false" />
        <span v-else>无</span>
      </template>
      <template #thebidtype="{ row }">
        {{ row.thebidtype == "1" ? "上岗证" : "准驾证" }}
      </template>
      <template #actions="{ row }">
        <span class="operedit" @click="handleSee(row)"><el-icon>
            <EditPen />
          </el-icon>查看审核状态</span>
        <span class="operesee" @click="handleedit(row)"><el-icon>
            <View />
          </el-icon>查看信息</span>
       <span class="operedown" v-if="row.state == '8'" @click="handleCkxx(row)"><el-icon>
            <RefreshLeft />
          </el-icon>重新提交</span>
       <el-popconfirm v-if="row.state == '8'" confirm-button-text="是" cancel-button-text="否" icon-color="red"
          title="确定要删除吗?" @confirm="handledel(row, 'see')">
          <template #reference>
            <span class="operdel">
              删除
            </span>
          </template>
        </el-popconfirm>
      </template>
    </MOSTY.Table>
    <div class="fenye">
      <MOSTY.Pages :pageConfiger="pageData.pageConfiger" @changeNo="handleCurrentChange"
        @changeSize="handleSizeChange" />
    </div>
  </section>

</template>

<script setup>
import { postApi } from "@/api/tobAcco_api.js";
import { onMounted, ref, reactive, defineProps, defineEmits, watch, defineExpose, getCurrentInstance } from "vue";
import { exportXlsx } from "@/excel/Export2Excel.js";
import * as MOSTY from "@/components/MyComponents/index";
const { proxy } = getCurrentInstance()
const tableloading = ref(false);
const props = defineProps({
  dict: {
    type: Object,
    default: { D_BZ_XB: [], D_BZ_AQRYWHCD: [], D_TB_DRIVELEVEL: [] }
  },
  params: Object,//查询参数
})
const emits = defineEmits([""])
// 分页
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 10,
  time: 1
});
const pageData = reactive({
  tableColumns: [
    {
      title: "姓名",
      prop: "drivername"
    },
    {
      title: "性别",
      prop: "sex",
      slot: true
    },
    {
      title: "单位/部门",
      prop: "deptname"
    },
    {
      title: "驾驶证申领日期",
      prop: "driverGetTime"
    },
    {
      title: "准驾车型",
      prop: "carstyle",
      slot: true
    },
    {
      title: "证件类型",
      prop: "thebidtype",
      slot: true
    },
    // {
    //   title: "上次上岗证领证时间",
    //   prop: "lastpoststime"
    // },
    {
      title: "到期时间",
      prop: "datedue"
    },
    {
      title: "操作",
      prop: "actions",
      slot: true
    }
  ],
  pageConfiger: {
    pageSize: 10,
    pageCurrent: 1,
    total: 0
  },
  tableData: []
})
//审核状态
const handleSee = (row) => {
  emits("clickBut", { id: row.id, type: "audit" })
  getListData()
}
//查看
const handleedit = (row) => {
  emits("clickBut", { id: row.id, type: "detail" })
}
//重新提交
const handleCkxx = (row) => {
  emits("clickBut", { id: row.id, type: "anewSubmit" })
}
//删除
const handledel = (row) => {
  emits("clickBut", { id: row.id, type: "delete" })
  getListData()
}

// 导出表格
const export2Excel = () => {
  let title = {
    drivername: "姓名",
    sex: "性别",
    brithday: "出生年月",
    education: "文化程度",
    driverhistory: "驾驶工龄(年)",
    drivernumber: "证件号码",
    carstyle: "准驾车型",
    thebidtype: "证件类型",
    dqstate: "当前状态"
  };
  listQuery.value.pageCurrent = 1
  listQuery.value.pageSize = 10000
  postApi(listQuery.value, "/mosty-jcgl/driverposts/queryDriverpostsList").then((res) => {
    if (res.records) {
      let data = res.records;
      if (data.length > 0) {
        for (let i = 0; i < data.length; i++) {
          data[i].sex = data[i].sex ? proxy.$getDictVal(data[i].sex, { value: props.dict.D_BZ_XB }) : '无';
          data[i].carstyle = data[i].carstyle ? proxy.$getDictVal(data[i].carstyle, { value: props.dict.D_TB_DRIVELEVEL }) : '无';
          data[i].education = data[i].education ? proxy.$getDictVal(data[i].education, { value: props.dict.D_BZ_AQRYWHCD }) : '无';
          data[i].thebidtype = data[i].thebidtype == '1' ? '上岗证' : '准驾证';
          filterTable(data[i])
        }
        let tablename = "上岗证申办列表.xlsx";
        exportXlsx(title, data, '上岗证申办列表', tablename);
      } else {
        proxy.$message.warning('暂无数据');
      }
    }
  });
};

//列表
const getListData = () => {
  tableloading.value = true;
  postApi(listQuery.value, `/mosty-jcgl/driverposts/queryDriverpostsList`).then(
    (res) => {
      if (res.records) {
        tableloading.value = false;
        pageData.tableData = res.records;
        pageData.tableData.forEach(item => {
          filterTable(item)
        })
        pageData.pageConfiger.total = res.total
      } else {
        tableData.value = [];
        tableloading.value = false;
      }
    }
  );
}
const filterTable = (item) => {
  // 审批状态(1待部门车队队长审批2待单位安全部门负责人审批3待单位分管领导审批
  //  4待市公司交安员审批 5市公司安全管理科科长审批 6 完成 7失败 8市公司分管审批)
  if (item.state == '1') {
    item.dqstate = '待县车管部门负责人审批'
  } else if (item.state == '2') {
    item.dqstate = '待市车管员审批'
  } else if (item.state == '3') {
    item.dqstate = '待物流车管部门负责人审批'
  } else if (item.state == '4') {
    item.dqstate = '待省/市车管部门负责人审批'
  } else if (item.state == '5') {
    item.dqstate = '待省/市交通管理员审批'
  } else if (item.state == '6') {
    item.dqstate = '待省/市车管部门负责人审批'
  } else if (item.state == '7') {
    item.dqstate = '完成'
  } if (item.state == '8') {
    if (item.unitpartspstate == '2') {
      item.dqstate = '县车管部门负责人审批不通过'
    } else if (item.fgldstate == '2') {
      item.dqstate = '市车管员审批不通过'
    } else if (item.unitsecurityspstate == '2') {
      item.dqstate = '物流车管部门负责人审批不通过'
    } else if (item.unitspheadspsate == '2') {
      item.dqstate = '省/市车管部门负责人审批不通过'
    } else if (item.carcontrollerstate == '2') {
      item.dqstate = '省/市交通管理员审批不通过'
    } else if (item.tokzstate == '2') {
      item.dqstate = '省/市车管部门负责人审批不通过'
    }
  }
}
//分页
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  pageData.pageConfiger.pageSize = currentSize;
  getListData();
};
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  pageData.pageConfiger.pageCurrent = currentPage;
  getListData();
};
watch(() => props.params, val => {
  if (val) {
    listQuery.value.pageCurrent = 1
    listQuery.value = { ...listQuery.value, ...val }
    getListData()
  }
})
onMounted(() => {
  if (props.params && props.params.orgcode) {
    listQuery.value.orgcode = props.params.orgcode
    getListData()
  }

})
defineExpose({
  export2Excel
})
</script>

<style lang="scss" scoped>
.eltab3 {
  height: calc(100vh - 430px);
}
</style>
