<script setup lang="ts">
import type { DiscussListItem } from '../types'
import AppList from '@/components/base/AppList.vue'
import { getDiscussList } from '../api'
import { useFilterList } from '../composables/useFilterList'

const DiscussDialog = defineAsyncComponent(() => import('../components/DiscussDialog.vue'))

const { courses, experiments, specialties, classes } = useFilterList()

const appList = $ref<InstanceType<typeof AppList> | null>(null)
// 列表配置
const listOptions = $computed(() => {
  return {
    remote: {
      httpRequest: getDiscussList,
      params: { student_name: '', course_id: '', experiment_id: '', specialty_id: '', class_id: '' }
    },
    filterForm: { labelWidth: 100 },
    filters: [
      {
        type: 'select',
        prop: 'course_id',
        label: '实验课程名称',
        placeholder: '请选择实验课程名称',
        options: courses.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      {
        type: 'select',
        prop: 'experiment_id',
        label: '实验名称',
        placeholder: '请选择实验名称',
        options: experiments.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      {
        type: 'select',
        prop: 'specialty_id',
        label: '专业',
        placeholder: '请选择专业',
        options: specialties.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      {
        type: 'select',
        prop: 'class_id',
        label: '班级',
        placeholder: '请选择班级',
        options: classes.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      { type: 'input', prop: 'student_name', label: '学生姓名', placeholder: '请输入学生姓名' }
    ],
    columns: [
      { label: '序号', type: 'index', width: 60 },
      { label: '实验名称', prop: 'experiment_id_name' },
      { label: '专业', prop: 'specialty_id_name' },
      { label: '班级', prop: 'class_id_name' },
      { label: '姓名', prop: 'student_name' },
      { label: '学号', prop: 'sno_number' },
      { label: '标题', prop: 'title' },
      { label: '评论数量', prop: 'replies_num', slots: 'table-replies-num' },
      { label: '是否回复', prop: 'is_reply_name', slots: 'table-reply' },
      { label: '更新时间', prop: 'created_time' },
      { label: '操作', slots: 'table-x', width: 100 }
    ]
  }
})

let dialogVisible = $ref(false)
const rowData = ref<DiscussListItem>()
// 评论
function handleComment(row: DiscussListItem) {
  rowData.value = row
  dialogVisible = true
}
function onUpdateSuccess() {
  appList?.refetch()
}
</script>

<template>
  <AppCard title="实验讨论交流">
    <AppList v-bind="listOptions" ref="appList">
      <template #table-replies-num="{ row }">
        <span :class="{ 'is-info': !!row.replies_num }">{{ row.replies_num }}</span>
      </template>
      <template #table-reply="{ row }">
        <span :class="{ 'is-success': row.is_reply === '1' }">{{ row.is_reply_name }}</span>
      </template>
      <template #table-x="{ row }">
        <el-button text type="primary" @click="handleComment(row)" v-permission="'v1-teacher-discussion-view'"
          >评论</el-button
        >
      </template>
    </AppList>
  </AppCard>
  <DiscussDialog
    v-model="dialogVisible"
    :data="rowData"
    @update="onUpdateSuccess"
    v-if="dialogVisible && rowData"
  ></DiscussDialog>
</template>

<style lang="scss" scoped>
.is-success {
  color: #63a103;
}
.is-info {
  color: #b18862;
}
</style>
