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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="attendanceTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('campusId')" prop="campus_id">
                        <el-select class="w-[280px]" v-model="attendanceTable.searchParam.campus_id" clearable :placeholder="t('campusIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in campusIdList"
                                       :key="index"
                                       :label="item['campus_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="attendanceTable.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('attendanceDate')" prop="attendance_date">
							
							<el-date-picker
							    class="flex-1 !flex"
							    v-model="attendanceTable.searchParam.attendance_date"
							    clearable
							    type="date"
							    value-format="YYYY-MM-DD"
							    :placeholder="t('attendanceDate')">
							</el-date-picker>
							
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="attendanceTable.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>
                        <el-button type="primary" @click="loadAttendanceList()">{{ 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="attendanceTable.data" size="large" v-loading="attendanceTable.loading">
                    <template #empty>
                        <span>{{ !attendanceTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="campus_id_name" :label="t('campusId')" 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="attendance_date" :label="t('attendanceDate')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="check_in_time" :label="t('checkInTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="check_out_time" :label="t('checkOutTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="remarks" :label="t('remarks')" 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('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="attendanceTable.page" v-model:page-size="attendanceTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="attendanceTable.total"
                        @size-change="loadAttendanceList()" @current-change="loadAttendanceList" />
                </div>
            </div>

            <edit ref="editAttendanceDialog" @complete="loadAttendanceList" />
        </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 { getAttendanceList, deleteAttendance, getWithCampusList, getWithPersonnelList } from '@/app/api/attendance'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import Edit from '@/app/views/attendance/components/attendance-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let attendanceTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "campus_id":"",
      "staff_id":"",
      "attendance_date":"",
      "status":""
    }
})

const searchFormRef = ref<FormInstance>()

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

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


/**
 * 获取考勤列表
 */
const loadAttendanceList = (page: number = 1) => {
    attendanceTable.loading = true
    attendanceTable.page = page

    getAttendanceList({
        page: attendanceTable.page,
        limit: attendanceTable.limit,
         ...attendanceTable.searchParam
    }).then(res => {
        attendanceTable.loading = false
        attendanceTable.data = res.data.data
        attendanceTable.total = res.data.total
    }).catch(() => {
        attendanceTable.loading = false
    })
}
loadAttendanceList()

const editAttendanceDialog: Record<string, any> | null = ref(null)

/**
 * 添加考勤
 */
const addEvent = () => {
    editAttendanceDialog.value.setFormData()
    editAttendanceDialog.value.showDialog = true
}

/**
 * 编辑考勤
 * @param data
 */
const editEvent = (data: any) => {
    editAttendanceDialog.value.setFormData(data)
    editAttendanceDialog.value.showDialog = true
}

/**
 * 删除考勤
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('attendanceDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteAttendance(id).then(() => {
            loadAttendanceList()
        }).catch(() => {
        })
    })
}

    
    const campusIdList = ref([])
    const setCampusIdList = async () => {
    campusIdList.value = await (await getWithCampusList({})).data
    }
    setCampusIdList()
    const staffIdList = ref([])
    const setStaffIdList = async () => {
    staffIdList.value = await (await getWithPersonnelList({})).data
    }
    setStaffIdList()

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