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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="schedulesTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('staffId')" prop="staff_id">
                        <el-select class="w-[280px]" v-model="schedulesTable.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('classId')" prop="class_id">
                        <el-select class="w-[280px]" v-model="schedulesTable.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('dateTime')" prop="date_time">
                        <el-date-picker v-model="schedulesTable.searchParam.date_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('createTime')" prop="create_time">
                        <el-date-picker v-model="schedulesTable.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="loadSchedulesList()">{{ 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="schedulesTable.data" size="large" v-loading="schedulesTable.loading">
                    <template #empty>
                        <span>{{ !schedulesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="id" :label="t('id')" 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 prop="class_id_name" :label="t('classId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="date_time" :label="t('dateTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="time_slot" :label="t('timeSlot')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="task" :label="t('task')" min-width="120" :show-overflow-tooltip="true"/>
                    
                     <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="schedulesTable.page" v-model:page-size="schedulesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="schedulesTable.total"
                        @size-change="loadSchedulesList()" @current-change="loadSchedulesList" />
                </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 { getSchedulesList, deleteSchedules, getWithStaffList, getWithClassesList } from '@/addon/zhjw/api/schedules'
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 schedulesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "staff_id":"",
      "class_id":"",
      "date_time":[],
      "task":"",
      "create_time":[]
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    

/**
 * 获取排班管理列表
 */
const loadSchedulesList = (page: number = 1) => {
    schedulesTable.loading = true
    schedulesTable.page = page

    getSchedulesList({
        page: schedulesTable.page,
        limit: schedulesTable.limit,
         ...schedulesTable.searchParam
    }).then(res => {
        schedulesTable.loading = false
        schedulesTable.data = res.data.data
        schedulesTable.total = res.data.total
    }).catch(() => {
        schedulesTable.loading = false
    })
}
loadSchedulesList()

const router = useRouter()

/**
 * 添加排班管理
 */
const addEvent = () => {
    router.push('/schedules/schedules_edit')
}

/**
 * 编辑排班管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/schedules/schedules_edit?id='+data.id)
}

/**
 * 删除排班管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('schedulesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteSchedules(id).then(() => {
            loadSchedulesList()
        }).catch(() => {
        })
    })
}

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

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