<template>
  <div>
    <div class="titleBox">
      <PageTitle title="保安题库">
        <el-button type="primary" @click="addEdit('add', null)">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
      </PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch" :key="pageData.keyCount" />
    </div>
    <!-- 表格 -->
    <div class="tabBox">
      <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData">
        <template #type="{ row }">
          <DictTag :value="row.type" :tag="false" :options="D_BAXX_KTLX" />
        </template>
        <template #answer="{ row }">
          <span v-if="row.type == '01' || row.type == '02'">
            <span v-for="val in row.correctAnswer" class="flex items-center just-center"> 
              <DictTag :value="val" :tag="false" :options="D_BA_TKDA" />、
            </span>
          </span>
          <span v-else>{{ row.isTrue }}</span>
        </template>
        <!-- 操作 -->
        <template #controls="{ row }">
          <el-link type="primary" link @click="addEdit('edit', row)">编辑</el-link>
          <el-link type="primary" link @click="addEdit('detail', row)">详情</el-link>
          <el-link type="danger" link @click="handleDelete([row.id])">删除</el-link>
        </template>
      </MyTable>
      <Pages @changeNo="changeNo" @changeSize="changeSize" :tableHeight="pageData.tableHeight" :pageConfiger="{
        ...pageData.pageConfiger,
        total: pageData.total
      }"></Pages>
    </div>
    <!-- 详情 -->
    <DetailForm ref="detailDiloag" :dic="{D_BAXX_KTLX,D_BA_TKDA,D_BA_TKDA_PDT}" @refresh="getList" />
  </div>
</template>

<script setup>
import PageTitle from "@/components/aboutTable/PageTitle.vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import DetailForm from "./components/detailForm.vue";
import { qcckPost } from "@/api/qcckApi.js";
import { reactive, ref, onMounted, getCurrentInstance, nextTick } from "vue";
const { proxy } = getCurrentInstance();
const { D_BAXX_KTLX,D_BA_TKDA,D_BA_TKDA_PDT } = proxy.$dict("D_BAXX_KTLX",'D_BA_TKDA','D_BA_TKDA_PDT');

const detailDiloag = ref();
const searchBox = ref(); //搜索框
const searchConfiger = ref([
  {
    label: "题目",
    prop: "tm",
    placeholder: "请输入题目",
    showType: "input"
  },
]);

const queryFrom = ref({});
const pageData = reactive({
  tableData: [],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "null",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  },
  controlsWidth: 180,
  tableColumn: [
    { label: "题型", prop: "type", showSolt: true },
    { label: "题目", prop: "tm" },
    { label: "选项A", prop: "optionA" },
    { label: "选项B", prop: "optionB" },
    { label: "选项C", prop: "optionC" },
    { label: "选项D", prop: "optionD" },
    { label: "选项E", prop: "optionE" },
    { label: "答案", prop: "answer", showSolt: true}
  ]
});

onMounted(() => {
  getList();
  tabHeightFn();
});

// 搜索
const onSearch = (val) => {
  queryFrom.value = { ...val };
  pageData.pageConfiger.pageCurrent = 1;
  getList();
};

const changeNo = (val) => {
  pageData.pageConfiger.pageNum = val;
  getList();
};
const changeSize = (val) => {
  pageData.pageConfiger.pageSize = val;
  getList();
};

// 获取列表
const getList = () => {
  pageData.tableConfiger.loading = true;
  let data = { ...pageData.pageConfiger, ...queryFrom.value };
  qcckPost(data, "/mosty-base/baxx/tkgl/page").then((res) => {
    let arr = res.records || []
    arr.forEach(item => {
      item.correctAnswer = item.correctAnswer.split(',')
    });
    pageData.tableData = arr;
    pageData.total = res.total;
    pageData.tableConfiger.loading = false;
  }).catch(() => {
    pageData.tableConfiger.loading = false;
  });
};


// 删除
const handleDelete = (ids) => {
  proxy.$modal.confirm("是否确认删除该题目？").then(() => {
    qcckPost(ids, "/mosty-base/baxx/tkgl/remove").then(() => {
      proxy.$modal.msgSuccess("删除成功");
      getList();
    });
  });
};

// 详情
const addEdit = (type, row) => {
  nextTick(() => {
    detailDiloag.value.init(type, row);
  })
};


// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight = window.innerHeight - searchBox.value.offsetHeight - 250;
  window.onresize = function () {
    tabHeightFn();
  };
};
</script>

<style>
.el-loading-mask {
  background: rgba(0, 0, 0, 0.5) !important;
}
</style>
