<!--
  -    Copyright (c) 2018-2025, cyweb All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  -
  -->

<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 搜索表单 -->
      <el-form :model="queryForm" inline class="mb-4">
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getDataList">查询</el-button>
          <el-button type="primary" plain icon="Refresh" class="ml10" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item v-auth="'recruit_recruitstudentplandegreeofeducation_add'">
          <el-button type="primary" icon="FolderAdd" class="ml10" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table
        ref="tableRef"
        :data="state.dataList"
        v-loading="state.loading"
        border
        stripe
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="planId" label="计划明细ID" align="center" show-overflow-tooltip />
        <el-table-column prop="degreeOfEducation" label="生源" align="center" show-overflow-tooltip />
        <el-table-column prop="remarks" label="备注信息" align="center" show-overflow-tooltip />
        <el-table-column prop="createBy" label="创建者" align="center" show-overflow-tooltip />
        <el-table-column prop="createDate" label="创建时间" align="center" show-overflow-tooltip />
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button
              v-auth="'recruit_recruitstudentplandegreeofeducation_edit'"
              type="primary"
              link
              icon="EditPen"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-auth="'recruit_recruitstudentplandegreeofeducation_del'"
              type="danger"
              link
              icon="Delete"
              @click="handleDel(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

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

      <!-- 新增/编辑弹窗 -->
      <el-dialog
        v-model="dialogVisible"
        :title="form.id ? '编辑' : '新增'"
        width="600px"
        :close-on-click-modal="false"
        destroy-on-close
      >
        <el-form
          ref="formRef"
          :model="form"
          :rules="formRules"
          label-width="120px"
        >
          <el-form-item label="计划明细ID" prop="planId">
            <el-input v-model="form.planId" placeholder="请输入计划明细ID" clearable />
          </el-form-item>
          <el-form-item label="生源" prop="degreeOfEducation">
            <el-input v-model="form.degreeOfEducation" placeholder="请输入生源" clearable />
          </el-form-item>
          <el-form-item label="备注信息" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" :rows="3" placeholder="请输入备注信息" clearable />
          </el-form-item>
        </el-form>
        
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="submitLoading">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts" name="recruitstudentplandegreeofeducation">
import { ref, reactive } from 'vue'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { useMessage, useMessageBox } from '/@/hooks/message'
import { addObj, delObj, fetchList, putObj } from '/@/api/recruit/recruitstudentplandegreeofeducation'

// 消息提示 hooks
const message = useMessage()
const messageBox = useMessageBox()

// 表格引用
const tableRef = ref()
const formRef = ref()

// 弹窗状态
const dialogVisible = ref(false)
const submitLoading = ref(false)

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

// 表单数据
const form = reactive({
  id: '',
  planId: '',
  degreeOfEducation: '',
  remarks: ''
})

// 表单验证规则
const formRules = {}

// 表格状态
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
      }
    }
  }
})

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

// 重置查询
const resetQuery = () => {
  Object.keys(queryForm).forEach(key => {
    queryForm[key] = ''
  })
  getDataList()
}

// 新增
const handleAdd = () => {
  Object.keys(form).forEach(key => {
    if (key === 'id') {
      form[key] = ''
    } else {
      form[key] = ''
    }
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = async (row: any) => {
  Object.keys(form).forEach(key => {
    form[key] = row[key]
  })
  dialogVisible.value = true
}

// 删除
const handleDel = async (row: any) => {
  try {
    await messageBox.confirm(`是否确认删除ID为${row.id}的记录？`)
    await delObj(row.id)
    message.success('删除成功')
    getDataList()
  } catch {
    // 用户取消
  }
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      submitLoading.value = true
      try {
        if (form.id) {
          await putObj(form)
          message.success('修改成功')
        } else {
          await addObj(form)
          message.success('添加成功')
        }
        dialogVisible.value = false
        getDataList()
      } catch (error: any) {
        message.error(error.msg || '操作失败')
      } finally {
        submitLoading.value = false
      }
    }
  })
}

// 表格数据由 useTable（createdIsNeed 默认 true）在挂载时自动请求
</script>

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