<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" label-width="100px">
            <el-form-item label="问题标题" prop="problemTitle">
              <el-input v-model="queryParams.problemTitle" placeholder="请输入问题标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="答复状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择答复状态" clearable @keyup.enter="handleQuery">
                <el-option v-for="(label, value) in statusOptions" :key="value" :label="label" :value="value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发布时间" prop="publishTime" style="width: 330px">
              <el-date-picker
                clearable
                v-model="queryParams.publishTime"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </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>

        <el-card shadow="never" class="mt-[10px]">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDel(null)">批量删除</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" width="50px" type="index"></el-table-column>
            <el-table-column
              :label="label"
              :align="align || 'center'"
              :min-width="minWidth || '100px'"
              v-for="{ label, prop, align, minWidth } in headers"
              :key="prop"
            >
              <template #default="{ row }">
                <span v-if="prop == 'status'">{{ statusOptions[row.status] }}</span>
                <span v-else>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" min-width="120px" fixed="right" class-name="small-padding fixed-width">
              <template #default="{ row }">
                <el-button link type="primary" v-if="row.status == 2" @click="handleShow(row, 'view')">详情</el-button>
                <el-button link type="primary" v-else @click="handleShow(row, 'reply')">解答</el-button>
                <el-button link type="danger" @click="handleDel(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="page.total > 0" :total="page.total" v-model:page="page.pageNum" v-model:limit="page.pageSize" @pagination="getList" />
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script setup name="Question" lang="ts">
import { getTableList, delQuestion } from '@/api/question';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

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

const route = useRoute();

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}
const headers = ref<Header[]>([
  {
    label: '问题标题',
    prop: 'problemTitle'
  },
  {
    label: '答复状态',
    prop: 'status'
  },
  {
    label: '发布人',
    prop: 'userName'
  },
  {
    label: '发布时间',
    prop: 'problemTime'
  }
]);

const statusOptions = {
  1: '未答复',
  2: '已答复'
};
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  problemTitle: '',
  status: '',
  publishTime: []
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  let params = {
    ...queryParams.value
  };
  let startTime = params.publishTime[0] ? params.publishTime[0] + ' 00:00:00' : '';
  let endTime = params.publishTime[1] ? params.publishTime[1] + ' 23:59:59' : '';
  delete params.publishTime;
  let pageParams = {...page.value}
  delete pageParams.total
  const res = await getTableList({ ...params, ...pageParams, startTime, endTime });
  if (res.code != 200) {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: 'error'
    });
    return;
  }

  /* let res = {
    rows: [
      {
        id: 1,
        title: '问题标题1',
        status: 1,
        createBy: '张三',
        createTime: '2024-12-12 12:12:12',
        status: 1
      },
      {
        id: 2,
        title: '问题标题2',
        status: 0,
        createBy: '张三',
        createTime: '2024-12-12 12:12:12',
        status: 0
      },
      {
        id: 3,
        title: '问题标题3',
        status: 1,
        createBy: '张三',
        createTime: '2024-12-12 12:12:12',
        status: 1
      },
      {
        id: 4,
        title: '问题标题4',
        status: 0,
        createBy: '张三',
        createTime: '2024-12-12 12:12:12',
        status: 1
      }
    ]
  }; */
  questionList.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 = async (row: any, type) => {
  proxy?.$router.push({
    path: '/questionDetail',
    query: { id: row.id, type }
  });
};
const ids = ref<Array<string | number>>([]);
const multiple = ref<boolean>(true);
// 多选框选中数据
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
};
const handleDel = (row) => {
  let id = [];
  if (!row) {
    id = ids.value;
  } else {
    id = [row.id];
  }
  ElMessageBox.confirm('是否确定删除', '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const params = {
        ids: id
      };
      let res = await delQuestion(params);
      if (res.code != 200) {
        ElNotification({
          title: '提示',
          message: res.msg,
          type: 'error'
        });
        return;
      }
      ElNotification({
        title: '提示',
        message: '删除成功',
        type: 'success'
      });
      getList();
    })
    .catch(() => {});
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
