<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('addAssignments') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="assignmentsTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('studentId')" prop="student_id">
                        <el-select class="w-[280px]" v-model="assignmentsTable.searchParam.student_id" clearable :placeholder="t('studentIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in studentIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('classId')" prop="class_id">
                        <el-select class="w-[280px]" v-model="assignmentsTable.searchParam.class_id" clearable :placeholder="t('classIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in classIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('staffId')" prop="staff_id">
                        <el-select class="w-[280px]" v-model="assignmentsTable.searchParam.staff_id" clearable :placeholder="t('staffIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in staffIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('contentType')" prop="content_type">
                        <el-select class="w-[280px]" v-model="assignmentsTable.searchParam.content_type" clearable :placeholder="t('contentTypePlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in content_typeList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('submitTime')" prop="submit_time">
                        <el-date-picker v-model="assignmentsTable.searchParam.submit_time" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('score')" prop="score">
                        <range-input v-model="assignmentsTable.searchParam.score"/>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="assignmentsTable.searchParam.status" clearable :placeholder="t('statusPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in statusList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('createTime')" prop="create_time">
                        <el-date-picker v-model="assignmentsTable.searchParam.create_time" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadAssignmentsList()">{{ 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="assignmentsTable.data" size="large" v-loading="assignmentsTable.loading">
                    <template #empty>
                        <span>{{ !assignmentsTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="student_id_name" :label="t('studentId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="class_id_name" :label="t('classId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="staff_id_name" :label="t('staffId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('contentType')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in content_typeList">
                                <div v-if="item.value == row.content_type">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="submit_time" :label="t('submitTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="score" :label="t('score')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('status')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in statusList">
                                <div v-if="item.value == row.status">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('createTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.create_time || '' }}
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('updateTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.update_time || '' }}
                        </template>
                    </el-table-column>
                    

                    <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="assignmentsTable.page" v-model:page-size="assignmentsTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="assignmentsTable.total"
                        @size-change="loadAssignmentsList()" @current-change="loadAssignmentsList" />
                </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 { getAssignmentsList, deleteAssignments, getWithStudentsList, getWithClassesList, getWithStaffList } from '@/addon/zhjw/api/assignments'
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 assignmentsTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "student_id":"",
      "class_id":"",
      "staff_id":"",
      "content_type":"",
      "submit_time":[],
      "score":[],
      "status":"",
      "create_time":[]
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const content_typeList = ref([] as any[])
    const content_typeDictList = async () => {
    content_typeList.value = await (await useDictionary('zhjw_assignments_content_type')).data.dictionary
    }
    content_typeDictList();
    const statusList = ref([] as any[])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('zhjw_assignments_status')).data.dictionary
    }
    statusDictList();


/**
 * 获取作业管理列表
 */
const loadAssignmentsList = (page: number = 1) => {
    assignmentsTable.loading = true
    assignmentsTable.page = page

    getAssignmentsList({
        page: assignmentsTable.page,
        limit: assignmentsTable.limit,
         ...assignmentsTable.searchParam
    }).then(res => {
        assignmentsTable.loading = false
        assignmentsTable.data = res.data.data
        assignmentsTable.total = res.data.total
    }).catch(() => {
        assignmentsTable.loading = false
    })
}
loadAssignmentsList()

const router = useRouter()

/**
 * 添加作业管理
 */
const addEvent = () => {
    router.push('/assignments/assignments_edit')
}

/**
 * 编辑作业管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/assignments/assignments_edit?id='+data.id)
}

/**
 * 删除作业管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('assignmentsDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteAssignments(id).then(() => {
            loadAssignmentsList()
        }).catch(() => {
        })
    })
}

    
    const studentIdList = ref([])
    const setStudentIdList = async () => {
    studentIdList.value = await (await getWithStudentsList({})).data
    }
    setStudentIdList()
    const classIdList = ref([])
    const setClassIdList = async () => {
    classIdList.value = await (await getWithClassesList({})).data
    }
    setClassIdList()
    const staffIdList = ref([])
    const setStaffIdList = async () => {
    staffIdList.value = await (await getWithStaffList({})).data
    }
    setStaffIdList()

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadAssignmentsList()
}
</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>
