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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="salaryTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('staffId')" prop="staff_id">
                        <el-select class="w-[280px]" v-model="salaryTable.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('departmentId')" prop="department_id">
                        <el-select class="w-[280px]" v-model="salaryTable.searchParam.department_id" clearable :placeholder="t('departmentIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in departmentIdList"
                                       :key="index"
                                       :label="item['department_name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('paymentStatus')" prop="payment_status">
                        <el-select class="w-[280px]" v-model="salaryTable.searchParam.payment_status" clearable :placeholder="t('paymentStatusPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in payment_statusList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('createdAt')" prop="created_at">
                        <el-date-picker v-model="salaryTable.searchParam.created_at" 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="loadSalaryList()">{{ 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="salaryTable.data" size="large" v-loading="salaryTable.loading">
                    <template #empty>
                        <span>{{ !salaryTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="staff_id_name" :label="t('staffId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="department_id_name" :label="t('departmentId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="net_salary" :label="t('netSalary')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('paymentStatus')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in payment_statusList">
                                <div v-if="item.value == row.payment_status">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column :label="t('paymentMethod')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in payment_methodList">
                                <div v-if="item.value == row.payment_method">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="salary_month" :label="t('salaryMonth')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="created_at" :label="t('createdAt')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="updated_at" :label="t('updatedAt')" 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="salaryTable.page" v-model:page-size="salaryTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="salaryTable.total"
                        @size-change="loadSalaryList()" @current-change="loadSalaryList" />
                </div>
            </div>

            <edit ref="editSalaryDialog" @complete="loadSalaryList" />
        </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 { getSalaryList, deleteSalary, getWithPersonnelList, getWithDepartmentsList } from '@/app/api/salary'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import Edit from '@/app/views/salary/components/salary-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let salaryTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "staff_id":"",
      "department_id":"",
      "payment_status":"",
      "created_at":[]
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const payment_statusList = ref([] as any[])
    const payment_statusDictList = async () => {
    payment_statusList.value = await (await useDictionary('payment_status')).data.dictionary
    }
    payment_statusDictList();
    const payment_methodList = ref([] as any[])
    const payment_methodDictList = async () => {
    payment_methodList.value = await (await useDictionary('payment_method')).data.dictionary
    }
    payment_methodDictList();


/**
 * 获取工资列表
 */
const loadSalaryList = (page: number = 1) => {
    salaryTable.loading = true
    salaryTable.page = page

    getSalaryList({
        page: salaryTable.page,
        limit: salaryTable.limit,
         ...salaryTable.searchParam
    }).then(res => {
        salaryTable.loading = false
        salaryTable.data = res.data.data
        salaryTable.total = res.data.total
    }).catch(() => {
        salaryTable.loading = false
    })
}
loadSalaryList()

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

/**
 * 添加工资
 */
const addEvent = () => {
    editSalaryDialog.value.setFormData()
    editSalaryDialog.value.showDialog = true
}

/**
 * 编辑工资
 * @param data
 */
const editEvent = (data: any) => {
    editSalaryDialog.value.setFormData(data)
    editSalaryDialog.value.showDialog = true
}

/**
 * 删除工资
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('salaryDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteSalary(id).then(() => {
            loadSalaryList()
        }).catch(() => {
        })
    })
}

    
    const staffIdList = ref([])
    const setStaffIdList = async () => {
    staffIdList.value = await (await getWithPersonnelList({})).data
    }
    setStaffIdList()
    const departmentIdList = ref([])
    const setDepartmentIdList = async () => {
    departmentIdList.value = await (await getWithDepartmentsList({})).data
    }
    setDepartmentIdList()

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