<template>
    <div>
        <el-dialog :title="modalTitle" :visible.sync="visible" width="500px" :before-close="onClose">
            <el-form :model="queryParams" :rules="rules" ref="queryParamsCorpModify" label-position="left"
                label-width="100px">
                <el-form-item label="企业ID" prop="originCorpId">
                    <el-input v-model="queryParams.originCorpId" placeholder="请输入企业ID" clearable size="small" />
                </el-form-item>
                <el-form-item label="企业简称" prop="corpName">
                    <el-input v-model="queryParams.corpName" placeholder="请输入企业简称" clearable size="small" />
                </el-form-item>
                <el-form-item label="企业全称" prop="corpFullName">
                    <el-input v-model="queryParams.corpFullName" placeholder="请输入企业全称" clearable size="small" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="onClose()">取 消</el-button>
                <el-button type="primary" :loading="loading" @click="submit('queryParamsCorpModify')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { addCorpApi, editCorpApi } from "@/api/accounts/corpWeChat";
export default {
    data() {
        return {
            queryParams: {
                corpFullName: null,
                corpName: null,
                originCorpId: null
            },
            rules: {
                corpFullName: [
                    { required: true, message: '请输入企业全称', trigger: 'blur' }
                ],
                originCorpId: [
                    { required: true, message: '请输入企业ID', trigger: 'blur' }
                ],
                corpName: [
                    { required: true, message: '请输入企业简称', trigger: 'blur' }
                ]
            },
            loading: false
        }
    },
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
        modifyData: {
            type: Object,
            default: null
        },
        modalTitle: {
            type: String,
            default: '新增企业'
        }
    },
    watch: {
        modifyData: {
            handler(val) {
                if (val && Object.keys(val).length > 0) {
                    this.queryParams = val
                } else {
                    this.queryParams = {
                        corpFullName: null,
                        corpName: null,
                        originCorpId: null
                    }
                }
            },
            immediate: true,
        },
    },
    methods: {
        onClose() {
            this.$emit("onClose");
        },
        submit(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    let data = null
                    if (this.modifyData && this.modifyData.corpId) {
                        data = await editCorpApi({ ...this.queryParams, corpId: this.modifyData.corpId })
                    } else {
                        data = await addCorpApi(this.queryParams)
                    }

                    this.loading = false
                    if (data.data) {
                        this.$message({
                            message: (this.modifyData && this.modifyData.corpId) ? '修改成功' : '新增成功',
                            type: 'success'
                        });
                        this.resetForm(formName);
                        this.$emit("onChange");
                    }
                }
            })
        },

    }
}
</script>