
<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="店铺名称">
                <el-input v-model="parentData.poi_name" disabled readonly clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="开通品类" prop="category" v-loading="categoryLoad">
                <el-cascader style="width: 100%;" v-model="formData.category" :options="categoryData" :props="props" clearable placeholder="请选择"></el-cascader>
                <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>
		<template #footer>
            <el-button @click="visible=false" size="large">取 消</el-button>
            <el-button size="large" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            loading: false,
            visible: false,
            isSaveing: false,
            categoryLoad:true,
            mode:"add",
            titleMap:{
                add:"开通新品类"
            },
            formData:{},
            parentData:{},
            props:{
                multiple: true,
                value:"category_id",
                label:"name"
            },
            categoryData:[],
            rules:{
                category: [
                    {required: true, message: '请选择'}
                ],
                product_type: [
                    {required: true, message: '请选择'}
                ],
            },
            cateData:[],
            channelLoad:true,
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.parentData = JSON.parse(JSON.stringify(data));
            this.getCate()
        },
        async getProductType(){
            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 getCate(){
            this.categoryLoad = true;
            var resp = await this.$API.category.list.get()
            this.categoryLoad = false;
            if (resp.code == 0) {
                this.$message.error(resp.msg)
                this.visible = false;
                return ;
            }
            this.categoryData = resp.data;
            this.getProductType()
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            this.formData.poi_id = this.parentData.poi_id;
            var resp = await this.$API.store.cate.post(this.formData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.$emit("success");
        }
    }
}
</script>