var regeneratorRuntime = require('../../utils/runtime.js');
var os = getApp().globalData.setting
Component({
    properties: {
        // 这里定义了数据列表属性，列表显示数据
        all_collocation_list: {
            type: Array,
            value: null
        },
        // 是不是购物车
        is_cart: {
            type: Boolean,
            value: false
        },
        // 购物车的索引
        cart_index: {
            type: Number,
            value: 0
        },
        //主商品物流方式
        distr_t: {
            type: Number,
            value: 0
        },

        sales_rules: {
            type: Number,
            value: 1
        },
        pick:{
            type: Number,
            value: 1
        },
        appoint_pick_keyid:{
            type: String,
            value: ''
        }

    },
    /*** 页面的初始数据***/
    data: {
        imgUrl: getApp().globalData.setting.imghost,
    },
    methods: {
        async  select_coll(e) {
            var th=this;
            var index = e.currentTarget.dataset.index;
            var itr = this.properties.all_collocation_list[index];
            var ob = {
                index: index,
                selected: !itr.selected
            }
            
            if(this.properties.is_cart){
                ob.is_cart =1;
                ob.cart_index =this.properties.cart_index;
            }


            if(th.data.sales_rules>1){
                var isok = 1;
                await th.check_store_num(itr.goods_id, th.data.pick, 1, function (res){
                    isok = res;
                })
                if(!isok){
                    wx.showToast({
                        title: itr.goods_name+"商品库存不足",
                        icon: 'none',
                        duration: 2000
                    })
                    return false;
                }
            }

            this.triggerEvent('childFun', ob);

            if(!ob.selected) {
                var txt = "all_collocation_list[" + index + "].selected"
                this.setData({[txt]: ob.selected})

            }
        },

        //图片失败，默认图片
        bind_bnerr3: function (e) {
            var _errImg = e.target.dataset.errorimg;
            var _Img = e.target.dataset.img;
            if (_Img != undefined) {
                var _errObj = {};
                _errObj[_errImg] = "/public/images/default_goods_image_240.gif";
                this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
            }
        },


        //---确认线下门店的数量足不足---
        async check_store_num(goods_id, pick, goods_num, func) {
            var lock = 0, pick_no, plist, erpwareid;

            var lock_rq={ store_id: os.stoid, wareId: goods_id, storageId: pick, pageSize: 1000 };
            if(this.data.sales_rules==3){
                lock_rq.appoint_pick_keyid=this.data.appoint_pick_keyid;
                delete lock_rq.storageId
            }
            //先读取门店的lock
            await getApp().request.promiseGet("/api/weshop/order/ware/lock/page", {
                data: lock_rq
            }).then(res => {
                if (res.data.code == 0 && res.data.data.total > 0) {
                    for (var i in res.data.data.pageData)
                        lock += res.data.data.pageData[i].outQty;
                }
            })

            if(this.data.sales_rules==2) {
                //先获取门店的编号
                await getApp().request.promiseGet("/api/weshop/pickup/get/" + os.stoid + "/" + pick, {
                }).then(res => {
                    if (res.data.code == 0) {
                        pick_no = res.data.data.pickup_no;
                    }
                })
            }

            //先获取商品的线下库存
            await getApp().request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + goods_id, {
            }).then(res => {
                if (res.data.code == 0) {
                    erpwareid = res.data.data.erpwareid;
                }
            })


            var sto_rq={ storageNos: pick_no, wareIds: encodeURIComponent(erpwareid), storeId: os.stoid, pageSize: 2000 };
            if(this.data.sales_rules==3){
                sto_rq.storageIds=this.data.appoint_pick_keyid;
                delete sto_rq.storageNos
            }

            //读取线下的门店库存
            await getApp().request.promiseGet("/api/weshop/goods/getWareStorages", {
                data: sto_rq
            }).then(res => {
                if (res.data.code == 0 && res.data.data.total > 0) {
                    plist = res.data.data.pageData[0];
                }
            })

            var isok = 1;
            if (!plist || goods_num > plist.CanOutQty - lock) {
                isok = 0;
            }
            func(isok);
        },

        set_sele(index){
            var txt = "all_collocation_list[" + index + "].selected"
            this.setData({[txt]: 1})
        },

        //-- 清理所有的选择 --
        clear_sele(){
            var all_collocation_list=this.data.all_collocation_list;
            for (let i = 0; i <all_collocation_list.length ; i++) {
                all_collocation_list[i].selected=0;
            }
            this.setData({all_collocation_list})
        }


    }
})