<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-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="店铺名称" prop="shop_name">
                                    <el-input v-model="formData.shop_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="联系人" prop="shop_contact">
                                    <el-input v-model="formData.shop_contact" 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="shop_mobile">
                                    <el-input v-model="formData.shop_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="营业时间">
                                    <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="提现手续费" prop="cash_rate">
                                    <el-input v-model="formData.cash_rate" clearable placeholder="请输入">
                                        <template #append>%</template>
                                    </el-input>
                                    <div class="el-form-item-msg">设置为10则为10%，意为每笔提现将收取提现金额的10%作为手续费</div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="店铺地址" prop="shop_address">
                                    <el-input v-model="formData.shop_address" 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="shop_notice">
                                    <el-input v-model="formData.shop_notice" type="textarea" clearable placeholder="请输入">
                                        <template #append>%</template>
                                    </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-form-item label="收费类型" prop="rate_type">
                            <el-radio-group v-model="formData.rate_type">
                                <el-radio border :label="1">收取年费</el-radio>
                                <el-radio border :label="2">订单金额抽佣</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="抽佣类型" prop="rate_radio" v-if="formData.rate_type==2">
                            <el-radio-group v-model="formData.rate_radio">
                                <el-radio border :label="1">固定金额</el-radio>
                                <el-radio border :label="2">金额比例</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="抽佣比例/金额" prop="rate_money" v-if="formData.rate_type==2">
                            <el-input v-model="formData.rate_money" clearable placeholder="请输入">
                                <template #append>{{formData.rate_radio==1?'元':'%'}}</template>
                            </el-input>
                            <div class="el-form-item-msg">如果类型为金额比例，比例设置10，则表示抽取每单金额的10%为系统佣金，最多不超过100</div>
                        </el-form-item>
                    </fieldset>
                </el-form>
            </el-main>
            <el-footer style="text-align: right;">
                <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>
            </el-footer>
        </el-container>
    </el-drawer>
</template>

<script>
export default {
    data(){
        return {
            loading: false,
            isSaveing: false,
            mode:"add",
            titleMap:{
                add:"新增店铺",
                edit:"编辑店铺信息"
            },
            visible:false,
            formData:{},
            rules:{
                shop_address: [
                    {required: true, message: '请输入'}
                ],
                rate_money: [
                    {required: true, message: '请输入'}
                ],
                rate_radio: [
                    {required: true, message: '请选择'}
                ],
                rate_type: [
                    {required: true, message: '请选择'}
                ],
                shop_name: [
                    {required: true, message: '请输入'}
                ],
                shop_contact: [
                    {required: true, message: '请输入'}
                ],
                shop_mobile: [
                    {required: true, message: '请输入'}
                ],
                shop_address: [
                    {required: true, message: '请输入'}
                ],
                start_at: [
                    {required: true, message: '请选择'}
                ],
                end_at: [
                    {required: true, message: '请选择'}
                ],
                username: [
                    {required: true, message: '请输入'}
                ],
                cash_rate: [
                    {required: true, message: '请输入'}
                ],
                password: [
                    {required: true, message: '请输入登录密码'}
                ],
                password2: [
                    {required: true, message: '请再次输入密码'},
                    {validator: (rule, value, callback) => {
                        if (value !== this.formData.password) {
                            callback(new Error('两次输入密码不一致!'));
                        }else{
                            callback();
                        }
                    }}
                ],
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        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') {
                var resp = await this.$API.shop.save.post(submitData);
            } else {
                var resp = await this.$API.shop.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>