<template>

  <ContentWrap>
     <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
     
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:sms-channel:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增</el-button
        >
      </el-form-item>
    </el-form> 
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="试卷编号" align="center" prop="paperId" />
      <el-table-column label="使用次数" align="center" prop="counts" />
      <el-table-column label="抽卷方式" align="center" prop="rollUp">
        <template #default="scope">
        <span v-if="scope.row.quLevel === '0'">固定</span>
        <span v-else-if="scope.row.quLevel === '1'">AB卷</span>
        <span v-else-if="scope.row.quLevel === '2'">随机</span>
        <span v-else-if="scope.row.quLevel === '3'">自选</span>
        <span v-else>未知</span>
      </template>
      </el-table-column>
      <el-table-column label="AB卷" align="center" prop="isAb">
        <template #default="scope">
        <span v-if="scope.row.quLevel === '0'">A卷</span>
        <span v-else-if="scope.row.quLevel === '1'">B卷</span>
        <span v-else>未知</span>
      </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="status" >
      <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYS_YES_NO" :value="scope.row.status" />
        </template>
    </el-table-column>
      
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openEdit('update', scope.row)"
            v-hasPermi="['system:sms-channel:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:sms-channel:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>




<paper-add  v-model="showAdd"  :task-Id="taskId" :task-specialty="taskSpecialty"   />
    <paper-edit v-model="showEdit" :data="current" />
    <!-- <paper-look v-model="showLook" :paper-id="paperId"  /> -->
    <paper-set  v-model="showSet"  :task-Id="taskId"  />
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

import * as SmsChannelApi from '@/api/system/paper';
import PaperEdit from './components/step-edit.vue';
  import PaperAdd from './components/step-add.vue';
  import PaperLook from './components/step-look.vue';
  import PaperSet from './components/step-set.vue';
  import PaperSearch from './components/step-search.vue';
  import { pagePapers, removePapers, exportPapers } from '@/api/system/paper';



defineOptions({ name: 'SystemPaper' });
  const props = defineProps({
    taskSpecialty: {
    type: String,
    default: ''
  },
  taskId: {
    type: String,
    default: ''
  }
})

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗


  /** 当前编辑数据 */
  const current = ref<object>();

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  const showLook = ref(false);

  const showAdd = ref(false);

  const showSet = ref(false);
  

const smsChannelFormRef = ref()
const taskEditRef = ref()
const taskAddRef = ref()
const taskTempRef = ref()



const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  signature: undefined,
  status: undefined,
  createTime: []
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const res = await SmsChannelApi.pagePapers(queryParams)
    console.log(res)
    list.value = res
    total.value = res.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref();
const openForm = (type: string, id?: number) => {
  showAdd.value = true
  taskAddRef.value?.open(type, id)
}

const openEdit = (type: string, row?: object) => {
  showEdit.value = true
  current.value = row
  console.log( current.value )
  taskEditRef.value?.open(type, row)
}



/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SmsChannelApi.removePaper(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
