<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="加价金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
                        <a-input-number v-model="model.money" placeholder="请输入加价金额" style="width: 100%" />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="">
                        <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
                    </a-form-model-item>
                </a-col>

                <a-col :span="24">
                    <a-form-model-item label="是否开启" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
                        <a-switch v-model="model.state" checked-children="启用" un-checked-children="禁用" />
                    </a-form-model-item>
                </a-col>

                <a-col :span="24">
                    <a-form-model-item label="应用范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
                        <a-radio-group :options="options" v-model="model.type" @change="onChange" />
                    </a-form-model-item>
                </a-col>
            </a-row>

            <a-col :span="24" v-if="model.type==1">
                <a-button @click="handleAddClass" type="primary" icon="plus">选择分类</a-button>
                <a-table :columns="columnsTwo" :data-source="model.detailListClass || []" :pagination="false" rowKey="id">
                    <template slot="num" slot-scope="text, record, index">
                        <div>
                            <a-input-number v-model="record.num" :min="1" :max="99" />
                        </div>
                    </template>
                    <template slot="required" slot-scope="text, record, index">
                        <div>
                            <a-switch v-model="record.required" />
                        </div>
                    </template>

                    <span slot="action" slot-scope="text, record, index">
                        <a @click="handleDeleteClass(index)">删除</a>
                    </span>
                </a-table>
            </a-col>

            <a-col :span="24" v-if="model.type==2">
                <a-button @click="handleAdd" type="primary" icon="plus">选择商品</a-button>
                <a-table :columns="columns" :data-source="model.detailList || []" :pagination="false" rowKey="id">
                    <template slot="num" slot-scope="text, record, index">
                        <div>
                            <a-input-number v-model="record.num" :min="1" :max="99" />
                        </div>
                    </template>
                    <template slot="required" slot-scope="text, record, index">
                        <div>
                            <a-switch v-model="record.required" />
                        </div>
                    </template>

                    <span slot="action" slot-scope="text, record, index">
                        <a @click="handleDelete(index)">删除</a>
                    </span>
                </a-table>
            </a-col>
        </a-form-model>
    </j-form-container>
    <select-goods-modal ref="modalForm2" @ok="modalFormOk"></select-goods-modal>
    <classificationModal ref="modalForm3" @ok="modalFormOkClass" />
</a-spin>
</template>

<script>
import {
    httpAction,
    getAction
} from "@/api/manage";
import {
    validateDuplicateValue
} from "@/utils/util";
import SelectGoodsModal from "./SelectGoodsModal";
import classificationModal from './classificationModal.vue';
const columns = [{
        title: "商品名称",
        dataIndex: "name",
    },
    {
        title: "商品条码",
        dataIndex: "barCode",
    },
    {
        title: "库存",
        dataIndex: "inventory",
    },
    {
        title: "操作",
        dataIndex: "action",
        align: "center",
        fixed: "right",
        width: 70,
        scopedSlots: {
            customRender: "action"
        },
    },
];

const columnsTwo = [{
        title: "分类名称",
        dataIndex: "name",
    },
    {
        title: "分类编号",
        dataIndex: "id",
    },
    {
        title: "操作",
        dataIndex: "action",
        align: "center",
        fixed: "right",
        width: 70,
        scopedSlots: {
            customRender: "action"
        },
    },
]
export default {
    name: "PosTableForm",
    components: {
        SelectGoodsModal,
        classificationModal
    },
    props: {
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false,
        },
    },
    data() {
        return {
            //分类弹窗
            classification: false,

            columns,
            columnsTwo,
            options: [{
                    label: '全部菜品',
                    value: 0
                },
                {
                    label: '菜品分类',
                    value: 1
                },
                {
                    label: '菜品',
                    value: 2
                },
            ],
            value: 1,
            model: {
                state: false,
                type: 0,
                detailList: [],
                detailListClass:[],
                remark: "",
                id: ''
            },
            labelCol: {
                xs: {
                    span: 24
                },
                sm: {
                    span: 5
                },
            },
            wrapperCol: {
                xs: {
                    span: 24
                },
                sm: {
                    span: 16
                },
            },
            confirmLoading: false,
            validatorRules: {
                name: [{
                    required: true,
                    message: "请输入口味名称!"
                }],
                money: [{
                    required: true,
                    message: "请输入金额!"
                }],
                state: [{
                    required: true,
                    message: "请选择是否开启!"
                }],
                type: [{
                    required: true,
                    message: "请选择应用范围!"
                }],
            },
            url: {
                add: "/pos/posKwConfig/add",
                edit: "/pos/posKwConfig/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;
        //     }
        // });
        // 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: {

        handleAddClass() {
            this.$refs.modalForm3.edit({
                formThali: true,
            });
            this.$refs.modalForm2.title = "分类选择";
            this.$refs.modalForm2.disableSubmit = false;
        },
        modalFormOkClass(e){
            console.log("e", e);
            //判断两个数组中是否有id相同的元素
            if (this.model.detailListClass.some((t) => e.some((ele) => ele.id == t.id))) {
                this.$message.warning("请勿重复添加");
                return;
            }
            this.model.detailListClass = this.model.detailListClass.concat(e);
        },

        onChange(e) {
            console.log(e);
        },
        add() {
            this.edit(this.modelDefault);
        },
        edit(record) {
            console.log(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";
                    }
                    // this.model.state = this.model.status ? 1 : 0;
                    this.model.money = parseInt(this.model.money)
                    if (this.model.type==0) {
                        this.model.detailList = []
                    }
                    if (this.model.type==1) {
                        this.model.detailList = this.model.detailListClass.map(item=>({
                            goodsId:item.id
                        }))
                    }
                    if (this.model.type==2) {
                        this.model.detailList = this.model.detailList.map(item=>({
                            goodsId:item.id
                        }))
                    }
                    console.log(this.model);
                    httpAction(httpurl, this.model, method)
                        .then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit("ok");
                            } else {
                                that.$message.warning(res.message);
                            }
                        })
                        .finally(() => {
                            that.confirmLoading = false;
                        });
                }
            });
        },

        handleAdd() {
            this.$refs.modalForm2.edit({
                formThali: true,
            });
            this.$refs.modalForm2.title = "商品选择";
            this.$refs.modalForm2.disableSubmit = false;
        },
        modalFormOk(e) {
            console.log("e", e);
            e.forEach((t) => {
                this.$set(t, "required", false);
                this.$set(t, "num", 1);
            });
            //判断两个数组中是否有id相同的元素
            if (this.model.detailList.some((t) => e.some((ele) => ele.id == t.id))) {
                this.$message.warning("请勿重复添加");
                return;
            }
            this.model.detailList = this.model.detailList.concat(e);
        },
        handleDelete(index) {
            this.model.detailList.splice(index, 1);
        },
        handleDeleteClass(index){
            this.model.detailListClass.splice(index, 1);
        }
    },
};
</script>
