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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="studentsTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('name')" prop="name">
                        <el-input v-model="studentsTable.searchParam.name" :placeholder="t('namePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('userId')" prop="user_id">
                        <el-select class="w-[280px]" v-model="studentsTable.searchParam.user_id" clearable :placeholder="t('userIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in userIdList"
                                       :key="index"
                                       :label="item['mobile']"
                                       :value="item['member_id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('haveStudyTime')" prop="have_study_time">
                        <range-input v-model="studentsTable.searchParam.have_study_time"/>
                    </el-form-item>
                    
                    <el-form-item :label="t('endStudyTime')" prop="end_study_time">
                        <range-input v-model="studentsTable.searchParam.end_study_time"/>
                    </el-form-item>
                    
                    <el-form-item :label="t('emergencyContact')" prop="emergency_contact">
                        <el-input v-model="studentsTable.searchParam.emergency_contact" :placeholder="t('emergencyContactPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('level')" prop="level">
                        <el-input v-model="studentsTable.searchParam.level" :placeholder="t('levelPlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="studentsTable.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="studentsTable.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="loadStudentsList()">{{ 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="studentsTable.data" size="large" v-loading="studentsTable.loading">
                    <template #empty>
                        <span>{{ !studentsTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="id" :label="t('id')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="name" :label="t('name')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="user_id_name" :label="t('userId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="have_study_time" :label="t('haveStudyTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="end_study_time" :label="t('endStudyTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="emergency_contact" :label="t('emergencyContact')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="level" :label="t('level')" 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="studentsTable.page" v-model:page-size="studentsTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="studentsTable.total"
                        @size-change="loadStudentsList()" @current-change="loadStudentsList" />
                </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 { getStudentsList, deleteStudents, getWithUsersList } from '@/addon/zhjw/api/students'
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 studentsTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "name":"",
      "user_id":"",
      "have_study_time":[],
      "end_study_time":[],
      "emergency_contact":"",
      "level":"",
      "status":"",
      "create_time":[]
    }
})

const searchFormRef = ref<FormInstance>()

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

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


/**
 * 获取学员管理列表
 */
const loadStudentsList = (page: number = 1) => {
    studentsTable.loading = true
    studentsTable.page = page

    getStudentsList({
        page: studentsTable.page,
        limit: studentsTable.limit,
         ...studentsTable.searchParam
    }).then(res => {
        studentsTable.loading = false
        studentsTable.data = res.data.data
        studentsTable.total = res.data.total
    }).catch(() => {
        studentsTable.loading = false
    })
}
loadStudentsList()

const router = useRouter()

/**
 * 添加学员管理
 */
const addEvent = () => {
    router.push('/students/students_edit')
}

/**
 * 编辑学员管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/students/students_edit?id='+data.id)
}

/**
 * 删除学员管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('studentsDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteStudents(id).then(() => {
            loadStudentsList()
        }).catch(() => {
        })
    })
}

    
    const userIdList = ref([])
    const setUserIdList = async () => {
    userIdList.value = await (await getWithUsersList({})).data
    }
    setUserIdList()

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