<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="学校名称" prop="nameCn">
              <el-input v-model="queryParams.nameCn" placeholder="模糊搜索" clearable style="width: 200px" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="国家/地区" prop="country">
              <el-input v-model="queryParams.country" placeholder="国家/地区" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="性质" prop="nature">
              <el-input v-model="queryParams.nature" placeholder="公立/私立" clearable style="width: 120px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['edu:school:add']" type="primary" plain icon="Plus" @click="handleAdd">新增学校</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['edu:school:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">批量删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['edu:school:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:show-search="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" border :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="中文名" align="center" prop="nameCn" min-width="160" show-overflow-tooltip />
        <el-table-column label="英文名" align="center" prop="nameEn" min-width="200" show-overflow-tooltip />
        <el-table-column label="国家/地区" align="center" prop="country" width="120" />
        <el-table-column label="性质" align="center" prop="nature" width="100" />
        <el-table-column label="排名" align="center" prop="ranking" width="120" />
        <el-table-column label="语言要求" align="center" prop="languageReq" width="150" show-overflow-tooltip />
        <el-table-column label="官网" align="center" prop="officialUrl" width="160" show-overflow-tooltip>
          <template #default="scope">
            <el-link v-if="scope.row.officialUrl" :href="scope.row.officialUrl" target="_blank" type="primary" :underline="false">访问</el-link>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center" fixed="right">
          <template #default="scope">
            <el-tooltip content="编辑" placement="top">
              <el-button v-hasPermi="['edu:school:edit']" link type="primary" icon="Edit" @click="handleEdit(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['edu:school:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="中文名" prop="nameCn">
              <el-input v-model="form.nameCn" placeholder="学校中文名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名">
              <el-input v-model="form.nameEn" placeholder="School Name" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="国家/地区">
              <el-input v-model="form.country" placeholder="如：英国" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性质">
              <el-input v-model="form.nature" placeholder="公立/私立" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排名">
              <el-input v-model="form.ranking" placeholder="QS/THE排名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="官网">
              <el-input v-model="form.officialUrl" placeholder="https://" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式">
              <el-input v-model="form.contactInfo" placeholder="联系电话/邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="语言要求">
          <el-input v-model="form.languageReq" placeholder="雅思/托福要求" />
        </el-form-item>
        <el-form-item label="费用信息">
          <el-input v-model="form.fees" placeholder="学费/生活费" />
        </el-form-item>
        <el-form-item label="优势专业">
          <el-input v-model="form.advantages" type="textarea" :rows="2" placeholder="优势专业介绍" />
        </el-form-item>
        <el-form-item label="学校简介">
          <el-input v-model="form.intro" type="textarea" :rows="3" placeholder="学校简介" />
        </el-form-item>
        <el-form-item label="申请材料">
          <el-input v-model="form.materials" type="textarea" :rows="2" placeholder="申请材料清单" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialog.visible = false">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="EduSchool" lang="ts">
import { listSchool, getSchool, addSchool, updateSchool, delSchool } from '@/api/edu/school';
import { SchoolQuery, SchoolVO, SchoolForm } from '@/api/edu/school/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const dataList = ref<SchoolVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<number | string>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const formRef = ref<ElFormInstance>();
const dialog = reactive<{ visible: boolean; title: string }>({ visible: false, title: '' });

const data = reactive<PageData<SchoolForm, SchoolQuery>>({
  form: { nameCn: '', nameEn: '', country: '', nature: '', ranking: '', intro: '', history: '', advantages: '', fees: '', languageReq: '', materials: '', officialUrl: '', contactInfo: '', brochureUrl: '', remark: '' },
  queryParams: { pageNum: 1, pageSize: 10, nameCn: '', country: '', nature: '' },
  rules: { nameCn: [{ required: true, message: '请输入学校中文名', trigger: 'blur' }] }
});
const { queryParams, form, rules } = toRefs(data);

const getList = async () => {
  loading.value = true;
  const res = await listSchool(queryParams.value);
  dataList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

const handleQuery = () => { queryParams.value.pageNum = 1; getList(); };
const resetQuery = () => { queryFormRef.value?.resetFields(); handleQuery(); };
const handleSelectionChange = (selection: SchoolVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

const reset = () => { form.value = { nameCn: '', nameEn: '', country: '', nature: '', ranking: '', intro: '', history: '', advantages: '', fees: '', languageReq: '', materials: '', officialUrl: '', contactInfo: '', brochureUrl: '', remark: '' }; formRef.value?.resetFields(); };

const handleAdd = () => { reset(); dialog.title = '新增学校'; dialog.visible = true; };
const handleEdit = async (row: SchoolVO) => { reset(); const res = await getSchool(row.id); Object.assign(form.value, res.data); dialog.title = '编辑学校'; dialog.visible = true; };

const submitForm = async () => {
  await formRef.value?.validate();
  if (form.value.id) { await updateSchool(form.value); proxy?.$modal.msgSuccess('修改成功'); }
  else { await addSchool(form.value); proxy?.$modal.msgSuccess('新增成功'); }
  dialog.visible = false; await getList();
};

const handleDelete = async (row?: SchoolVO) => {
  const delIds = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除选中的学校记录？');
  await delSchool(delIds); proxy?.$modal.msgSuccess('删除成功'); await getList();
};

const handleExport = () => { proxy?.download('edu/school/export', { ...queryParams.value }, `school_${new Date().getTime()}.xlsx`); };

onMounted(() => { getList(); });
</script>
