<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 搜索表单 -->
      <el-form :model="queryForm" inline ref="searchFormRef">
        <el-form-item label="招生计划" prop="groupId">
          <el-select v-model="queryForm.groupId" filterable clearable placeholder="请选择招生计划">
            <el-option
              v-for="item in planList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getDataList">查询</el-button>
          <el-button plain icon="Refresh" class="ml10" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮 -->
      <div class="mb15">
        <el-button
          v-if="hasAuth('recruit_recruitstudentplancorrectscoreconfig_add')"
          type="primary"
          icon="FolderAdd"
          @click="addOrUpdateHandle"
        >
          新 增
        </el-button>
        <el-button
            v-auth="'recruit_areascore_import'"
            type="primary"
            plain
            icon="UploadFilled"
            :loading="exportLoading"
            @click="handleImportDialog"
        >导入信息
        </el-button>
      </div>

      <!-- 表格 -->
      <el-table
        ref="tableRef"
        :data="state.dataList"
        v-loading="state.loading"
        border
        stripe
        row-key="id"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="groupId" label="招生计划名称" align="center" show-overflow-tooltip>
          <template #default="scope">
            {{ getPlanName(scope.row.groupId) }}
          </template>
        </el-table-column>
        <el-table-column prop="regionName" label="地区" align="center" show-overflow-tooltip />
        <el-table-column prop="fullScore" label="分数线" align="center" show-overflow-tooltip />
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button
              v-if="hasAuth('recruit_recruitstudentplancorrectscoreconfig_edit')"
              type="primary"
              link
              icon="EditPen"
              @click="addOrUpdateHandle(scope.row.id)"
            >
              修改
            </el-button>
            <el-button
              v-if="hasAuth('recruit_recruitstudentplancorrectscoreconfig_del')"
              type="danger"
              link
              icon="Delete"
              @click="deleteHandle(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-bind="state.pagination"
        @current-change="currentChangeHandle"
        @size-change="sizeChangeHandle"
      />

      <!-- 弹窗, 新增 / 修改 -->
      <table-form ref="addOrUpdateRef" @refreshDataList="getDataList" />

      <import-recruit-info ref="ImportRecruitInfoRef"  @refreshDataList="getDataList"></import-recruit-info>

    </div>
  </div>
</template>

<script setup lang="ts" name="recruitstudentplancorrectscoreconfig">
import { ref, reactive, onMounted, nextTick, defineAsyncComponent } from 'vue'
import { useAuth } from '/@/hooks/auth'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { useMessage, useMessageBox } from '/@/hooks/message'
import { getList } from '/@/api/recruit/recruitstudentplangroup'
import { fetchList, delObj } from '/@/api/recruit/recruitstudentplancorrectscoreconfig'
const ImportRecruitInfo = defineAsyncComponent(() => import('/@/views/recruit/common/import-recruit-info.vue'));
const ImportRecruitInfoRef=ref<any>();

const TableForm = defineAsyncComponent(() => import('./detaiform.vue'))
const { hasAuth } = useAuth()
// 消息提示 hooks
const message = useMessage()
const messageBox = useMessageBox()

// 表格引用
const tableRef = ref()
const searchFormRef = ref()
const addOrUpdateRef = ref()

// 数据
const planList = ref<any[]>([])

// 查询表单
const queryForm = reactive({
  groupId: ''
})

// 获取计划名称
const getPlanName = (groupId: string) => {
  const item = planList.value.find(item => item.id === groupId)
  return item ? item.groupName : ''
}

// 表格状态
const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: queryForm,
  pageList: async (params: any) => {
    const response = await fetchList(params)
    return {
      data: {
        records: response.data.records,
        total: response.data.total
      }
    }
  },
  createdIsNeed: false
})

// 使用 table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, tableStyle } = useTable(state)

// 初始化
const init = async () => {
  try {
    const data = await getList()
    planList.value = data.data || []
    if (planList.value.length > 0) {
      queryForm.groupId = planList.value[0].id
    }
    getDataList()
  } catch (error) {
    // console.log(error)
  }
}

// 新增 / 修改
const addOrUpdateHandle = (payload?: string | MouseEvent) => {
  // 新增按钮未传参时会传入 MouseEvent，这里统一转换为 id 或 null
  const id = typeof payload === 'string' ? payload : null
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

// 删除
const deleteHandle = async (id: string) => {
  try {
    await messageBox.confirm('是否确认删除本条数据？请谨慎操作')
    await delObj(id)
    message.success('删除成功')
    getDataList()
  } catch {
    // 用户取消
  }
}

// 重置查询
const resetQuery = () => {
  searchFormRef.value?.resetFields()
  queryForm.groupId = ''
  if (planList.value.length > 0) {
    queryForm.groupId = planList.value[0].id
  }
  getDataList()
}

const exportLoading = ref(false);

const handleImportDialog = () => {
  ImportRecruitInfoRef.value?.init("R10002");
};

onMounted(() => {
  init()
})
</script>

<style lang="scss" scoped>
</style>
