<template>
    <!-- 公司业绩管理 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="login" label-width="0px" class="ms-content">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-form-item>
                        <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
                            v-model="timeValue"
                            type="daterange"
                            range-separator="~"
                            style="width: 100%"
                            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="getCompanyPerformanceList">查询</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="getCompanyPerformanceList"
        />
        <DialogModel :dialogConfig="dialogConfig" @closeDialogOuter="closeDialogOuter">
            <template slot="outerContent">
                <AddAndEditForm :formType="formType" @closeDialogOuter="closeDialogOuter" :editRow="editRow" />
            </template>
        </DialogModel>
    </div>
</template>

<script>
import DialogModel from '@/components/DialogModel/index'
import AddAndEditForm from './components/addAndEditForm.vue'
import { getCompanyPerformanceListAPI, deleteCompanyPerformanceAPI } from '@/api/company-manage/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: '起止时间', isSlot: true },
                { label: '竣工日期', prop: 'stopTime' },
                { label: '变电站数量', prop: 'stationNum' },
                { label: '线路建设规模(折单公里)', prop: 'lineScale' },
                { label: '承包范围', prop: 'contractRang' },
                { label: '业主单位', prop: 'ownerUnit' },
                { label: '业主单位联系方式', prop: 'ownerPhone' },
                { label: '关键人员', prop: 'keyUser', isSlot: true },
            ],
        }
    },
    methods: {
        // 关闭
        closeDialogOuter(isRefresh = false) {
            this.dialogConfig.outerVisible = false
            if (isRefresh) {
                this.getCompanyPerformanceList()
            }
        },
        // 删除
        onHandleDelete(row) {
            this.$modal
                .confirm('确定删除该业绩吗？')
                .then(async () => {
                    const res = await deleteCompanyPerformanceAPI({ id: row.id })
                    if (res.code === 200) {
                        this.$modal.msgSuccess('删除成功')
                        this.getCompanyPerformanceList()
                    }
                })
                .catch(() => {})
        },
        // 新增或编辑
        onHandleAddOrEdit(row, type) {
            this.formType = type
            if (type === 1) {
                this.editRow = {}
                this.dialogConfig.outerTitle = '新增业绩'
            } else if (type === 3) {
                this.dialogConfig.outerTitle = '编辑业绩'
            } else if (type === 2) {
                this.dialogConfig.outerTitle = '业绩详情'
            }

            if (type === 2 || type === 3) {
                const {
                    proName,
                    voltage,
                    startTime, // 开始时间
                    endTime, // 结束时间
                    stopTime, // 竣工日期
                    stationNum, // 变电站座数
                    lineScale, // 线路建设规模
                    contractRang, // 承包范围
                    ownerUnit, // 业主单位
                    ownerPhone, // 业主联系方式
                    tbCompanyPerfRelList, // 关键人员
                    tbFileSourceVoList, // 附件
                    id, // 主键id
                } = row
                Object.assign(this.editRow, {
                    proName,
                    voltage,
                    startTime, // 开始时间
                    endTime, // 结束时间
                    stopTime, // 竣工日期
                    stationNum, // 变电站座数
                    lineScale, // 线路建设规模
                    contractRang, // 承包范围
                    ownerUnit, // 业主单位
                    ownerPhone, // 业主联系方式
                    tbCompanyPerfRelList,
                    tbFileSourceVoList,
                    id,
                })
            }
            this.dialogConfig.outerVisible = true
        },
        async getCompanyPerformanceList() {
            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 getCompanyPerformanceListAPI(this.queryParams)
            this.tableList = res.rows
            this.total = res.total
        },

        // 重置按钮
        resetQueryParams() {
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.timeValue = []
            this.queryParams = {
                proName: '',
                startTime: '',
                endTime: '',
            }
            this.getCompanyPerformanceList()
        },
        // 导出
        handleExport() {
            this.download(
                '/tbCompanyPerf/tbCompanyPerfExport',
                {
                    ...this.queryParams,
                },
                `公司业绩管理_${new Date().getTime()}.xlsx`,
            )
        },
    },
    created() {
        this.getCompanyPerformanceList()
    },
}
</script>
