<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" placeholder="请选择商家" clearbale readonly :style="{width:'100%'}" @click="selectStore">
                    <template #append>
                        <el-tooltip effect="dark" content="点这里，清除选择" placement="top-start">
                            <div class="remove-a" @click="clearStore">清除</div>
                        </el-tooltip>
                    </template>
                </el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="保证金余额" v-if="formData.poi_id">
                <el-input v-model="formData.deposit" :disabled="true" readonly></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="操作类型" prop="type">
                <el-select v-model="formData.type" clearable placeholder="请选择" :style="{width: '100%'}">
                    <el-option :value="1" label="充值"></el-option>
                    <el-option :value="2" label="商责赔付扣款"></el-option>
                    <el-option :value="3" label="商责赔付退款"></el-option>
                    <el-option :value="4" label="商责扣罚扣款"></el-option>
                    <el-option :value="5" label="商责扣罚退款"></el-option>
                    <el-option :value="6" label="保证金退回"></el-option>
                </el-select>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="操作金额" prop="money">
                <el-input v-model="formData.money" clearable placeholder="请输入">
                    <template #append>元</template>
                </el-input>
                <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>
    <storeData ref="storeData" :multiple="false" @success="handleStore"></storeData>
</template>

<script>
import storeData from '@/views/manage/components/agent';
export default {
    components: {
        storeData
    },
    data(){
        return {
            loading: false,
            isSaveing: false,
            mode:"add",
            titleMap:{
                add:"保证金操作",
                edit:"保证金操作"
            },
            visible:false,
            formData:{
                type:1
            },
            rules:{
                type: [
                    {required: true, message: '请选择'}
                ],
                poi_name: [
                    {required: true, message: '请选择'}
                ],
                money: [
                    {required: true, message: '请输入'}
                ]
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = JSON.parse(JSON.stringify(data));
        },
        selectStore(){
            this.$nextTick(()=>{
                this.$refs.storeData.open()
            })
        },
        handleStore(data){
            this.formData.poi_id = data.poi_id;
            this.formData.poi_name = data.poi_name;
            this.formData.deposit = this.$TOOL.money(data.deposit);
        },
        clearStore(){
            this.formData.poi_id = "";
            this.formData.poi_name = "";
            this.formData.deposit = "";
        },
        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.deposit.save.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 = {type:1};
            this.$emit("success");
        }
    }
}
</script>

<style>
.text-center{text-align: center;}
</style>