<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            <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-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-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-form-item label="可发布商品类型" prop="product_type" v-loading="channelLoad">
                <el-checkbox-group v-model="formData.product_type">
                    <el-checkbox-button border :value="item.key" :label="item.key" v-for="(item,indx) in cateData" :key="indx">{{ item.name }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="到期时间" prop="vip_end">
                <el-date-picker v-model="formData.vip_end" :style="{width: '100%'}" placeholder="请选择" />
            </el-form-item>
            <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-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:{
                service_mobile: [
                    {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;}
</style>