<template>
<a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
                <!-- <a-col :span="24">
            <a-form-model-item
              label="餐桌名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="name"
            >
              <a-input
                v-model="model.name"
                placeholder="请输入餐桌名称"
              ></a-input>
            </a-form-model-item>
          </a-col> -->
                <a-col :span="24">
                    <a-form-model-item label="pos类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="posTypeId">
                        <a-select v-model="model.posTypeId" placeholder="请选择pos类型">
                            <a-select-option :value="item.id" v-for="item in posTypeList" :key="item.id">
                                {{ item.name }}
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-form-model-item label="桌型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="posTableTypeId">
                        <a-select v-model="model.posTableTypeId" placeholder="请选择pos类型">
                            <a-select-option :value="item.id" v-for="item in tableTypeList" :key="item.id">
                                {{ item.name }}
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-form-model-item label="餐桌区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="posRegionId">

                        <a-select v-model="model.posRegionId" placeholder="请选择pos类型">
                            <a-select-option :value="item.id" v-for="item in regionList" :key="item.id">
                                {{ item.name }}
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-form-model-item label="餐桌名前缀" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qz">
                        <a-input v-model="model.qz" placeholder="请输入餐桌名前缀" style="width: 100%" />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-form-model-item label="座位数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
                        <a-input-number v-model="model.num" placeholder="请输入座位数" style="width: 100%" />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-form-model-item label="餐桌序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tableNo">
                        <a-row>
                            <a-col :span="10">
                                <a-input v-model="model.tableNo" placeholder="开始序号"></a-input>
                            </a-col>
                            <a-col :span="2">
                                至
                            </a-col>
                            <a-col :span="10">
                                <a-input v-model="model.tableNoEnd" placeholder="结束序号"></a-input>
                            </a-col>
                        </a-row>
                        
                    </a-form-model-item>
                </a-col>

            </a-row>
        </a-form-model>
    </j-form-container>
</a-spin>
</template>

<script>
import {
    httpAction,
    getAction
} from "@/api/manage";
import {
    validateDuplicateValue
} from "@/utils/util";

export default {
    name: "PosTableForm",
    components: {},
    props: {
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false,
        },
    },
    data() {
        return {
            model: {},
            labelCol: {
                xs: {
                    span: 24
                },
                sm: {
                    span: 5
                },
            },
            wrapperCol: {
                xs: {
                    span: 24
                },
                sm: {
                    span: 16
                },
            },
            confirmLoading: false,
            validatorRules: {
                tenantId: [{
                    required: true,
                    message: "请输入关联租户!"
                }],
                qz: [{
                    required: true,
                    message: "请输入餐桌名前缀!"
                }],
                hotelId: [{
                    required: true,
                    message: "请输入关联酒店!"
                }],
                name: [{
                    required: true,
                    message: "请输入餐桌名称!"
                }],
                posTypeId: [{
                    required: true,
                    message: "请输入pos类型id!"
                }],
                posTableTypeId: [{
                    required: true,
                    message: "请输入pos桌型id!"
                }],
                posRegionId: [{
                    required: true,
                    message: "请输入pos餐桌区域id!"
                }],
                num: [{
                    required: true,
                    message: "请输入座位数!"
                }],
                tableNo: [{
                    required: true,
                    message: "请输入餐桌序号!"
                }],
                state: [{
                    required: true,
                    message: "请输入状态!"
                }],
            },
            url: {
                add: "/pos/posTable/add",
                edit: "/pos/posTable/edit",
                queryById: "/pos/posTable/queryById",
            },
            posTypeList: [],
            tableTypeList: [],
            regionList: []
        };
    },
    computed: {
        formDisabled() {
            return this.disabled;
        },
    },
    created() {
        var _info = JSON.parse(localStorage.getItem("storeInfo"));
        if (_info) {
            this.model.hotelId = _info.id;
        }
        //备份model原始值
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
        getAction("/pos/posType/list", {
            hotelId: _info.id,
            pageNo: 1,
            pageSize: 100,
        }).then((res) => {
            if (res.success) {
                this.posTypeList = res.result.records;
                // this.model.posTypeId = this.posTypeList[0].id;
            }
        });
        getAction("/pos/posTableType/list", {
            hotelId: _info.id,
            pageNo: 1,
            pageSize: 100,
        }).then((res) => {
            if (res.success) {
                this.tableTypeList = res.result.records;
            }
        });
        getAction("/pos/posRegion/list", {
            hotelId: _info.id,
            pageNo: 1,
            pageSize: 100,
        }).then((res) => {
            if (res.success) {
                this.regionList = res.result.records;
            }
        });
    },
    methods: {
        add() {
            this.edit(this.modelDefault);
        },
        edit(record) {
            this.model = Object.assign({}, record);
            this.visible = true;
        },
        submitForm() {
            const that = this;
            // 触发表单验证
            this.$refs.form.validate((valid) => {
                if (valid) {
                    // that.confirmLoading = true;
                    let httpurl = "";
                    let method = "";
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = "post";
                    } else {
                        httpurl += this.url.edit;
                        method = "put";
                    }
                    let arr = [];
                    let num = parseInt(this.model.tableNoEnd)
                    this.model.tableNo = parseInt(this.model.tableNo)
                    this.model.tableNoEnd = parseInt(this.model.tableNoEnd)

                    console.log(typeof num);
                    for(let i=0; i<num; i++){
                        // console.log(i);
                        arr.push({
                            hotelId: this.model.hotelId,
                            num: this.model.num,
                            posRegionId: this.model.posRegionId,
                            posTableTypeId: this.model.posTableTypeId,
                            posTypeId: this.model.posTypeId,
                            name: this.model.qz + (this.model.tableNo*1 + i*1),
                            tableNo: (this.model.tableNo*1 + i*1),
                        })
                    }
                    console.log(this.model);
                    console.log(arr);
                    // this.$emit("ok", arr);
                    httpAction('/pos/posTable/addBatch', arr, 'post')
                        .then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit("ok");
                            } else {
                                that.$message.warning(res.message);
                            }
                        })
                        .finally(() => {
                            that.confirmLoading = false;
                        });
                }
            });
        },
    },
};
</script>
