<template>
    <!-- 公司管理 ---- 其他人员管理 -->
    <div class="app-container">
        <!-- 查询 -->
        <el-form :model="queryParams" ref="queryParamsRef" label-width="0px" class="ms-content">
            <el-row :gutter="20">
                <el-col :span="4">
                    <el-form-item prop="userName">
                        <el-input v-model.trim="queryParams.userName" placeholder="请输入姓名" clearable> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item prop="title">
                        <el-input v-model.trim="queryParams.title" placeholder="请输入职称" clearable> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item prop="diploma">
                        <el-input v-model.trim="queryParams.diploma" placeholder="请输入资格证书" clearable> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item prop="major">
                        <el-input v-model.trim="queryParams.major" placeholder="请输入专业" clearable> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item>
                        <el-button type="primary" @click="getOtherPersonList">查询</el-button>
                        <el-button @click="resetQueryParams">重置</el-button>
                        <el-button type="primary" @click="onHandleAddOrEdit(null, 1)">新增人员</el-button>
                        <el-button @click="handleExport">导出数据</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <!-- 列表 -->
        <el-table :data="tableList" stripe>
            <el-table-column label="序号" width="55" type="index" align="center" />
            <el-table-column
                :key="index"
                align="center"
                :prop="item.prop"
                :label="item.label"
                show-overflow-tooltip
                v-for="(item, index) in tableColumn"
            >
                <template #default="{ row }">
                    <template v-if="item.isSlot">
                        <template v-if="item.label === '养老保险'">
                            <el-tag size="mini" type="success" v-if="row[item.prop] === '0'">正常</el-tag>
                            <el-tag size="mini" type="danger" v-else>异常</el-tag>
                        </template>
                        <template v-if="item.label === '职工类型'">
                            <el-tag size="mini" type="success" v-if="row[item.prop] === '1'">正式职工</el-tag>
                            <el-tag size="mini" type="success" v-if="row[item.prop] === '2'">劳务派遣</el-tag>
                        </template>
                    </template>

                    <template v-else>
                        {{ row[item.prop] }}
                    </template>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="200" align="center">
                <template #default="{ row }">
                    <el-button
                        type="text"
                        size="mini"
                        icon="el-icon-view"
                        style="color: #67c23a"
                        @click="onHandleAddOrEdit(row, 2)"
                    >
                        详情
                    </el-button>
                    <el-button
                        type="text"
                        size="mini"
                        icon="el-icon-edit"
                        style="color: #409eff"
                        @click="onHandleAddOrEdit(row, 3)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        size="mini"
                        icon="el-icon-delete"
                        style="color: #f56c6c"
                        @click="onHandleDelete(row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getOtherPersonList"
        />
        <DialogModel :dialogConfig="dialogConfig" @closeDialogOuter="closeDialogOuter">
            <template slot="outerContent">
                <AddAndEditForm
                    :editRow="editRow"
                    :formType="formType"
                    ref="addAndEditFormRef"
                    @closeDialogOuter="closeDialogOuter"
                />
            </template>
        </DialogModel>
    </div>
</template>

<script>
import DialogModel from '@/components/DialogModel/index'
import AddAndEditForm from './components/addAndEditForm.vue'
import { getOtherPersonListAPI, deleteOtherPersonAPI } from '@/api/company-manage/other-person'
export default {
    components: {
        DialogModel,
        AddAndEditForm,
    },
    data() {
        return {
            total: 0, // 总条数
            formType: 1, // 1新增 2详情 3编辑
            timeValue: [], // 时间选择器
            editRow: {}, // 编辑行
            // 弹窗配置
            dialogConfig: {
                outerTitle: '新增',
                innerTitle: false,
                outerWidth: '80%',
                outerVisible: false,
                innerVisible: false,
            },
            // 查询参数
            queryParams: {
                title: '',
                major: '',
                userName: '',
                diploma: '',
                pageNum: 1,
                pageSize: 10,
            },
            // 列表
            tableList: [],
            // 列表列
            tableColumn: [
                { label: '姓名', prop: 'userName' },
                { label: '身份证号', prop: 'idCard' },
                { label: '学历', prop: 'qualification' },
                { label: '职称', prop: 'title' },
                { label: '资格证书', prop: 'diploma' },
                { label: '级别', prop: 'level' },
                { label: '证书编号', prop: 'diplomaNum' },
                { label: '专业', prop: 'major' },
                { label: '养老保险', prop: 'isNormal', isSlot: true },
                { label: '职工类型', prop: 'workType', isSlot: true },
            ],
        }
    },
    methods: {
        // 关闭
        closeDialogOuter(isRefresh = false) {
            this.dialogConfig.outerVisible = false
            if (isRefresh) {
                this.getOtherPersonList()
            } else {
                this.$refs.addAndEditFormRef.deleteFileListFun()
            }
        },
        // 删除
        onHandleDelete(row) {
            this.$modal
                .confirm('确定删除该人员吗？')
                .then(async () => {
                    const res = await deleteOtherPersonAPI({ id: row.id })
                    if (res.code === 200) {
                        this.$modal.msgSuccess('删除成功')
                        this.getOtherPersonList()
                    }
                })
                .catch(() => {})
        },
        // 新增或编辑
        onHandleAddOrEdit(row, type) {
            this.formType = type
            if (type === 1) {
                this.dialogConfig.outerTitle = '新增人员'
                this.editRow = {}
            } else if (type === 3) {
                this.dialogConfig.outerTitle = '编辑人员'
            } else if (type === 2) {
                this.dialogConfig.outerTitle = '人员详情'
            }

            if (type === 2 || type === 3) {
                const {
                    userName, // 姓名
                    idCard, // 身份证号码
                    qualification, // 学历
                    title, // 职称
                    workType, // 职工类型
                    major, // 专业
                    diploma, // 资格证书
                    diplomaNum, // 证书编码
                    level, // 级别
                    isNormal, // 养老保险
                    id,
                    tbFileSourceVoList,
                } = row
                Object.assign(this.editRow, {
                    userName,
                    idCard,
                    qualification,
                    title,
                    workType,
                    major,
                    diploma,
                    diplomaNum,
                    level,
                    isNormal,
                    id,
                    tbFileSourceVoList,
                })
            }

            this.dialogConfig.outerVisible = true
        },
        // 获取列表
        async getOtherPersonList() {
            const res = await getOtherPersonListAPI(this.queryParams)
            this.tableList = res.rows
            this.total = res.total
        },
        // 重置
        resetQueryParams() {
            this.$refs.queryParamsRef.resetFields()
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.getOtherPersonList()
        },
        // 导出
        handleExport() {
            this.download(
                '/tbOtherPeople/tbOtherPeopleExport',
                {
                    ...this.queryParams,
                },
                `项目其他人员_${new Date().getTime()}.xlsx`,
            )
        },
    },
    created() {
        this.getOtherPersonList()
    },
}
</script>
