<template>
  <el-dialog title="试卷审核列表" v-model="dialogVisible" width="1300px" :before-close="CloseEvent"
    :close-on-click-modal="false">
    <div>
      <div class="flex just-end">
        <el-button type="primary" @click="doPass(true)" :loading="loading">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>全部通过</span>
        </el-button>
        <el-button type="primary" @click="doPass(false)" :loading="loading">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>全部不通过</span>
        </el-button>
      </div>
      <el-table :data="tableData" border style="width: 100%;" height="calc(85vh - 150px - 100px)">

        <el-table-column type="index" label="序号" width="65" />
        <el-table-column prop="name" label="试卷名称" />
        <!-- <el-table-column prop="count" label="题目数量" width="120" /> -->
        <el-table-column prop="type" label="试卷类型">
          <template #default="{ row }">
            <div class="flex just-center">
              <dict-tag v-for="(item, index) in row.typeArr" :key="index" :options="D_BZ_KTKYWLX" :value="item"
                :tag="true" style='display:flex;flex-wrap:wrap;' />
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="pzlx" label="配置类型">
          <template #default="{ row }">
            {{ row.pzlx == '0' ? '随机生成' : '手动配置' }}
          </template>
        </el-table-column>


        <el-table-column prop="checked" label="是否通过" width="180">
          <template #default="{ row }">
            <div>
              <el-radio-group v-model="row.isPass">
                <el-radio label="1">通过</el-radio>
                <el-radio label="0">不通过</el-radio>
              </el-radio-group>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="checked" label="操作" width="100">
          <template #default="{ row }">
            <span style="margin-left: 0;" class="operesee" @click="goDetail(row)"><el-icon>
                <EditPen />
              </el-icon>
              详情
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt15 mb15">
        <span>审核意见：</span>
        <el-input v-model="reviewerInfo" style="width: calc(100% - 5.5em)"></el-input>
      </div>

      <!-- 考题信息弹框 -->
      <el-dialog title="考题信息" v-model="isShowExamlibDialog" width="1200px">
        <div v-if="isShowExamlibDialog">
          <ExamlibList @close="isShowExamlibDialog = false" :Row="currRow" :zdlist='zdlist' />
        </div>
      </el-dialog>

    </div>
    <template #footer>
      <div class="flex just-center mt10">

        <el-button type="primary" @click="doAudit('set')" :loading="loading">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>审核</span>
        </el-button>

        <el-button type="primary" @click="CloseEvent">
          <el-icon>
            <DocumentDelete />
          </el-icon>
          <span>关闭</span>
        </el-button>
      </div>

    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineProps, onBeforeMount, defineEmits, computed, getCurrentInstance } from 'vue';
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus"
import ExamlibList from '../examlibList.vue';

const { proxy } = getCurrentInstance();
const { D_TB_YWLX, D_BZ_KTKYWLX, D_TB_KTTX, D_BZ_SF, D_BZ_PXSSCTFS, D_BZ_PXZYK } = proxy.$dict("D_TB_YWLX", "D_BZ_KTKYWLX", "D_TB_KTTX", "D_BZ_SF", "D_BZ_PXSSCTFS", "D_BZ_PXZYK");



const emits = defineEmits(['close']);
const props = defineProps({
  /** 回传的数据 */
  resData: {
    type: Array,
    default: []
  },
  dictObj: {
    type: Object,
    default: () => ({})
  }
});

const currRow = ref({})
const zdlist = ref({ D_TB_YWLX, D_BZ_KTKYWLX, D_TB_KTTX, D_BZ_SF, D_BZ_PXSSCTFS, D_BZ_PXZYK })

const dialogVisible = ref(true);
const isShowExpand = ref(false);// 先不展示，因为点击radio会自动关闭
const loading = ref(false)
const isShowExamlibDialog = ref(false)
/** 表格数据 */
const tableData = ref([]);
const reviewerInfo = ref('')
const CloseEvent = () => {
  emits('close')
};

/** 获取需要的判断题答案(AB 转TF)
 * @param {String} answer 答案
 */
function getNeedPdAnswer(answer) {
  if (answer === 'T') return "A"
  if (answer === 'F') return "B"
  return answer
}
/** 全通过，全拒绝 */
const doPass = (isPass) => {
  tableData.value = tableData.value.map(item => {
    return {
      ...item,
      isPass: isPass ? '1' : '0'
    }
  })
};
/**
 * 审核
 * @param type 'all' 全部通过  allnot:都不通过，set，根据设置
 */
const doAudit = (type) => {
  loading.value = true
  const items = tableData.value.map(row => {
    return {
      id: row.id,
      status: row.isPass == '0' ? '05' : row.status // 拒绝05 通过传原值
    }
  })

  let params = {
    items: items,
    rwid: props.resData?.id,
    reviewerInfo: reviewerInfo.value,
  }
  postApi(params, `/mosty-jcgl/exampaper/examineExaminationInfo`).then(() => {
    ElMessage.success("审核完成");
    // getTableData()
    CloseEvent()
  }).finally(() => {
    loading.value = false
  })
}
/** 获取列表 */
const getTableData = () => {
  getApi({ rwId: props.resData?.rwid }, '/mosty-jcgl/exampaper/getRwId').then(res => {

    tableData.value = Array.isArray(res) ? res : []
    tableData.value = tableData.value.map(item => {
      return {
        ...item,
        typeArr: item.type ? item.type.split(',') : [],
        isPass: '0' // 默认不通过
      }
    })
  })
}
const goDetail = (row={}) => {
  currRow.value = {
    id: row?.id,
    name: row.name,
    sjRow: { ...row }
  }
  isShowExamlibDialog.value = true
}
onBeforeMount(() => {
  getTableData()
});
</script>

<style scoped lang="scss">
.el-dialog {
  .el-table {
    margin-top: 20px;
  }
}
</style>
