<template>
    <lj-win-index-common-list :title="title" api-name="GetL1MtrldefSale"
        :data-reset-callback="handleDataReset"
        :data-loaded-callback="handleDataLoaded"
        ifShowSearchbar
        :query-fields="queryFields"
        :queryHardParms="queryHardParms"
        hint="编码/品名/规格/产品系列/子型号"
        :searchText="keyword"
        v-on:update:searchText="emitChangeKeyword"
        :query-quick="queryQuick"
        :search-actions="searchActions"
        :closeWinHandler="handleCloseWin"
        @scan-click="handleSearchScan"
        ref="baseWin">
        <template slot="header_right">
            <lj-saletask-cart />
        </template>

        <div slot="body_list">
            <template v-for="(item, idx) in allData">
                <product-index-item
                    :item="item"
                    :key="idx"
                    :baseMsg="baseMsg"
                    @click.native="handleCellClick(item)"
                    @button-click="handleAddGoodsToCart"
                ></product-index-item>
            </template>

            <lj-mtrldef-shopping
                ref="ljMtrldefShopping"
                :cusid="cusid"
                :moneyid="moneyid"
                :typeid="typeid"
                :targetName="addItem.mtrlname"
                :targetCode="addItem.mtrlcode"
                @confirm="handleConfirmToCart"
            >
            </lj-mtrldef-shopping>
        </div>
    </lj-win-index-common-list>
</template>

<script>
    import LjWinIndexCommonList from "components/lj-win-index-common-list";
    import LjSearchbar from "components/lj-searchbar";
    import ProductIndexItem from './product-index-sale-item';
    import LjSaletaskCart from './lj-saletask-cart';
    import LjMtrldefShopping from './lj-mtrldef-shopping.vue';

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommonList,
            ProductIndexItem,
            LjSaletaskCart,
            LjMtrldefShopping
        },
        data() {
            return {
                open: false,
                docked: false,
                position: true,
                allData: [],
                selectedItems: [],
                winMode: "",
                title: "销售产品列表",
                mtrltype: "",
                mtrltypeList: [], //'经理椅','职员椅','会议椅','休闲椅','电竞椅','办公网椅','办公布椅'
                saletypeList:[],
                queryHardParms: {
                },
                baseMsg: {
                    mtrlsectypename: "自定义1"
                },
                queryFields: [
                    {
                        title: "产品编码",
                        property: "mtrlcode",
                        initValue: "",
                        group: "常规属性"
                    },
                    {
                        title: "产品名称",
                        property: "mtrlname",
                        initValue: "",
                        group: "常规属性"
                    },
                    {
                        title: "规格",
                        property: "mtrlmode",
                        initValue: "",
                        group: "常规属性"
                    },
                    {
                        title: "子型号",
                        property: "sub_model",
                        initValue: "",
                        group: "常规属性"
                    },
                    {
                        title: "全部状态",
                        property: "isuse",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            options: [
                                {
                                    isuse: 1,
                                    dscrp: "有效"
                                },
                                {
                                    isuse: 0,
                                    dscrp: "无效"
                                }
                            ], 
                            valueMap: { text: 'dscrp', value: 'isuse'},
                        },
                        group: "状态"
                    },
                    {
                        title: "显示0库存(仅库存模式使用)",
                        property: "ifzeromtrlware",
                        initValue: 0,
                        type: "switch"
                    }

                ],
                queryQuick: [
                    {
                        title: "选择模式",
                        property: "selectModel",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            options: [
                                {
                                    isuse: 0,
                                    dscrp: "标准模式"
                                },
                                {
                                    isuse: 1,
                                    dscrp: "库存模式"
                                }
                            ], 
                            valueMap: { text: 'dscrp', value: 'isuse'},
                        }
                    },
                    {
                        title: "产品分类",
                        property: "mtrltype",
                        type: 'radioFree',
                        initValue: "",
                        selectOptions: {
                            valueMap: { text: 'dscrp', value: 'isuse'},
                        }
                    },
                    {
                        title: "仓库",
                        property: "storageid",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            valueMap: { text: 'storagename', value: 'storageid'},
                        }
                    }
                ],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'keyword'
                    },
                    { 
                        title: '产品编码',
                        property: 'mtrlcode' 
                    },
                    { 
                        title: '产品名称',
                        property: 'mtrlname'
                    },
                    { 
                        title: '规格',
                        property: 'mtrlmode'
                    },
                    { 
                        title: '产品系列',
                        property: 'band'
                    },
                    { 
                        title: '子型号',
                        property: 'sub_model'
                    },
                ],
                keyword: '',
                cusid: 0,
                moneyid: 0,
                typeid: 0,
                productDraft:"saletask-edit-product-draft",
                addItem: {}, // 加入购物车item
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            
            // 销售订单添加产品
            this.cusid = pageParam.cusid;
            this.moneyid = pageParam.moneyid;
            this.typeid = pageParam.typeid;

            this.queryHardParms.cusid = this.cusid;
            this.queryHardParms.moneyid = this.moneyid;
            this.queryHardParms.typeid = this.typeid;

            // this.refreshType();
            this.getType();

            let userInfo = $lj.getStorage('userInfo');
            this.baseMsg.mtrlsectypename = userInfo.zxmtrlmodename || "";

            let storageInfo = $lj.getStorage("storageInfo") || {};
            if (storageInfo.storagePower == null || storageInfo.storagePower == "") {
                $lj.toast("该用户的仓库权限不存在 / 该用户不存在");
                return false;
            }
            if (storageInfo.storagePower == "-1") {
                $lj.toast("你没有仓库权限");
                return false;
            }
            this.$set(this.queryQuick[2].selectOptions, 'options', storageInfo.storageList);

            // 固定返回，
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err) {
                $lj.openWin("saletask-edit", {
                    winMode: 0
                });
            });
        },
        methods: {
            handleConfirmToCart(mx) {
                // mx.mxdscrp = "";
                // mx.requiredate = $lj.dateformat(new Date(),'yyyy-MM-dd');
                $lj.sendEvent("saletask_edit_add",mx);
                $lj.toast("成功添加到销售订单明细列表");
                $lj.sendEvent("cart_refresh");
            },
            /**关闭窗口 */
            handleCloseWin(){
                $lj.openWin("saletask-edit", {
                    winMode: 0
                });
            },
            /**搜索栏-扫描 */
            handleSearchScan(searchState) {
                $lj.openScanner((ret) => {
                    setTimeout(() => {
                        let obj = {};
                        obj[searchState] = ret.content.trim();
                        this.$refs.baseWin.handleSearchbar(obj, true);
                    }, 200);
                })
            },
            /**回调绑定值 */
            emitChangeKeyword(val) {
                this.keyword = val;
            },
            getType() {
                let self = this;
                let userInfo = $lj.getStorage('userInfo');
                if (userInfo.mtrltypeAllList) {
                    for (let i = 0; i < self.queryQuick.length; i++) {
                        if (self.queryQuick[i].property == 'mtrltype') {
                            self.$set(self.queryQuick[i].selectOptions, 'options', userInfo.mtrltypeAllList);
                            break;
                        }
                    }
                }
            },
            handleDataReset(response) {
                this.allData = [];
                this.addList(response.mtrldefList);
            },
            handleDataLoaded(response) {
                let moreItems = response.mtrldefList;
                if (moreItems.length > 0) {
                    this.addList(moreItems);
                    return true;
                }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleCellClick(mainObject) {
                $lj.openWin("product-shopping", {
                    mainObject: mainObject,
                    cusid: this.cusid,
                    moneyid: this.moneyid,
                    typeid: this.typeid
                });
            },
            handleAddGoodsToCart(item) {
                console.log('handleAddGoodsToCart item :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(item));
                this.addItem = item;
                this.$nextTick(() => {
                    this.$refs.ljMtrldefShopping.open(item);
                })
                // // 添加到购物车
                // let self = this;
                // $lj.postLJRequest("AddGoodsToCart", {
                //     token: $lj.getStorage("token"),
                //     mtrlid: item.mtrlid,
                //     status: item.dftStatus || "",
                //     woodcode: item.dftWoodcode || "",
                //     pcode: item.dftPcode || "",
                //     unit: item.unit,
                //     qty: 1,
                //     rebate: item.rebate,
                //     enprice: item.price,
                //     unit: item.unit,
                //     taxrate: item.taxrate
                // }, function (ret) {
                //     if(ret && ret.saletaskMx) {

                //         var mx = ret.saletaskMx;

                //         mx.mxdscrp = "";
                //         mx.requiredate = $lj.dateformat(new Date(),'yyyy-MM-dd');

                //         $lj.sendEvent("saletask_edit_add",mx);
                //         $lj.toast("成功添加到销售订单明细列表");
                //         $lj.sendEvent("cart_refresh");
                //     }
                // })
            },
            handleGetFilterReturn(item) {
                this.selectModel = item.selectModel;
            }
        },
        computed: {
        }
    }
</script>

<style>
    .topbar {
        min-height: 44px;
        border-bottom: 1px solid #e5e5e5;
        background: #fff;
        position: fixed;
        width: 100%;
        z-index: 9999;
        display: flex;
        font-size: 18px;
        align-items: center;
        margin-top: 25px;
    }

    .topbar_left {
        flex: 0 0 20px;
        margin-left: 10px;
    }

    .topbar_middle {
        text-align: center;
        flex: 0 0 89%;
    }

    .topbar_right {
        flex: 0 0 20px;
        position: absolute;
        right: 10px;
    }

    .quote_mask {
        margin-top: 60px;
        padding: 10px 10px 30px 10px;
    }

    .demo4-item {
        background-color: #ddd;
        color: #222;
        font-size: 14px;
        padding: 5px 10px;
        margin: 5px;
        line-height: 18px;
        border-radius: 15px;
    }

    .demo4-item-selected {
        background-color: #FF3B3B;
        color: #fff;
    }
</style>