<template>
    <el-container>
        <el-main v-loading="isLoading">
            <el-card shadow="never" header="店铺设置" class="borderNone">
                <div style="width: 50%;">
                    <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
                        <fieldset>
                            <legend><el-tag>店铺信息</el-tag></legend>
                            <el-row :gutter="15">
                                <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="12">
                                    <el-form-item label="来客账户ID" prop="store_id">
                                        <el-input disabled="true" 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="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-col :span="6">
                                    <el-form-item label="核销形式" prop="service_type">
                                        <el-select v-model="formData.service_type" 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-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>
                    </el-form>
                </div>
            </el-card>
        </el-main>
        <el-footer>
            <el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
        </el-footer>
    </el-container>
</template>

<script>

export default {
    data() {
        return {
            isSaveing: false,
            isLoading:false,
            formData:{},
            orderEnd:[{"id":1,"name":"订单提交后"},{"id":2,"name":"快递取件后"},{"id":3,"name":"快递签收后"}],
            orderReback:[{"id":1,"name":"开启"},{"id":2,"name":"关闭"}],
            expressType:[],
            rules:{
                express_type: [
                    {required: true, message: '请选择'}
                ],
                is_auto: [
                    {required: true, message: '请选择'}
                ],
                service_type: [
                    {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: '请输入'}
                ],
                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: '请选择'}
                ],
            }
        }
    },
    created(){
        this.getConfig()
    },
    methods:{
        async getExpress(){
            this.loading = true;
            var resp = await this.$API.express.page.get();
            this.loading = false;
            this.expressType = resp.data;
        },
        async getConfig(){
            this.isLoading = true;
            var resp = await this.$API.storeShop.detail.get();
            this.isLoading = false;
            if (resp.code !== 1) {
                return this.$message.error(resp.msg)
            }
            this.formData = resp.data;
            this.getExpress()
        },
        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));
            var resp = await this.$API.storeShop.save.post(submitData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
        }
    }
}
</script>