$(function () {
    $("#jqGrid").jqGrid(
        {
            datatype : "local",
            height : 600,
            width: 1600,
            rownumbers: true,
            colNames : [ 'ID', '商品编码','门店', 'SKU', '名称', '零售价格','数量','库存', '商品图'],
            colModel : [
                {name : 'id',index : 'id',width : 60,sorttype : "int",hidden: true, key: true},
                {name : 'goodsSn',index : 'goodsSn',width : 80, align: 'center'},
                {name : 'storeName',index : 'storeName',width : 90, align: 'center'},
                {name: 'sku', index: 'sku', width: 80, align: 'center'},
                {name: 'name', index: 'name', width: 160, align: 'center'},
                {name: 'retailPrice', index: 'retailPrice', width: 60, align: 'center'},
                {name: 'nums', index: 'nums', width: 60, align: 'center'},
                {name: 'stockNum', index: 'stockNum', width: 60, align: 'center'},
                {name: 'listPicUrl', index: 'listPicUrl', width: 80, align: 'center'}
            ],
            multiselect : true
        });
    // $("#jqGrid").jqGrid({
    //     // url: '../productstorerela/sannGoodslist',
    //     datatype: "json",
    //     colModel: [
    //         {label: 'ID', name: 'id', index: 'id', hidden: true, key: true},
    //         {label: '商品编码', name: 'goodsSn', hidden: true, index: 'goods_Sn', width: 60},
    //         {label: '门店', name: 'storeName', index: 'storeName', width: 60},
    //         {label: 'SKU', name: 'sku', index: 'sku', width: 80},
    //         {label: '商品类型', name: 'categoryName', hidden: true, index: 'category_id', width: 40},
    //         {label: '名称', name: 'goodsName', index: 'goodsName', width: 160},
    //         {
    //             label: '货品业务类型', name: 'goodsBizType', index: 'goods_biz_type', width: 70,
    //             formatter: function (value) {
    //                 if (value == '00') {
    //                     return '保税备货';
    //                 } else if (value == '02') {
    //                     return '保税展示补货';
    //                 } else if (value == '10') {
    //                     return '保税展示跨境';
    //                 }
    //                 return '普通货物';
    //             }
    //         },
    //         {label: '零售价格', name: 'retailPrice', index: 'retail_price', width: 80},
    //         {label: '库存', name: 'stockNum', index: 'stockNum', width: 80},
    //         {
    //             label: '商品图', name: 'listPicUrl', index: 'list_pic_url', width: 80, align: 'center',
    //             formatter: function (value) {
    //                 return transImg(value);
    //             }
    //         },
    //         {
    //             label: '销售量',
    //             name: 'sellVolume',
    //             index: 'a.sell_volume',
    //             width: 80,
    //             sortable: true,
    //             onSortCol: function (index, colindex, sortorder) {
    //                 //列排序事件
    //                 jQuery("#jqGrid").jqGrid('setGridParam', {page: $('#XtoPage').val()});
    //             }
    //         }
    //     ],
    //     viewrecords: true,
    //     height: 385,
    //     rowNum: 10,
    //     rowList: [10, 30, 50],
    //     rownumbers: true,
    //     rownumWidth: 25,
    //     autowidth: true,
    //     multiselect: true,
    //     pager: "#jqGridPager",
    //     jsonReader: {
    //         root: "page.list",
    //         page: "page.currPage",
    //         total: "page.totalPage",
    //         records: "page.totalCount"
    //     },
    //     prmNames: {
    //         page: "page",
    //         rows: "limit",
    //         order: "order"
    //     },
    //     gridComplete: function () {
    //         $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
    //     }
    // });
    // $('#goodsDesc').editable({
    //     inlineMode: false,
    //     alwaysBlank: true,
    //     height: '500px', //高度
    //     minHeight: '200px',
    //     language: "zh_cn",
    //     spellcheck: false,
    //     plainPaste: true,
    //     enableScript: false,
    //     imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
    //     allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
    //     imageUploadURL: '../sys/oss/upload',
    //     imageUploadParams: {id: "edit"},
    //     imagesLoadURL: '../sys/oss/queryAll'
    // })
});

var ztree;

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        uploadList: [],
        imgName: '',
        visible: false,
        ruleValidate: {
        },
        q: {name: '', goodsSn: ''},
        goodsTotalPrice:0,
        applyPrice:0
    },
    methods: {
        reloadSearch: function () {
            vm.q = {
                name: '',
                goodsSn: ''
            }
        },
        query: function () {
            vm.reload(1);
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.uploadList = [];
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.goods.id == null ? "../goods/save" : "../goods/update";
            vm.goods.goodsDesc = $('#goodsDesc').editable('getHTML');
            vm.goods.goodsImgList = vm.uploadList;
            vm.goods.attributeEntityList = vm.attributeEntityList;
            vm.goods.productEntityList = vm.productEntityList;
            $.ajax({
                type: "POST",
                url: url,
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(vm.goods),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getInfo: function (id) {
            $.get("../goods/info/" + id, function (r) {
                vm.goods = r.goods;
                vm.categoryId = r.goods.categoryId;
                // vm.getCategory();
                var opt = {};
                opt.value = vm.goods.attributeCategory;
                vm.changeCategories(opt);
                if (r.goods.attributeEntityList.length > 0) {
                    vm.attributeEntityList = r.goods.attributeEntityList;
                } else {
                    vm.attributeEntityList = [{'id': '', 'goodsId': '', 'attributeId': '', 'value': '', 'isDelete': 0}];
                }
                if (r.goods.productEntityList.length > 0) {
                    vm.productEntityList = r.goods.productEntityList;
                } else {
                    vm.productEntityList = [{'id': '', 'goodsId': '', 'goodsSpecificationIds': '', 'goodsSpecificationNameValue': '', 'goodsSn': '', 'goodsNumber': '', 'isDelete': 0, 'goodsDefault': 0}];
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            // let page = event;
            // if (event != 1) {
            //     page = $("#jqGrid").jqGrid('getGridParam', 'page');
            // }
            // $("#jqGrid").jqGrid('setGridParam', {
            //     postData: {
            //         'name': vm.q.name,
            //         'goodsSn': vm.q.goodsSn
            //     },
            //     page: page
            // }).trigger("reloadGrid");
            $.get("../goods/scannInfo/" + vm.q.goodsSn, function (r) {
                if (r.code === 0) {
                    vm.goods = r.goods;
                    var mydata = {id : vm.goods.id,goodsSn : vm.goods.goodsSn,storeName:vm.goods.storeName,sku : vm.goods.sku,name : vm.goods.name,
                            retailPrice : vm.goods.retailPrice,nums:1,stockNum : vm.goods.stockNum,listPicUrl : transImg(vm.goods.listPicUrl)};

                    var ids = $("#jqGrid").jqGrid('getDataIDs');
                    var rowid = ids + 1;
                    $("#jqGrid").jqGrid('addRowData', rowid, mydata, "last");

                    var totalPrice =0;
                    var ids = $("#jqGrid").jqGrid('getGridParam', 'selarrrow');//获取 多行数据
                    alert(ids.length);
                    for(var i = 0;i < ids.length ;i ++) {
                        var rowData = $("#jqGrid").jqGrid('getRowData', ids[i]);
                        alert(rowData);
                        alert(rowData.nums);
                        alert(rowData.retailPrice);
                        totalPrice = (rowData.nums * rowData.retailPrice)+totalPrice;
                    }
                    alert(totalPrice);
                    vm.goodsTotalPrice = totalPrice;
                } else {
                    alert(r.msg);
                }
            });


        },
        removeData: function () {
            // confirm('您确定删除此记录？', function () {
            //     if(id != null){
            //         $("#jqGrid").delRowData(id);
            //         $("#jqGrid").jqGrid().trigger("reloadGrid");
            //     }
            // });
            var selectedRowIds = $("#jqGrid").jqGrid("getGridParam","selarrrow");
            if(!selectedRowIds){
                alert("请选择要删除的行");
                return;
            }else{
                var len = selectedRowIds.length;
                for(var i = 0;i < len ;i ++) {
                    // alert(selectedRowIds[i]);
                    $("#jqGrid").jqGrid("delRowData", selectedRowIds[0]);
                }
            }
        }
    },
    mounted() {
        // $.get("../store/queryAll", function (r) {
        //     vm.stores = r.list;
        // });
    }
});