<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="name">
              <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="队伍名称" prop="teamName">
              <el-input v-model="queryParams.teamName" placeholder="请输入队伍名称" clearable @keyup.enter="handleQuery" />
            </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="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:advice:remove']"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:advice:export']"> 导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="adviceList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="id" align="center" prop="id" v-if="true" />
        <el-table-column label="姓名" align="center" prop="name" />
        <!--        <el-table-column label="队伍id" align="center" prop="teamId" />-->
        <el-table-column label="队伍名称" align="center" prop="teamName" />
        <!--        <el-table-column label="反馈内容" align="center" prop="content" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="预览" placement="top">
              <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['system:article:view']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:advice:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <el-dialog title="建议预览" v-model="dialog.visible" width="1200px" append-to-body>
      <el-form ref="articleFormRef" :model="form" label-width="80px" disabled>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="队伍名称" prop="teamName">
          <el-input v-model="form.teamName" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" readOnly />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Advice" lang="ts">
import { listAdvice, delAdvice } from '@/api/system/advice';
import { AdviceVO, AdviceQuery, AdviceForm } from '@/api/system/advice/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const adviceList = ref<AdviceVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const adviceFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: AdviceForm = {
  id: undefined,
  name: undefined,
  teamId: undefined,
  content: undefined
};
const data = reactive<PageData<AdviceForm, AdviceQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    content: undefined,
    params: {},
    orderByColumn: undefined,
    isAsc: undefined,
  },
  rules: {
    name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
    teamId: [{ required: true, message: '队伍id不能为空', trigger: 'change' }],
    content: [{ required: true, message: '反馈内容不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户反馈建议列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAdvice(queryParams.value);
  adviceList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  adviceFormRef.value?.resetFields();
};

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: AdviceVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 删除按钮操作 */
const handleDelete = async (row?: AdviceVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除用户反馈建议编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delAdvice(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const handleView = async (row?: AdviceVO) => {
  Object.assign(form.value, row);
  dialog.visible = true;
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/advice/export',
    {
      ...queryParams.value
    },
    `advice_${new Date().getTime()}.xlsx`
  );
};

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