<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="800" :close-on-click-modal="false" destroy-on-close @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            
            <el-row :gutter="10">
                <el-col :span="6">
                    <el-form-item label="" prop="poi_logo" class="border">
                        <sc-upload v-model="formData.poi_logo" title="店铺LOGO"></sc-upload>
                        <div class="el-form-item-msg text-center">小程序前端展示</div>
                    </el-form-item>
                </el-col>
                <el-col :span="18">
                    <el-form-item label="店铺名称" prop="poi_name">
                        <el-input v-model="formData.poi_name" disabled readonly clearable placeholder="请输入"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                    <el-row :gutter="10">
                        <el-col :span="12">
                            <el-form-item label="到期时间" prop="vip_end">
                                <el-date-picker v-model="formData.vip_end" style="width: 100%" placeholder="请选择" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <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>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="营业时间">
                        <el-row :gutter="10">
                            <el-col :span="11">
                                <el-time-select v-model="formData.start_at" start="00:00" step="00:01" end="23:59" placeholder="请选择"  style="width: 100%"/>
                            </el-col>
                            <el-col :span="2" class="text-center">
                                <span class="text-gray-500">-</span>
                            </el-col>
                            <el-col :span="11">
                                <el-time-select v-model="formData.end_at" start="00:00" step="00:01" end="23:59" placeholder="请选择"  style="width: 100%"/>
                            </el-col>
                        </el-row>
                        <div class="el-form-item-msg">营业时间为必选，否则小程序端无法下单</div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="可核销时段">
                        <el-row :gutter="10">
                            <el-col :span="11">
                                <el-time-select v-model="formData.order_start" start="00:00" step="00:01" end="23:59" placeholder="请选择"  style="width: 100%"/>
                            </el-col>
                            <el-col :span="2" class="text-center">
                                <span class="text-gray-500">-</span>
                            </el-col>
                            <el-col :span="11">
                                <el-time-select v-model="formData.order_end" start="00:00" step="00:01" end="23:59" placeholder="请选择"  style="width: 100%"/>
                            </el-col>
                        </el-row>
                        <div class="el-form-item-msg">默认为09:00 - 22:30</div>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="12">
                    <el-form-item label="团购结算费率" prop="life_rate">
                        <el-input v-model="formData.life_rate" clearable placeholder="请输入">
                            <template #append>%</template>
                        </el-input>
                        <div class="el-form-item-msg">来客结算费率</div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="补差价结算费率" prop="mini_rate">
                        <el-input v-model="formData.mini_rate" clearable placeholder="请输入">
                            <template #append>%</template>
                        </el-input>
                        <div class="el-form-item-msg">小程序结算费率</div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="核销方式" prop="trade_type">
                        <el-select v-model="formData.trade_type" style="width: 100%;">
                            <el-option :value="1" label="立即核销"></el-option>
                            <el-option :value="2" label="物流核销"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="formData.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>
                </el-col>
            </el-row>
        </el-form>
		<template #footer>
            <el-button @click="visible=false" size="large">取 消</el-button>
            <el-button v-if="mode!='show'" size="large" 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:"编辑店铺信息"
            },
            channelLoad:false,
            visible:false,
            formData:{},
            cateData:[],
            rules:{
                poi_logo: [
                    {required: true, message: '请上传'}
                ],
                trade_type: [
                    {required: true, message: '请选择'}
                ],
                service_mobile: [
                    {required: true, message: '请输入'}
                ],
                life_rate: [
                    {required: true, message: '请输入'}
                ],
                mini_rate: [
                    {required: true, message: '请输入'}
                ],
                start_at: [
                    {required: true, message: '请选择'}
                ],
                end_at: [
                    {required: true, message: '请选择'}
                ],
                vip_end: [
                    {required: true, message: '请选择'}
                ],
                product_type: [
                    {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.getCate()
        },
        async getCate(){
            this.channelLoad = true;
            var resp = await this.$API.store.cate.get();
            this.channelLoad = false;
            if (resp.code == 0 ) {
                return this.$message.error(resp.message)
            }
            this.cateData = resp.data.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.formData.vip_end) {
                submitData.vip_end = this.$TOOL.dateFormat(this.formData.vip_end, "yyyy-MM-dd");
            }
            if (this.mode == 'add') {
                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>
.text-center{text-align: center;}
.border{border: 1px solid var(--el-disabled-border-color);}
</style>