<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <span class="text-lg">{{pageName}}</span>
                <el-button type="primary" @click="addEvent">
                    {{ t('addFeedback') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="feedbackTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('studentsId')" prop="students_id">
                        <el-select class="w-[280px]" v-model="feedbackTable.searchParam.students_id" clearable :placeholder="t('studentsIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in studentsIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadFeedbackList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card>

            <div class="mt-[10px]">
                <el-table :data="feedbackTable.data" size="large" v-loading="feedbackTable.loading">
                    <template #empty>
                        <span>{{ !feedbackTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="students_id_name" :label="t('studentsId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="content" :label="t('content')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="mailbox" :label="t('mailbox')" min-width="120" :show-overflow-tooltip="true"/>
                    

                    <el-table-column :label="t('operation')" fixed="right" min-width="120">
                       <template #default="{ row }">
                           <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                           <el-button type="primary" link @click="deleteEvent(row.id)">{{ t('delete') }}</el-button>
                       </template>
                    </el-table-column>

                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="feedbackTable.page" v-model:page-size="feedbackTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="feedbackTable.total"
                        @size-change="loadFeedbackList()" @current-change="loadFeedbackList" />
                </div>
            </div>

            
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getFeedbackList, deleteFeedback, getWithStudentsList } from '@/addon/zhjw/api/feedback'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let feedbackTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "students_id":""
    }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    

/**
 * 获取意见反馈列表
 */
const loadFeedbackList = (page: number = 1) => {
    feedbackTable.loading = true
    feedbackTable.page = page

    getFeedbackList({
        page: feedbackTable.page,
        limit: feedbackTable.limit,
         ...feedbackTable.searchParam
    }).then(res => {
        feedbackTable.loading = false
        feedbackTable.data = res.data.data
        feedbackTable.total = res.data.total
    }).catch(() => {
        feedbackTable.loading = false
    })
}
loadFeedbackList()

const router = useRouter()

/**
 * 添加意见反馈
 */
const addEvent = () => {
    router.push('/feedback/feedback_edit')
}

/**
 * 编辑意见反馈
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/feedback/feedback_edit?id='+data.id)
}

/**
 * 删除意见反馈
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('feedbackDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteFeedback(id).then(() => {
            loadFeedbackList()
        }).catch(() => {
        })
    })
}

    
    const studentsIdList = ref([])
    const setStudentsIdList = async () => {
    studentsIdList.value = await (await getWithStudentsList({})).data
    }
    setStudentsIdList()

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadFeedbackList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>
