<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.subName" placeholder="请输入分包商名称" clearable> </el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="18">
                    <el-form-item>
                        <el-button type="primary" @click="getSubManageList">查询</el-button>
                        <el-button @click="resetQueryParam">重置</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"
            />

            <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="getSubManageList"
        />
        <DialogModel :dialogConfig="dialogConfig" @closeDialogOuter="closeDialogOuter">
            <template slot="outerContent">
                <AddAndEditForm
                    :formType="formType"
                    ref="addAndEditFormRef"
                    :editRow="editRow"
                    @closeDialogOuter="closeDialogOuter"
                />
            </template>
        </DialogModel>
    </div>
</template>

<script>
import DialogModel from '@/components/DialogModel/index'
import AddAndEditForm from './components/addAndEditForm.vue'
import { getSubManageListAPI, deleteSubManageAPI } from '@/api/sub-manage/sub-manage'
export default {
    components: {
        DialogModel,
        AddAndEditForm,
    },
    data() {
        return {
            formType: 1, // 1新增 2详情 3编辑
            dialogConfig: {
                outerTitle: '新增',
                innerTitle: false,
                outerWidth: '80%',
                outerVisible: false,
                innerVisible: false,
            },
            queryParams: {
                subName: '',
                pageNum: 1,
                pageSize: 10,
            },
            timeValue: [],
            tableList: [], // 列表
            tableColumn: [
                { label: '分包商名称', prop: 'subName' },
                { label: '负责人', prop: 'userName' },
                { label: '联系方式', prop: 'userPhone' },
                { label: '公司地址', prop: 'address' },
            ],
            editRow: {},
            total: 0,
        }
    },
    methods: {
        // 关闭
        closeDialogOuter(isRefresh = false) {
            this.dialogConfig.outerVisible = false
            if (isRefresh) {
                this.getSubManageList()
            } else {
                this.$refs.addAndEditFormRef.deleteFileListFun()
            }
        },
        // 删除
        onHandleDelete(row) {
            this.$modal
                .confirm('确定删除该分包商吗？')
                .then(async () => {
                    const res = await deleteSubManageAPI({ id: row.id })
                    if (res.code === 200) {
                        this.$modal.msgSuccess('删除成功')
                        this.getSubManageList()
                    }
                })
                .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 { subName, address, userName, userPhone, id, tbFileSourceVoList } = row
                Object.assign(this.editRow, {
                    subName,
                    address,
                    userName,
                    userPhone,
                    id,
                    tbFileSourceVoList,
                })
            }

            this.dialogConfig.outerVisible = true
        },
        // 获取分包商列表
        async getSubManageList() {
            const res = await getSubManageListAPI(this.queryParams)
            this.tableList = res.rows
            this.total = res.total
        },
        // 重置
        resetQueryParam() {
            this.queryParams.subName = ''
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.getSubManageList()
        },

        // 导出
        handleExport() {
            this.download(
                '/tbSub/tbSubExport',
                {
                    ...this.queryParams,
                },
                `分包商管理_${new Date().getTime()}.xlsx`,
            )
        },
    },
    created() {
        this.getSubManageList()
    },
}
</script>
