<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="openSearch('create')"
          v-hasPermi="['system:sms-channel:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 添加学生</el-button
        >
        <el-button
          v-if="showAssignButton"
          type="primary"
          plain
          @click="openEdit('create')"
          v-hasPermi="['system:sms-channel:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 分配场次</el-button
        >
        <el-button
  type="danger"
  class="ele-btn-del"
  :disabled="!selections.length"
  @click="handleDeletes()"
>
  批量删除
</el-button>
      </el-form-item>
    </el-form> 
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="用户名称" align="center" prop="username" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="班级名称" align="center" prop="className"/>
      <el-table-column label="分配考场" align="center" prop="batch"/>

      <!-- <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.schemeId)"
            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>




  <!-- 编辑弹窗 -->
  <person-edit v-model="showEdit"  :task-Id="props.taskId" @done="reload" />
  <PersonSearch  :task-id="props.taskId" v-model="showPersonEdit"   ref="stuAddRef"   @done="reload" />
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

import * as SmsPersonlApi from '@/api/system/person';
import PersonEdit from './components/person-edit.vue';
import PersonSearch from './components/person-serach.vue'
import * as SmsChannelApi from '@/api/system/session';
import { fa } from 'element-plus/es/locale';

defineOptions({ name: 'SystemSmsChannel' })
const showAssignButton = ref(true) // 控制按钮显示
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const props = defineProps({
  taskSpecialty: {
    type: String,
    default: ''
  },
  taskId: {
    type: String,
    default: ''
  },
  formData:{
    type: Object,
    default: null
  }
})
const showPersonEdit = ref(false)
  /** 当前编辑数据 */
  const current = ref<object>();

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

  const stuAddRef = ref();

  

const taskEditRef = 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: [],
  taskId: props.taskId 
})
/** 查询列表 */
const getList = async () => {
  loading.value = true

  try {
    const resButton = await SmsChannelApi.fetchNoMes(props.taskId)

      if (resButton==='200') {
        showAssignButton.value = true
      } else {
        showAssignButton.value = false
      }



    const res = await SmsPersonlApi.pagePersons(queryParams)
    console.log(res)
    list.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const reload = () => {
  getList()
}
  /** 表格选中数据 */
  const selections = ref([]);
const handleSelectionChange = (rows) => {
  selections.value = rows;
}


const selectedRows = ref<string[]>([]);

const handleDeletes = async () => {
  try {
  
  const rows = selections.value;
  if (!rows.length) {
    message.error('请至少选择一条数据');
    return;
  }
    await ElMessageBox.confirm(
      '只能删除待考状态的学生,是否确认删除？',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    );
  selectedRows.value = rows.map((d: any) => d.id); // 保存选中的行数据

  const deleteData = {
      studentIds: selectedRows.value,  // 选中的 ID 列表
      taskId: props.taskId  // 任务 ID
    }
  const res=  await SmsPersonlApi.removePersons(deleteData)
if(res =='删除成功'){
    message.success(t('common.delSuccess'))

}else{
    message.error(res)
  
}
    // 刷新列表
    await getList()
  } catch {}
}


const openEdit = (type: string, row?: object) => {
  showEdit.value = true
  current.value = row
  nextTick(() => {
    taskEditRef.value?.open(type, row)
  })
}
const openSearch = (type: string, row?: object) => {
  showPersonEdit.value = true
  current.value = row
  nextTick(() => {
    stuAddRef.value?.open(type, row)
  })
}



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

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

</script>
