<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :close-on-click-modal="false" :width="600" destroy-on-close @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="submitData" :rules="rules" label-width="100px" label-position="top" v-loading="loading">
            <el-form-item label="自动呼叫快递" prop="status">
                <el-radio-group v-model="submitData.status">
                    <el-radio-button border label="1">开启</el-radio-button>
                    <el-radio-button border label="2">关闭</el-radio-button>
                </el-radio-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <template v-if="submitData.status==1">
                <el-row :gutter="10">
                    <el-col :span="24">
                        <el-form-item label="快递公司" prop="type">
                            <el-select v-model="submitData.type" clearable placeholder="请选择" :style="{width: '100%'}">
                                <el-option value="sfexpress" label="顺丰速运"></el-option>
                            </el-select>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="快递类型" prop="rule.expressTypeId">
                            <el-select v-model="submitData.rule.expressTypeId" clearable placeholder="请选择" :style="{width: '100%'}">
                                <el-option value="1" label="顺丰特快"></el-option>
                                <el-option value="2" label="顺丰标快"></el-option>
                                <el-option value="201" label="冷运标快"></el-option>
                                <el-option value="150" label="标快D类"></el-option>
                                <el-option value="66" label="特快D类"></el-option>
                                <el-option value="283" label="填舱标快"></el-option>
                                <el-option value="247" label="电商标快"></el-option>
                                <el-option value="231" label="陆运包裹"></el-option>
                            </el-select>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="月结帐号" prop="rule.customId">
                            <el-input v-model="submitData.rule.customId" clearable placeholder="请输入"></el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </el-col>
                </el-row>
                <fieldset>
                    <legend><el-tag type="danger">收货地址信息</el-tag></legend>
                    <el-row :gutter="10">
                        <el-col :span="12">
                            <el-form-item label="联系人" prop="rule.name">
                                <el-input v-model="submitData.rule.name" clearable placeholder="请输入"></el-input>
                                <div class="el-form-item-msg">用于呼叫快递</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="联系电话" prop="rule.mobile">
                                <el-input v-model="submitData.rule.mobile" clearable placeholder="请输入"></el-input>
                                <div class="el-form-item-msg">用于呼叫快递</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="物品名称" prop="rule.goods">
                                <el-input v-model="submitData.rule.goods" clearable placeholder="请输入"></el-input>
                                <div class="el-form-item-msg">用于呼叫快递</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="物品单位" prop="rule.unit">
                                <el-input v-model="submitData.rule.unit" clearable placeholder="请输入"></el-input>
                                <div class="el-form-item-msg">用于呼叫快递</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="发货地址" prop="rule.address">
                                <el-input v-model="submitData.rule.address" clearable placeholder="请输入"></el-input>
                                <div class="el-form-item-msg">必须是真实地址</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </fieldset>
            </template>
        </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,
            isSaveing: false,
            mode:"add",
            titleMap:{
                add:"快递权限设置",
                edit:"快递权限设置"
            },
            visible:false,
            formData:{},
            submitData:{
                rule:{}
            },
            rules:{
                status: [
                    {required: true, message: '请选择'}
                ],
                type: [
                    {required: true, message: '请选择'}
                ],
                'rule.expressTypeId': [
                    {required: true, message: '请选择'}
                ],
                'rule.customId': [
                    {required: true, message: '请输入'}
                ],
                'rule.name': [
                    {required: true, message: '请输入'}
                ],
                'rule.mobile': [
                    {required: true, message: '请输入'}
                ],
                'rule.goods': [
                    {required: true, message: '请输入'}
                ],
                'rule.unit': [
                    {required: true, message: '请输入'}
                ],
                'rule.address': [
                    {required: true, message: '请输入'}
                ],
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = JSON.parse(JSON.stringify(data));
            this.getExpress()
        },
        async getExpress(){
            this.loading = true;
            var resp = await this.$API.store.express.get({"poi":this.formData.poi_id});
            this.loading = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg)
            }
            if (resp.data.return) {
                this.submitData = resp.data.return;
            } else {
                this.submitData = {rule:{}}
            }
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            let submitData = JSON.parse(JSON.stringify(this.submitData));
            submitData.poi_id = this.formData.poi_id;
            var resp = await this.$API.store.expressSave.post(submitData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {
                rule:{}
            };
            this.$emit("success");
        }
    }
}
</script>