<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            
            <el-form-item label="顾客编码" prop="appid">
                <el-input v-model="formData.appid" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg">顺丰开放平台获取</div>
            </el-form-item>
            <el-form-item label="校验码" prop="secret">
                <el-input v-model="formData.secret" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="月结账号" prop="account">
                <el-input v-model="formData.account" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
        </el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">提交</el-button>
		</template>
    </el-dialog>
</template>

<script>
export default{
    data(){
        return {
            loading: false,
            mode:"add",
            titleMap:{
                add:"顺丰开放平台授权",
                edit:"顺丰开放平台授权"
            },
            formData:{
                appid:"",
                secret:"",
                account:""
            },
            rules:{
                appid: [
                    {required: true, message: '请输入'}
                ],
                secret: [
                    {required: true, message: '请输入'}
                ],
                account: [
                    {required: true, message: '请输入'}
                ],
            },
            visible: false,
            isSaveing: false,
            agent_id:0
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            if (data.express.appid) {
                this.formData = data.express;
            }
            this.agent_id = data.agent_id;
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            var submitData = {};
            submitData.express = this.formData;
            submitData.agent_id = this.agent_id;
            var resp = await this.$API.agent.edit.post(submitData);
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>