<template>
    <el-drawer :title="titleMap[mode]" :append-to-body="true" v-model="visible" :size="1000" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')" :with-header="false">
        <el-container class="flex-column" v-loading="loading">
            <div class="drawer-detail-main">
                <div class="drawer-detail-header">
                    <div class="drawer-detail-header-body">
                        <div class="drawer-detail-header-left">{{ titleMap[mode] }}</div>
                        <div class="drawer-detail-header-left">
                            <el-button type="default" icon="el-icon-close" @click="visible=false"></el-button>
                        </div>
                    </div>
                </div>
            </div>
            <el-main>
                <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
                    <fieldset>
                        <legend><el-tag>店铺信息</el-tag></legend>
                        <el-alert type="warning" title="已发货的订单用户发起退款后，开启退款自动拦截快递后系统将自动拦截途中快递并退回" :closable="false" style="margin-bottom: 10px;"></el-alert>
                        <el-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="所属代理" prop="agent_id">
                                    <el-input v-model="formData.agent_id" placeholder="所属代理" clearable readonly :style="{ width: '100%' }" @click="selectUser">
                                        <template #append>
                                            <el-tooltip
                                                effect="dark"
                                                content="点这里，清除选择"
                                                placement="top-start"
                                            >
                                            <div class="remove-a" @click="clearUser">清除</div>
                                            </el-tooltip>
                                        </template>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="来客店铺名称" prop="store_name">
                                    <el-input v-model="formData.store_name" clearable placeholder="来客店铺名称"></el-input>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="来客账户ID" prop="store_id">
                                    <el-input v-model="formData.store_id" clearable placeholder="来客账户ID"></el-input>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="自动呼叫快递" prop="is_auto">
                                    <el-select v-model="formData.is_auto" clearable placeholder="请选择" :style="{width: '100%'}">
                                        <el-option :value="1" label="开启"></el-option>
                                        <el-option :value="2" label="关闭"></el-option>
                                    </el-select>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="快递类型" prop="express_type">
                                    <el-select v-model="formData.express_type" clearable placeholder="请选择" :style="{width: '100%'}">
                                        <el-option :value="item.code" :label="item.name" v-for="item in expressType"></el-option>
                                    </el-select>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="呼叫快递时间" prop="express_time">
                                    <el-select v-model="formData.express_time" clearable placeholder="请选择" :style="{width: '100%'}">
                                        <el-option :value="(item-1)" :label="(item-1)==0?'实时':(item-1)+'点后'" v-for="item in 20"></el-option>
                                    </el-select>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="核销时机" prop="order_end">
                                    <el-select v-model="formData.order_end" clearable placeholder="请选择" :style="{width: '100%'}">
                                        <el-option :value="item.id" :label="item.name" v-for="item in orderEnd"></el-option>
                                    </el-select>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="退款自动拦截快递" prop="is_reback">
                                    <el-select v-model="formData.is_reback" clearable placeholder="请选择" :style="{width: '100%'}">
                                        <el-option :value="item.id" :label="item.name" v-for="item in orderReback"></el-option>
                                    </el-select>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="营业时间-开始" prop="service_at_start">
                                    <el-time-select
                                        v-model="formData.service_at_start"
                                        :style="{ width: '100%' }"
                                        start="00:15"
                                        step="00:15"
                                        end="23:55"
                                        placeholder="营业时间-开始"
                                    />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="营业时间-结束" prop="service_at_end">
                                    <el-time-select
                                        v-model="formData.service_at_end"
                                        :style="{ width: '100%' }"
                                        start="00:15"
                                        step="00:15"
                                        end="23:55"
                                        placeholder="营业时间-结束"
                                    />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="客服电话" prop="service_mobile">
                                    <el-input v-model="formData.service_mobile" clearable placeholder="请输入"></el-input>
                                    <div class="el-form-item-msg">在小程序选择门店中展现</div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </fieldset>
                    <fieldset>
                        <legend><el-tag>发货信息</el-tag></legend>
                        <el-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="联系人" prop="express_name">
                                    <el-input v-model="formData.express_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="express_mobile">
                                    <el-input v-model="formData.express_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="express_goods">
                                    <el-input v-model="formData.express_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="express_unit">
                                    <el-input v-model="formData.express_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="express_address">
                                    <el-input type="textarea" rows="5" v-model="formData.express_address" clearable placeholder="请输入"></el-input>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </fieldset>
                    <fieldset>
                        <legend><el-tag>开放平台信息</el-tag></legend>
                        <el-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="AppId" prop="appid">
                                    <el-input v-model="formData.appid" 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="secret">
                                    <el-input v-model="formData.secret" clearable show-password placeholder="请输入"></el-input>
                                    <div class="el-form-item-msg"></div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </fieldset>
                </el-form>
            </el-main>
            <el-footer style="text-align: right;">
                <el-button @click="visible=false" >取 消</el-button>
                <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
            </el-footer>
        </el-container>
    
    </el-drawer>
    <agentData ref="agentData" :multiple="false" @success="handleAgent"></agentData>
</template>
<script>
import agentData from "@/views/manage/components/agent";
export default {
    components: {
        agentData
    },
    data(){
        return {
            stepActive:1,
            loading: false,
            mode:"add",
            titleMap:{
                add:"新增店铺",
                edit:"编辑店铺信息"
            },
            orderEnd:[{"id":1,"name":"订单提交后"},{"id":2,"name":"快递取件后"},{"id":3,"name":"快递签收后"}],
            orderReback:[{"id":1,"name":"开启"},{"id":2,"name":"关闭"}],
            expressType:[],
            visible: false,
            isSaveing: false,
            formData:{},
            time:0,
            submitState:false,
            disabled:false,
            rules:{
                appid: [
                    {required: true, message: '请输入'}
                ],
                secret: [
                    {required: true, message: '请输入'}
                ],
                express_type: [
                    {required: true, message: '请选择'}
                ],
                is_auto: [
                    {required: true, message: '请选择'}
                ],
                service_mobile: [
                    {required: true, message: '请输入'}
                ],
                is_reback: [
                    {required: true, message: '请选择'}
                ],
                store_name: [
                    {required: true, message: '请输入'}
                ],
                service_at_start: [
                    {required: true, message: '请输入'}
                ],
                service_at_end: [
                    {required: true, message: '请输入'}
                ],
                store_id: [
                    {required: true, message: '请输入'}
                ],
                agent_id: [
                    {required: true, message: '请选择'}
                ],
                express_name: [
                    {required: true, message: '请输入'}
                ],
                express_mobile: [
                    {required: true, message: '请输入'}
                ],
                express_goods: [
                    {required: true, message: '请输入'}
                ],
                express_unit: [
                    {required: true, message: '请输入'}
                ],
                express_address: [
                    {required: true, message: '请输入'}
                ],
                express_time: [
                    {required: true, message: '请选择'}
                ],
                order_end: [
                    {required: true, message: '请选择'}
                ],
            }
        }
    },
    methods:{
        handleAgent(data){
            this.formData.agent_id = data.agent_id;
        },
        clearUser(){
            this.formData.agent_id = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.agentData.open()
            })
        },
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            this.getExpress();
            return this
        },
        async getExpress(){
            this.loading = true;
            var resp = await this.$API.express.page.get();
            this.loading = false;
            this.expressType = resp.data;
        },
        //表单注入数据
        setData(data){
            this.formData = JSON.parse(JSON.stringify(data));
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            let submitData = JSON.parse(JSON.stringify(this.formData));
            if (this.mode == 'add') {
                submitData.scene = "register";
                var resp = await this.$API.store.save.post(submitData);
            } else {
                var resp = await this.$API.store.edit.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 = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>
<style>
.mt20 {margin-top: 20px;}
.login-msg-yzm {display: flex;width: 100%;}
.login-msg-yzm .el-button {margin-left: 10px;--el-button-size:42px;}
</style>