jqMultipleShow("click", ".navList", ".tab", ".con");
var myGoods = new Vue({
    el:"#myGoods",
    data: {
        userId:0,
        shopId:'',
        userIdentity:'',
        isSelectActive:0,
        isRequest:false,
        listRecord: 0,
        pageInput: '',
        tabIndex:1,
        featuredNum:'',//主图商品标志
        listQuery:{
            shopID:'',
            pageNum:1,
            pageSize:10,
            productCode:'',//货号
            name:'',//商品名称
            validFlag:'',//状态
            featuredFlag:'',//是否主推
            bigTypeID:0,//一级
            smallTypeID:0,//二级
            tinyTypeID:0//三级
        },
        classFlyInfoArr:[{name:'全部',value:''}],
        classificationFirstList:[],
        classificationTwoList:[],
        classificationThreeList:[],
        productsList:[],
        checkList:[],
        isCheckedAll:false
    },
    computed: {
        pageTotal: function () {
            var total = Math.ceil(this.listRecord / this.listQuery.pageSize);
            return total > 0 ? total : 1;
        },
        showPageBtn: function () {
            var total = Math.ceil(this.listRecord / this.listQuery.pageSize);
            total = total > 0 ? total : 1;
            var index = this.listQuery.pageNum, arr = [];
            if (total <= 6) {
                for (var i = 1; i <= total; i++) {
                    arr.push(i);
                }
                return arr;
            }
            if (index <= 3) return [1, 2, 3, 4, 5, 0, total];
            if (index >= total - 2) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
            return [1, 0, index - 2, index - 1, index, index + 1, index + 2, 0, total];
        }
    },
    methods: {
        GetMyproductDataInfo: function(){//我的商品数据初始化
            var _this = this;
            // _this.returnedTarget();
            console.log(_this.listQuery)
            SupplierApi.GetMyProductList(_this.listQuery,function(response){
                if(response.code == 0){
                    _this.isRequest = true;
                    _this.featuredNum = response.data.featuredNum;
                    var data = response.data.productPage;
                    if(data.results && data.results.length>0){
                        _this.productsList = data.results;
                        _this.listRecord = data.totalRecord;
                        var NewProduct = [],isAddFeatured=false;
                        _this.productsList.map(function(el){
                            if(_this.featuredNum<4){
                                if(el.validFlag == 2 && el.featuredFlag ==0 ){
                                    isAddFeatured = true;
                                }else{
                                    isAddFeatured = false;
                                }
                            }
                            if(el.validFlag == 2 && el.featuredFlag == 1 ){
                                isDelFeatured = true;
                            }else{
                                isDelFeatured = false;
                            }
                            _this.returnedTarget();
                            NewProduct.push(Object.assign({},el,{isAddFeatured:isAddFeatured,isDelFeatured:isDelFeatured}))
                        });
                        _this.productsList = NewProduct;
                    }else{
                        _this.productsList = data.results;
                    }
                }else{
                    CAIMEI.Alert(response.msg, '确定');
                }
            })
        },
        touchmoveFn: function(){
            this.isSelectActive=0;
        },
        SelectChangeStateFn: function(){//选择状态
            var _this = this;
            _this.listQuery.validFlag =event.target.value;
        },
        SelectChangeInsideFn: function(){//选择主推
            var _this = this;
            _this.listQuery.featuredFlag =event.target.value;
        },
        GetFistClassFn: function(){//获取一级分类菜单
            var _this = this;
            PublicApi.GetFirstClassFication({},function (response) {
            console.log(response)
                if(response.data.length>0){
                     _this.classificationFirstList = response.data;
                }else {
                      _this.classificationFirstList = [];
                }
            })
        },
        GetTwoClassFn: function(value){//获取二级分类菜单
            var _this = this;
            PublicApi.GetTwoClassFication({bigTypeId:value},function (response) {
                if(response.code==0){
                    if(response.data.length>0){
                        _this.classificationTwoList = response.data;
                     }else {
                         _this.classificationTwoList = [];
                     }
                }
            })
        },
        GetThreeClassFn: function(value){//获取三级分类菜单
            var _this = this;
            PublicApi.GetThreeClassFication({smallTypeId:value},function (response) {
            console.log(response,'三级分类')
                if(response.code==0){
                  if(response.data.length>0){
                        _this.classificationThreeList = response.data;
                  }else {
                     _this.classificationThreeList = [];
                  }
                }
            })
        },
        ChangeClassificationFirst: function(){//选择一级分类
            var _this = this;
            _this.listQuery.bigTypeID = event.target.value;
            if(_this.listQuery.bigTypeID == ''){
                _this.listQuery.bigTypeID = '';
            }else{
                _this.GetTwoClassFn(_this.listQuery.bigTypeID);
            }
        },
        ChangeClassificationTwo: function(){//选择二级分类
            var _this = this;
             _this.listQuery.smallTypeID = event.target.value;
                console.log(event.target.value)
            if(_this.listQuery.smallTypeID == ''){
                _this.listQuery.smallTypeID = '';

            }else{
                _this.GetThreeClassFn(_this.listQuery.smallTypeID);
                 console.log(2222)
            }
        },
        ChangeClassificationThree: function(){//选择三级分类
            var _this = this;
             _this.listQuery.tinyTypeID = event.target.value;
            if(_this.listQuery.tinyTypeID == ''){
                _this.listQuery.tinyTypeID = '';
            }else{
            }
        },
        SearchQueryListFn: function(){//搜索
            var _this = this;
            _this.GetMyproductDataInfo();
        },
        checkNum : function() {
            var _this = this;
            if (_this.pageInput > _this.pageTotal) {
                _this.pageInput = _this.pageTotal;
            } else if (_this.pageInput < 1) {
                _this.pageInput = 1;
            }
        },
        toPagination : function(pageNum) {
            var _this = this;
            _this.productsList = [];
            if (pageNum <= this.pageTotal) {
                _this.listQuery.pageNum = pageNum;
                _this.GetMyproductDataInfo()
            }
        },
        ItemDownshelfAll: function(){//批量下架
            var _this = this;
            var isValidFlag = false;
            if(_this.isCheckedAll){
                _this.productsList.map(function(el){
                    if(el.validFlag != 2){ isValidFlag = true; }
                });
                if(isValidFlag){
                    CAIMEI.Alert('部分选中的商品暂未上架，不能进行下架操作','确定');
                    return;
                }else{
                    CAIMEI.Modal('确定下架选中的商品吗？','取消','确定',function () {
                          var shelProductsList='';
                        _this.productsList.map(function(el){
                            if(el.validFlag == 2){
                                shelProductsList += el.productID+','
                            }
                        });
                        SupplierApi.SupplierSoldOut({productIds:shelProductsList,},function (response) {
                            if (response.code == 0){
                                CAIMEI.dialog('下架成功！');
                                _this.productsList = [];
                                _this.GetMyproductDataInfo();
                            }else{
                                 CAIMEI.Alert(response.msg,'确定');
                            }
                        })
                    })
                }
            }
        },
        ItemDownshelf: function(pros){//下架商品
            var _this = this;
            CAIMEI.Modal('确定下架该商品吗？','取消','确定',function () {
                 SupplierApi.SupplierSoldOut({productIds:pros.productID,},function (response) {
                    if (response.code == 0){
                        CAIMEI.dialog('下架成功~');
                        _this.productsList = [];
                        _this.GetMyproductDataInfo();
                    }else{
                        CAIMEI.Alert(response.msg,'确定');
                    }
                })
            })
        },
        AddPushHotFn: function(pros){//添加主页推荐
            var _this = this;
            var num = 4-this.featuredNum;
            CAIMEI.Modal('总共能添加4个主推商品，您还能添加'+num+'个确定将该商品添加为主推商品吗？','取消','确定',function () {
                 SupplierApi.SwitchFeatured({productId:pros.productID,featuredFlag:1},function (response) {
                    if (response.code == 0){
                        CAIMEI.dialog('添加成功~');
                        _this.productsList = [];
                        _this.GetMyproductDataInfo();
                    }else{
                         CAIMEI.Alert(response.msg,'确定');
                    }
                })
            })
        },
        DeletePushHotFn: function(pros){//删除主页推荐
            var _this = this;
             CAIMEI.Modal('是否把该商品从主推商品中删除？','取消','确定',function () {
                  SupplierApi.SwitchFeatured({productId:pros.productID,featuredFlag:0},function (response) {
                    if (response.code == 0){
                        CAIMEI.dialog('删除成功~');
                        _this.productsList = [];
                        _this.GetMyproductDataInfo();
                    }else{
                         CAIMEI.Alert(response.msg,'确定');
                    }
                })
             })
        },
        pageLinkDetils: function(pros){//预览商品
            window.open('/product-'+pros.productID+'.html');
        },
        editGoodFn: function(pros){//编辑商品
            location.href ='/supplier/release.html?productID='+pros.productID+'&type=edit';
        },
        ischeck: function(pros){//为未选中的时候改变为true，反之为true
            pros.productsChecked = !pros.productsChecked;
            this.updateCheckAllBtn();
        },
        updateCheckAllBtn: function() {// 全选勾选判断
            var _this = this;
            var goodsCheckedLength = 0,
                productsList = _this.productsList;
            productsList.forEach(function(el){
                if(el.productsChecked) { goodsCheckedLength++; }
            });
            _this.isCheckedAll = goodsCheckedLength === productsList.length;
        },
        updateBothCheckBtn: function() {
            var _this = this;
            _this.productsList.forEach(function(el){
                el.productsChecked = _this.isCheckedAll ;
            })

        },
        checkedAll: function() {
            var _this = this;
            _this.isCheckedAll = !_this.isCheckedAll;
            _this.updateBothCheckBtn();
        },
        toFixedFn: function(text){
            return Number(text).toFixed(2);
        },
        StatusColorFn: function(state){
            var stateColor = '',
                stateColorObject={
                    '2':'#1890f9',
                    '1':'#f9a94b',
                    '0':'#FFB12A',
                    '3':'#627386',
                    '8':'#FF2A2A',
                    '9':'#f94b4b'
                };
            Object.keys(stateColorObject).forEach(function(key){
                if(key == state){
                    stateColor = stateColorObject[key]
                }
            });
            return stateColor;
        },
        StatusTextFn: function(state){
            var stateText = '',
                stateTextObject={
                    '2':'已上架',
                    '3':'已下架',
                    '1':'待审核',
                    '8':'审核未通过',
                    '9':'已冻结',
                    '0':'已删除'
                };
            Object.keys(stateTextObject).forEach(function(key) {
                if(key == state){
                    stateText = stateTextObject[key]
                }
            });
            return stateText;
        },
        StatusButtonFn: function(state){
            var _this = this;
            var btnState = false;
            _this.mapStateArr.forEach(function(el){
                el.val.forEach(function(value){
                    if(state === value){
                        btnState = el.status;
                    }
                })
            });
            return btnState
        },
        returnedTarget:function(){//对象合并 IE 兼容方法
            if (typeof Object.assign != 'function') {
                Object.assign = function(target) {
                    'use strict';
                    if (target == null) {
                        throw new TypeError('Cannot convert undefined or null to object');
                    }
                    target = Object(target);
                    for (var index = 1; index < arguments.length; index++) {
                        var source = arguments[index];
                        if (source != null) {
                            for (var key in source) {
                                if (Object.prototype.hasOwnProperty.call(source, key)) {
                                    target[key] = source[key];
                                }
                            }
                        }
                    }
                    return target;
                };
            }
        }
    },
    mounted: function () {
        var _this = this;
        if(globalUserData){
            _this.userId = globalUserData.userId;
            _this.listQuery.shopID = globalUserData.shopId;
            _this.shopId =globalUserData.shopId;
        }
        _this.GetMyproductDataInfo();
        _this.GetFistClassFn();
    }
});

