$(function () {
    $("#jqGrid").jqGrid(
        {
            datatype : "local",
            height : 600,
            width: 1700,
            rownumbers: true,
            colNames : [ 'ID', '产品条码','门店', 'SKU', '名称', '零售价格','数量','库存', '商品图'],
            colModel : [
                {name : 'id',index : 'id',width : 60,sorttype : "int",hidden: true, key: true},
                {name : 'prodBarcode',index : 'prodBarcode',width : 80, align: 'center'},
                {name : 'storeName',index : 'storeName',width : 90, align: 'center'},
                {name: 'sku', index: 'sku', width: 80, align: 'center'},
                {name: 'goodsName', index: 'name', width: 160, align: 'center'},
                {name: 'storeRetailPrice', index: 'storeRetailPrice', width: 60, align: 'center'},
                {name: 'number', 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: '../goods/scannInfo',
    //     datatype: "json",
    //     colModel: [
    //         {label: 'ID', name: 'id', index: 'id', hidden: true, key: true},
    //         {label: '商品编码', name: 'goodsSn',index: 'goods_Sn', width: 60, align: 'center'},
    //         {label: '门店', name: 'storeName', index: 'storeName', width: 60, align: 'center'},
    //         {label: 'SKU', name: 'sku', index: 'sku', width: 80, align: 'center'},
    //         {label: '名称', name: 'goodsName', index: 'goodsName', width: 160, align: 'center'},
    //         {label: '零售价格', name: 'storeRetailPrice', index: 'retail_price', width: 80, align: 'center'},
    //         {label: '数量', name: 'number', index: 'number', width: 80, align: 'center'},
    //         {label: '库存', name: 'stockNum', index: 'stockNum', width: 80, align: 'center'},
    //         {
    //             label: '商品图', name: 'listPicUrl', index: 'list_pic_url', width: 80, align: 'center',
    //             formatter: function (value) {
    //                 return transImg(value);
    //             }
    //         }
    //     ],
    //     viewrecords: true,
    //     height: 550,
    //     rownumbers: true,
    //     rownumWidth: 25,
    //     autowidth: true,
    //     multiselect: true,
    //     jsonReader: {
    //         root: "cartEntityList"
    //     },
    //     prmNames: {
    //         page: "page",
    //         rows: "limit",
    //         order: "order"
    //     },
    //     gridComplete: function () {
    //         $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
    //     }
    // });
});

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: [],
        offlineCartList: [{'id': '', 'prodBarcode': '', 'storeName': '', 'SKU': '', 'goodsName': '', 'storeRetailPrice':'', 'number':'','stockNum':'','listPicUrl':''}],
        offlineCart:{'id': '', 'prodBarcode': '', 'storeName': '', 'SKU': '', 'goodsName': '', 'storeRetailPrice':'', 'number':'','stockNum':'','listPicUrl':''},
        imgName: '',
        visible: false,
        ruleValidate: {
        },
        q: {prodBarcode: ''},
        goodsTotalPrice:0,
        applyPrice:0,
        idList:[],
        count:0
    },
    methods: {
        reloadSearch: function () {
            vm.q = {
                prodBarcode: ''
            }
        },
        query: function () {
            vm.reload(1);
        },
        getInfo: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            vm.offlineCartList = [];
            var totalPrice =0;
            for(var i = 0;i < ids.length ;i ++) {
                var rowData = $("#jqGrid").jqGrid('getRowData', ids[i]);
                vm.offlineCartList.push(rowData);
                totalPrice = (rowData.number * rowData.storeRetailPrice)+totalPrice;
            }
            totalPrice = totalPrice.toFixed(2);
            vm.goodsTotalPrice = totalPrice;
            vm.applyPrice = totalPrice;
            console.log('合计后的总和：'+vm.goodsTotalPrice);

            for(var i = 0;i < vm.offlineCartList.length ;i ++) {
                vm.offlineCartList[i].listPicUrl ='';
            }
            $.ajax({
                type: "POST",
                url: "../offlinecart/infoList",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(vm.offlineCartList),
                success: function (r) {
                    if (r.code == 0) {
                        vm.offlineCartList = r.list;
                        vm.showList = false;
                    } else {
                        alert(r.msg);
                    }
                }
            });

            // vm.showList = false;
            vm.title = "预订单生成";
        },
        reload: function (event) {
            vm.showList = true;
            // let page = event;
            // if (event != 1) {
            //     page = $("#jqGrid").jqGrid('getGridParam', 'page');
            // }
            // $("#jqGrid").jqGrid('setGridParam', {
            //     postData: {
            //         'goodsSn': vm.q.goodsSn
            //     }
            // }).trigger("reloadGrid");
            $.get("../goods/scannInfo/" + vm.q.prodBarcode, function (r) {
                if (r.code === 0) {
                    vm.goods = r.goods;
                    var mydata = {id : vm.goods.id,prodBarcode : vm.goods.prodBarcode,storeName:vm.goods.storeName,sku : vm.goods.sku,goodsName : vm.goods.name,
                        storeRetailPrice : vm.goods.storeRetailPrice,number:1,stockNum : vm.goods.stockNum,listPicUrl : transImg(vm.goods.listPicUrl)};
                    vm.count = vm.count+1;
                    // var ids = $("#jqGrid").jqGrid('getDataIDs');
                    // if(ids!=""){
                    //     vm.idList = ids.split(",");
                    //     ids = vm.idList[vm.idList.length];
                    //     console.log("ids2："+ids);
                    // }
                    // console.log("ids4："+ids);
                    var rowid = parseInt(vm.count);
                    $("#jqGrid").jqGrid('addRowData', rowid, mydata, "last");
                    vm.q = {
                        prodBarcode: ''
                    };
                } else {
                    alert(r.msg);
                }
            });


        },
        back: function (event) {
            vm.showList = true;
            $.ajax({
                type: "POST",
                url: "../offlinecart/deleteByUserId",
                contentType: "application/json",
                success: function (r) {
                    if (r.code == 0) {
                        vm.q = {
                            prodBarcode: ''
                        };
                        jQuery("#jqGrid").jqGrid("clearGridData");
                        $("#cb_jqGrid").click();
                    }
                }
            });
        },
        orderSubmit: function (event) {
            var url = "../order/orderSubmit";
            $.ajax({
                type: "POST",
                url: url,
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(vm.offlineCartList),
                success: function (r) {
                    if (r.code === 0) {
                        alert('订单生成成功，请到线下订单页面中操作待付款单', function (index) {
                            vm.showList = true;
                            vm.q = {
                                prodBarcode: ''
                            };
                            jQuery("#jqGrid").jqGrid("clearGridData");
                            $("#cb_jqGrid").click();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        handleSubmit: function (name) {
            vm.orderSubmit();
        },
        removeData: function () {
        //     var ids = getSelectedRows();
        //     if (ids == null) {
        //         return;
        //     }
        //     confirm('确定要删除选中的记录？', function () {
        //         $.ajax({
        //             type: "POST",
        //             url: "../offlinecart/delete",
        //             contentType: "application/json",
        //             data: JSON.stringify(ids),
        //             success: function (r) {
        //                 if (r.code == 0) {
        //                     alert('操作成功', function (index) {
        //                         vm.q = {
        //                             goodsSn: ''
        //                         }
        //                         $("#jqGrid").jqGrid('setGridParam', {
        //                             postData: {
        //                                 'goodsSn': ''
        //                             }
        //                         }).trigger("reloadGrid");
        //                     });
        //                 } else {
        //                     alert(r.msg);
        //                 }
        //             }
        //         });
        //     });
            var selectedRowIds = $("#jqGrid").jqGrid("getGridParam","selarrrow");
            if(!selectedRowIds){
                alert("请选择要删除的行");
                return;
            }else{
                var len = selectedRowIds.length;
                for(var i = 0;i < len ;i ++) {
                    $("#jqGrid").jqGrid("delRowData", selectedRowIds[0]);
                }
            }
        },
        getTotal:function () {

            $("#cb_jqGrid").click();

            // var totalPrice =0;
            // var ids = $("#jqGrid").jqGrid('getGridParam', 'selarrrow');//获取 多行数据
            // for(var i = 0;i < ids.length ;i ++) {
            //     var rowData = $("#jqGrid").jqGrid('getRowData', ids[i]);
            //     totalPrice = (rowData.nums * rowData.storeRetailPrice)+totalPrice;
            // }
            // vm.goodsTotalPrice = totalPrice;
            // vm.applyPrice = totalPrice;
        }
    },
    mounted() {
        // $.get("../store/queryAll", function (r) {
        //     vm.stores = r.list;
        // });
    }
});