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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="staffTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('memberId')" prop="member_id">
                        <el-select class="w-[280px]" v-model="staffTable.searchParam.member_id" clearable :placeholder="t('memberIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in memberIdList"
                                       :key="index"
                                       :label="item['username']"
                                       :value="item['member_id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('name')" prop="name">
                        <el-input v-model="staffTable.searchParam.name" :placeholder="t('namePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('gender')" prop="gender">
                        <el-select class="w-[280px]" v-model="staffTable.searchParam.gender" clearable :placeholder="t('genderPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in genderList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('phone')" prop="phone">
                        <el-input v-model="staffTable.searchParam.phone" :placeholder="t('phonePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('email')" prop="email">
                        <el-input v-model="staffTable.searchParam.email" :placeholder="t('emailPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('position')" prop="position">
                        <el-input v-model="staffTable.searchParam.position" :placeholder="t('positionPlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="staffTable.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="loadStaffList()">{{ 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="staffTable.data" size="large" v-loading="staffTable.loading">
                    <template #empty>
                        <span>{{ !staffTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="member_id_name" :label="t('memberId')" 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 :label="t('header')" width="100" align="left">
                        <template #default="{ row }">
                            <el-avatar v-if="row.header" :src="img(row.header)" />
                            <el-avatar v-else icon="UserFilled" />
                        </template>
                    </el-table-column>
                    <el-table-column :label="t('gender')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in genderList">
                                <div v-if="item.value == row.gender">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="phone" :label="t('phone')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="email" :label="t('email')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="position" :label="t('position')" 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('roleId')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in role_idList">
                                <div v-if="item.value == row.role_id">{{ 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="staffTable.page" v-model:page-size="staffTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="staffTable.total"
                        @size-change="loadStaffList()" @current-change="loadStaffList" />
                </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 { getStaffList, deleteStaff, getWithMemberList } from '@/addon/zhjw/api/staff'
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 staffTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "member_id":"",
      "name":"",
      "gender":"",
      "phone":"",
      "email":"",
      "position":"",
      "status":""
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const genderList = ref([] as any[])
    const genderDictList = async () => {
    genderList.value = await (await useDictionary('sex')).data.dictionary
    }
    genderDictList();
    const statusList = ref([] as any[])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('zz_status')).data.dictionary
    }
    statusDictList();
    const role_idList = ref([] as any[])
    const role_idDictList = async () => {
    role_idList.value = await (await useDictionary('roles_name')).data.dictionary
    }
    role_idDictList();


/**
 * 获取人员管理列表
 */
const loadStaffList = (page: number = 1) => {
    staffTable.loading = true
    staffTable.page = page

    getStaffList({
        page: staffTable.page,
        limit: staffTable.limit,
         ...staffTable.searchParam
    }).then(res => {
        staffTable.loading = false
        staffTable.data = res.data.data
        staffTable.total = res.data.total
    }).catch(() => {
        staffTable.loading = false
    })
}
loadStaffList()

const router = useRouter()

/**
 * 添加人员管理
 */
const addEvent = () => {
    router.push('/staff/staff_edit')
}

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

/**
 * 删除人员管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('staffDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteStaff(id).then(() => {
            loadStaffList()
        }).catch(() => {
        })
    })
}

    
    const memberIdList = ref([])
    const setMemberIdList = async () => {
    memberIdList.value = await (await getWithMemberList({})).data
    }
    setMemberIdList()

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