<template>
    <div v-loading="loading" element-loading-text="拼命加载中......请稍等">
        <el-row>
            <el-col :span="14">
                <el-form :model="form" :rules="rules" ref="refForm" label-width="180px">
                    <el-form-item>
                        <span style="font-weight:bold;font-size:28px;margin-right:20px">编辑服务商信息</span>
                        <el-link type="primary" @click="onRefresh">刷新</el-link>
                    </el-form-item>
                    <el-form-item label="是否有效：" prop="enable">
                        <el-switch v-model="form.enable"> </el-switch>
                    </el-form-item>
                    <el-form-item label="服务商名称：" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="渠道标识代码：" prop="channelSign">
                        <el-input v-model="form.channelSign"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="联系人姓名：" prop="contactName">
                                <el-input v-model="form.contactName"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="联系人电话：" prop="contactPhone">
                                <el-input v-model="form.contactPhone"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="服务商地址：" prop="address">
                        <el-input type="textarea" :rows="5" v-model="form.address" placeholder="请输入服务商地址"></el-input>
                    </el-form-item>
                    <el-form-item label="企业邮箱：" prop="email">
                        <el-input v-model="form.email" style="width:100%"></el-input>
                    </el-form-item>
                    <el-form-item label="服务商指定开票商品的企业：" prop="invoiceEntTaxId">
                        <el-input v-model="form.invoiceEntTaxId"></el-input>
                    </el-form-item>
                    <br>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit('refForm')">立即保存</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

    </div>
</template>

<script>
import USER_MANAGEMENT from "@/api/user-management";

export default {
    name: "ISPInfo",
    data() {
        return {
            loading: false,
            form: {
                ispId: "", // 服务商信息id
                channelSign: "", // 渠道标识代码
                enable: "", // 是否有效
                name: "", // 服务商名称
                contactPhone: "", // 联系人电话
                contactName: "", // 联系人姓名
                address: "", // 服务商地址
                email: "", // 邮箱
                invoiceEntTaxId: "", // 服务商指定的开票企业
            },
            rules: {
                enable: [
                    {
                        required: true,
                        message: "是否启用",
                        trigger: "change",
                    },
                ],
                name: [
                    {
                        required: true,
                        message: "请输入服务商名称",
                        trigger: "change",
                    },
                ],
                channelSign: [
                    {
                        required: true,
                        message: "请输入渠道标识代码",
                        trigger: "change",
                    },
                ],
                contactPhone: [
                    {
                        required: true,
                        message: "请输入联系人电话",
                        trigger: "change",
                    },
                ],
                contactName: [
                    {
                        required: true,
                        message: "请输入联系人姓名",
                        trigger: "change",
                    },
                ],
                contactPhone: [
                    {
                        required: true,
                        message: "请输入企业联系电话",
                        trigger: "change",
                    },
                ],
                email: [
                    {
                        required: true,
                        message: "请输入邮箱",
                        trigger: "change",
                    },
                ],
                invoiceEntTaxId: [
                    {
                        required: true,
                        message: "请输入服务商指定的开票企业",
                        trigger: "change",
                    },
                ],
            },
        };
    },
    mounted() {
        this.callGetISPInfo();
    },
    methods: {
        callGetISPInfo() {
            let _this = this;
            _this.loading = true;
            USER_MANAGEMENT.getISPInfo()
                .then((val) => {
                    _this.form.ispId = val.data.ispId;
                    _this.form.channelSign = val.data.channelSign;
                    _this.form.enable = val.data.enable;
                    _this.form.name = val.data.name;
                    _this.form.contactPhone = val.data.contactPhone;
                    _this.form.contactName = val.data.contactName;
                    _this.form.address = val.data.address;
                    _this.form.email = val.data.email;
                    _this.form.invoiceEntTaxId = val.data.invoiceEntTaxId;
                })
                .catch((err) => {
                    _this.$message.error(err);
                })
                .finally(() => {
                    _this.loading = false;
                });
        },
        onRefresh() {
            this.callGetISPInfo();
        },
        onSubmit(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true;
                    USER_MANAGEMENT.updateISPInfo({
                        name: _this.form.name,
                        contactPhone: _this.form.contactPhone,
                        contactName: _this.form.contactName,
                        email: _this.form.email,
                        enable: _this.form.enable,
                        channelSign: _this.form.channelSign,
                        invoiceEntTaxId: _this.form.invoiceEntTaxId,
                        address: _this.form.address,
                        ispId: _this.form.ispId,
                    })
                        .then((val) => {
                            _this.$message.success(val.msg);
                            location.reload();
                        })
                        .catch((error) => {
                            _this.$message.error(error);
                        })
                        .finally(() => {
                            _this.loading = false;
                        });
                } else {
                    _this.$message.error(
                        "输入信息不合法，请检查您的输入信息！"
                    );
                }
            });
        },
    },
};
</script>
