<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="660" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            <el-form-item label="所属门店" prop="store_id">
                <el-input v-model="formData.store_id" clearable placeholder="请选择门店" readonly :style="{ width: '100%' }" @click="selectUser">
                    <template #append>
                        <el-tooltip
                            effect="dark"
                            content="点这里，清除选择"
                            placement="top-start"
                        >
                        <div class="remove-a" @click="clearUser">清除</div>
                        </el-tooltip>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item label="项目名称" prop="name">
                <el-input v-model="formData.name" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg">如：精洗、普洗、修复</div>
            </el-form-item>
            <el-form-item label="加价金额" prop="price">
                <el-input v-model="formData.price" clearable placeholder="请输入">
                    <template #append>元</template>
                </el-input>
                <div class="el-form-item-msg">设置加价金额后，每一件需洗涤的衣服按该金额加价</div>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio-button :label="1">正常</el-radio-button>
                    <el-radio-button :label="0">禁用</el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button type="primary" :loading="isSaveing" @click="submit()">提交</el-button>
        </template>
    </el-dialog>
    <storeData ref="storeData" :multiple="false" @success="handleSuccess"></storeData>
</template>

<script>
import storeData from "@/views/manage/components/store";
export default {
    components: {
        storeData
    },
    data(){
        return {
            loading: false,
            mode:"add",
            titleMap:{
                add:"新增洗护项目",
                edit:"编辑洗护项目"
            },
            rules:{
                store_id: [
                    {required: true, message: '请选择'}
                ],
                name: [
                    {required: true, message: '请输入'}
                ],
                price: [
                    {required: true, message: '请输入'}
                ],
                status: [
                    {required: true, message: '请选择'}
                ],
            },
            visible: false,
            isSaveing: false,
            formData:{},
        }
    },
    methods:{
        handleSuccess(data){
            this.formData.agent_id = data.agent_id;
            this.formData.store_id = data.store_id;
        },
        clearUser(){
            this.formData.agent_id = "";
            this.formData.store_id = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.storeData.open()
            })
        },
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = data;
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            let submitData = JSON.parse(JSON.stringify(this.formData));
            this.isSaveing = true;
            var resp = await this.$API.washType.save.post(submitData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.formData = {};
            this.visible = false;
            this.$emit("success");
        }
    }
}
</script>
