<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch">
        <el-card class="mb-[10px]" shadow="hover">
          <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-width="auto">
            <el-form-item label="专家姓名" prop="expertName">
              <el-input v-model="queryParams.expertName" clearable placeholder="请输入专家姓名" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <el-card shadow="never">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button plain type="primary" @click="OpenPlan" v-hasPermi="['study:studyFeedback:add']">新增</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="feedbackList">
            <el-table-column label="序号" type="index" width="50px"></el-table-column>
            <el-table-column
              v-for="{ label, prop, align, minWidth } in headers"
              :key="prop"
              :align="align || 'center'"
              :label="label"
              :min-width="minWidth || '100px'"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                <span>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
              <template #default="{ row }">
                <el-button link type="primary" @click="handleShow(row)">查看</el-button>
                <el-divider direction="vertical" v-hasPermi="['study:studyFeedback:delete']" />
                <el-button link type="primary" v-hasPermi="['study:studyFeedback:delete']" @click="deletePlan(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="page.total > 0" v-model:limit="page.pageSize" v-model:page="page.pageNum" :total="page.total" @pagination="getList" />
        </el-card>
        <el-dialog v-model="showDialog" :before-close="closeDialog" title="详情" width="800px">
          <div class="flex justify-between lh-[20px]">
            <span class="text-[16px] font-600"> {{ activeRow.backName }} </span>
            <span class="text-[12px] color-[#999]"> {{ activeRow.createTime }} </span>
          </div>
          <p class="text-[14px]">{{ activeRow.backContent }}</p>
          <template #footer>
            <el-button @click="closeDialog">关闭</el-button>
          </template>
        </el-dialog>
      </div>
    </transition>
  </div>
</template>

<script lang="ts" setup>
import { getFeedbackList, delFeedback } from '@/api/study/studyFeedback';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const feedbackList = ref<any>([]);
const loading = ref(true);
const showSearch = ref(true);

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}

const headers = ref<Header[]>([
  {
    label: '内容名称',
    prop: 'backName'
  },
  {
    label: '专家姓名',
    prop: 'expertName'
  },
  {
    label: '提交时间',
    prop: 'createTime'
  },
  {
    label: '反馈内容',
    prop: 'backContent'
  }
]);

const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  expertName: ''
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await getFeedbackList({
    ...queryParams.value,
    ...page.value
  })
  feedbackList.value = res.rows;
  page.value.total = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const activeRow = ref<any>({});
const OpenPlan = () => {
  proxy.$router.push({
    path: '/study/studyFeedbackAdd'
  });
};
const showDialog = ref(false);
const closeDialog = () => {
  showDialog.value = false;
};
const handleShow = (row: any) => {
  activeRow.value = row;
  showDialog.value = true;
};
const deletePlan = (row: any) => {
  activeRow.value = row;
  ElMessageBox.confirm('是否确认删除', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      console.log(row);
      let res = await delFeedback(row.id);
      if (res.code === 200) {
        ElNotification({
          title: '提示',
          message: '删除成功',
          type: 'success'
        });
        getList();
        return;
      }
      ElNotification({
        title: '提示',
        message: res.msg,
        type: 'error'
      });
    })
    .catch(() => {});
};
onActivated(() => {
  getList();
})
onMounted(() => {
  getList();
});
</script>
<style scoped lang="scss">
:deep(.el-table) {
  .el-popper {
    width: 500px !important;
  }
}
</style>
