<template>
    <!-- 公司业绩管理 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="queryParamsRef" label-width="0px" class="ms-content">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-form-item prop="proName">
                        <el-input v-model="queryParams.proName" placeholder="请输入项目名称" clearable> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item>
                        <el-date-picker
                            style="width: 100%"
                            v-model="timeValue"
                            type="daterange"
                            range-separator="~"
                            value-format="yyyy-MM-dd"
                            end-placeholder="结束时间"
                            start-placeholder="开始时间"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item>
                        <el-button type="primary" @click="getSubPerformanceManageList">查询</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 style="width: 100%">
            <el-table-column label="序号" width="55" type="index" />
            <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 === '开竣工日期'"> {{ row.startTime }}~{{ row.endTime }} </template>
                        <template v-if="item.label === '派遣人员'">
                            <span v-if="row.tbCompanyPerfRelList.length > 0">
                                {{ row.tbCompanyPerfRelList.length }} 人
                            </span>
                            <span v-else> - </span>
                        </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
                        style="color: #67c23a"
                        icon="el-icon-view"
                        type="text"
                        size="mini"
                        @click="onHandleAddOrEdit(row, 2)"
                    >
                        详情
                    </el-button>
                    <el-button
                        style="color: #409eff"
                        icon="el-icon-edit"
                        type="text"
                        size="mini"
                        @click="onHandleAddOrEdit(row, 3)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        style="color: #f56c6c"
                        icon="el-icon-delete"
                        type="text"
                        size="mini"
                        @click="onHandleDelete(row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getSubPerformanceManageList"
        />
        <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 { getSubPerformanceManageListAPI, deleteSubPerformanceManageAPI } from '@/api/sub-manage/sub-performance-manage'
export default {
    components: {
        DialogModel,
        AddAndEditForm,
    },
    data() {
        return {
            formType: 1, // 1新增 2详情 3编辑
            total: 0, // 总条数
            editRow: {},
            dialogConfig: {
                outerTitle: '新增',
                innerTitle: false,
                outerWidth: '80%',
                outerVisible: false,
                innerVisible: false,
            },
            queryParams: {
                proName: '', //项目名称
                startTime: '',
                endTime: '',
                pageNum: 1,
                pageSize: 10,
            },
            timeValue: [],
            tableList: [],
            tableColumn: [
                { label: '项目名称(按合同)', prop: 'proName' },
                { label: '电压等级', prop: 'voltage' },
                { label: '建设单位', prop: 'consUnit' },
                { label: '分包合同额(万元)', prop: 'money' },
                { label: '开竣工日期', isSlot: true },
                { label: '合同身份', prop: 'htRemark' },
                { label: '派遣人员', isSlot: true },
            ],
        }
    },
    methods: {
        // 关闭
        closeDialogOuter(isRefresh = false) {
            this.dialogConfig.outerVisible = false
            if (isRefresh) {
                this.getSubPerformanceManageList()
            } else {
                this.$refs.addAndEditFormRef.deleteFileListFun()
            }
        },
        // 删除
        onHandleDelete(row) {
            this.$modal
                .confirm('确定删除该业绩吗？')
                .then(async () => {
                    const res = await deleteSubPerformanceManageAPI({ id: row.id })
                    if (res.code === 200) {
                        this.$modal.msgSuccess('删除成功')
                        this.getSubPerformanceManageList()
                    }
                })
                .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 {
                    subId, // 所属分包商
                    proName, // 项目名称
                    consUnit, // 项目建设单位
                    htRemark, // 合同身份
                    money, // 分包商合同额
                    voltage, // 电压等级
                    startTime, // 开始时间
                    endTime, // 结束时间
                    tbCompanyPerfRelList,
                    tbFileSourceVoList,
                    id, // 主键id
                } = row
                Object.assign(this.editRow, {
                    subId, // 所属分包商
                    proName, // 项目名称
                    consUnit, // 项目建设单位
                    htRemark, // 合同身份
                    money, // 分包商合同额
                    voltage, // 电压等级
                    startTime, // 开始时间
                    endTime, // 结束时间
                    tbCompanyPerfRelList,
                    tbFileSourceVoList,
                    id, // 主键id
                })
            }

            this.dialogConfig.outerVisible = true
        },
        async getSubPerformanceManageList() {
            if (this.timeValue.length > 0) {
                this.queryParams.startTime = this.timeValue[0]
                this.queryParams.endTime = this.timeValue[1]
            } else {
                this.queryParams.startTime = ''
                this.queryParams.endTime = ''
            }
            const res = await getSubPerformanceManageListAPI(this.queryParams)
            this.tableList = res.rows
            this.total = res.total
        },
        resetQueryParams() {
            this.timeValue = []
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.queryParams.proName = ''
            this.getSubPerformanceManageList()
        },

        // 导出
        handleExport() {
            this.download(
                '/tbSubPerf/tbSubPerfExport',
                {
                    ...this.queryParams,
                },
                `分包商业绩管理_${new Date().getTime()}.xlsx`,
            )
        },
    },
    created() {
        this.getSubPerformanceManageList()
    },
}
</script>
