<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="planName">
              <el-input v-model="queryParams.planName" 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">新增</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="PlanList">
            <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'"
              prop="expertName"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                <span v-if="prop === 'planTime'">{{ row.startTime }} - {{ row.endTime }}</span>
                <span v-else-if="prop === 'studyContent'">{{ row.studyContent || '-' }}</span>
                <span v-else>{{ 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" />
                <el-button link type="primary" @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>
      </div>
    </transition>
  </div>
</template>

<script lang="ts" setup>
import { getPlanList, delPlan } from '@/api/study/studyPlan';
import { PlanVO, PlanQuery } from '@/api/study/studyPlan/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const PlanList = ref<PlanVO[]>([]);
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: 'planName'
  },
  {
    label: '学习目标',
    prop: 'studyTarget'
  },
  {
    label: '计划执行时间',
    prop: 'planTime'
  },
  {
    label: '学习内容',
    prop: 'studyContent'
  }
]);

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

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await getPlanList({
    ...queryParams.value,
    ...page.value
  });
  PlanList.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 handleShow = (row: PlanVO) => {
  proxy.$router.push({
    path: '/study/studyPlanDetail',
    query: {
      id: row.id
    }
  });
};
const activeRow = ref({});
const OpenPlan = () => {
  proxy.$router.push({
    path: '/study/studyPlanAdd'
  });
};
const deletePlan = (row: PlanVO) => {
  activeRow.value = row;
  ElMessageBox.confirm('确定要删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    let res = await delPlan(row.id);
    if (res.code === 200) {
      ElNotification({
        title: '提示',
        message: '删除成功',
        type: 'success'
      });
      getList();
      return;
    }
    ElNotification({
      title: '提示',
      message: res.msg,
      type: 'error'
    });
  });
};
onActivated(() => {
  getList();
})
onMounted(() => {
  getList();
});
</script>
