@{
    ViewBag.Title = "按线路选择客户";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<body>
    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <input type="hidden" id="bnum" />
                <input type="hidden" id="EName" />
                <input type="hidden" id="LoginId" />
                <input type="hidden" id="num" />
                <table class="table table-bordered table-striped table-hover">
                    <tr>
                        <td style="text-align:right"><button id="btnSave" onclick="Save()" class="btn btn-default"><i class="glyphicon glyphicon-ok"></i> 确定选择</button></td>
                    </tr>
                </table>
                <div style="width:49%;float:left"><table id="BtypeLodeTable"></table></div>
                <div style="width:2%;float:left">&nbsp;</div>
                <div style="width:49%;float:left">
                    <table id="CustomerLineTable"  data-mobile-responsive="true"></table>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        var selectarray = [];
    function refreshCustomerLine(Num) {//选择线路信息加载客户线路
        $('#CustomerLineTable').bootstrapTable('refresh', { 'url': '../Shop/GetCustomerLineList?Num=' + Num });
        $("#num").val("" + Num + "");
    }
    function Save() {//确认选择
        var str = "[";
        if (selectarray.length <=0) {
            layer.msg("请选择要拜访的客户！"); return false;
        } else {//2354
            for (var i = 0; i < selectarray.length; i++) {
                var strsel = selectarray[i].split(':');
                if (i == selectarray.length - 1)
                { str += '{"btypeid":' + "\"" + strsel[2] + "\"" + "," + '"bfullname":' + "\"" + strsel[3] + "\"" + "," + '"Area":' + "\"" + strsel[5] + "\"" + "," + '"address":' + "\"" + strsel[4] + "\"" + "," + '"usercode":' + "\"" + strsel[6] + "\"" + "}"; }
                else
                { str += '{"btypeid":' + "\"" + strsel[2] + "\"" + "," + '"bfullname":' + "\"" + strsel[3] + "\"" + "," + '"Area":' + "\"" + strsel[5] + "\"" + "," + '"address":' + "\"" + strsel[4] + "\"" + "," + '"usercode":' + "\"" + strsel[6] + "\"" + "},"; }
            }
            str += "]";
            var old = parent.$("#NewSimpleCPTable").bootstrapTable("getData");
            var data1 = JSON.parse(str);
            var newarr = [];
            if (old.length > 0) {
                for (var i = 0; i < data1.length; i++) {
                    var t = 0;
                    for (var j = 0; j < old.length; j++) {
                        if (data1[i].btypeid == old[j].btypeid) {
                            t++;
                        }
                    }
                    if (t<1) {
                        newarr.push(data1[i]);
                    }
                }
                parent.$("#NewSimpleCPTable").bootstrapTable('append', newarr);
                parent.$("#NewSimpleCPTable").bootstrapTable('scrollTo', 'bottom');
                parent.parent.$("#isornosave").val("yes");
            } else {
                parent.$("#NewSimpleCPTable").bootstrapTable('append', data1);
                parent.$("#NewSimpleCPTable").bootstrapTable('scrollTo', 'bottom');
                parent.parent.$("#isornosave").val("yes");
            }
                parent.layer.msg("保存成功！", { time: 2000 });
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
        }
    }
    $(function () {
        $('#BtypeLodeTable').bootstrapTable({//线路信息
            url: '../Shop/GetTableBLodeBysaleList?etypeid=' + "@ViewBag.etypeid",         //请求后台的URL（*）
            method: 'post',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            pagination: true,                   //是否显示分页（*）
            sortOrder: "asc",                   //排序方式
            undefinedText: ' ',
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 30, 50],        //可供选择的每页的行数（*）
            minimumCountColumns: 2,  //最少允许的列数
            height: "470",               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "num",            //每一行的唯一标识，一般为主键列
            clickToSelect: true,              //是否启用点击选中行
            sortable: true,                     //是否启用排序
            maintainSelected: true,
            silentSort: true,
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: false,
            paginationFirstText: '首页',
            paginationLastText: '末页',
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            showHeader: true,
            showColumns: false,            //是否显示所有的列
            showRefresh: false,              //是否显示刷新按钮
            showToggle: false,                     //是否显示详细视图和列表视图的切换按钮
             cardView: false,                    //是否显示详细视图
            detailView: false,                   //是否显示父子表
            queryParams: function (params) {
                return {
                    pagesize: params.limit,//页面大小
                    page: params.offset //页码
                };
            },//传递参数（*）
            columns: [
            //    {
            //    checkbox: true, width: "5%"
            //},
                {
                    title: '序号', width: '5%', align: 'center',
                    formatter: function (value, row, index) {
                        var pageNumber = $('#BtypeLodeTable').bootstrapTable('getOptions').pageNumber;
                        var pageSize = $('#BtypeLodeTable').bootstrapTable('getOptions').pageSize;
                        return (pageNumber - 1) * pageSize + index + 1;
                    }
                },
                {
                    field: "FULLNAME", title: "线路名称", align: "center", formatter: function (value, row, index) {//选择线路
                        var strHtml = '<a id="salesman"  onclick="refreshCustomerLine(' + row.NUM + ')">' + value + '</a>';
                        return strHtml;
                    }
                },
                { field: "NUM", title: "NUM" },
                { field: "LOGINID", title: "LOGINID" },
                { field: "ENAME", title: "ENAME" }
            ]
        });
        $('#CustomerLineTable').bootstrapTable({//客户线路
            url: '../Shop/GetCustomerLineList',         //请求后台的URL（*）
            method: 'post',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            pagination: true,                   //是否显示分页（*）
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 30, 50],        //可供选择的每页的行数（*）
            minimumCountColumns: 2,  //最少允许的列数
            height: "470",               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "",            //每一行的唯一标识，一般为主键列
            clickToSelect: true,              //是否启用点击选中行
            sortable: true,                     //是否启用排序
            silentSort: true,
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: false,
            paginationFirstText: '首页',
            paginationLastText: '末页',
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            showHeader: true,
            showColumns: false,            //是否显示所有的列
            showRefresh: false,              //是否显示刷新按钮
            showToggle: false,                     //是否显示详细视图和列表视图的切换按钮
             cardView: false,                    //是否显示详细视图
            detailView: false,                   //是否显示父子表
            queryParams: function (params) {
                return {
                    pagesize: params.limit,//页面大小
                    page: params.offset //页码
                };
            },//传递参数（*）
            columns: [
                {
                    checkbox: true, width: "5%", formatter: function (value, row, index) {
                        for (var i = 0; i < selectarray.length; i++) {
                            if (selectarray[i] == row.LODEID + ":" + row.NUM + ":" + row.TYPEID_ + ":" + row.FULLNAME + ":" + row.ADDRESS + ":" + row.AREA+":"+row.USERCODE) {
                                return true;
                            }
                        }
                        if (selectarray.some(function (item, index, array) {
                        return item.substring(item.replace(':', 'a').indexOf(':') + 1, item.replace(':', 'a').replace(':', 'a').indexOf(":")) == row.TYPEID_;
                        })) {
                            return {
                                disabled: true,
                                checked: false
                            }
                        }
                    }
                },
                { field: "SORT", title: "SORT" },
                {
                    title: '序号', width: '5%', align: 'center',
                    formatter: function (value, row, index) {
                        var pageNumber = $('#CustomerLineTable').bootstrapTable('getOptions').pageNumber;
                        var pageSize = $('#CustomerLineTable').bootstrapTable('getOptions').pageSize;
                        return (pageNumber - 1) * pageSize + index + 1;
                    }
                },
                {
                    field: "FULLNAME", title: "客户名称", align: "center"
                },
                { field: "ADDRESS", title: "地址" },
                {field:"AREA",title:"地区"},
                { field: "NUM", title: "NUM" },
            { field: "LODEID", title: "LODEID" }
            ],
            onCheck: function (row) {
                if (selectarray.length > 0) {
                    if (!selectarray.some(function (item, index, array) {
                        return item.substring(item.replace(':', 'a').indexOf(':') + 1, item.replace(':', 'a').replace(':', 'a').indexOf(":")) == row.TYPEID_;
                    })) {
                        selectarray.push(row.LODEID + ":" + row.NUM + ":" + row.TYPEID_ + ":" + row.FULLNAME + ":" + row.ADDRESS + ":" + row.AREA + ":" + row.USERCODE);//插入新的,
                    }
                    else {
                        $('#CustomerLineTable').bootstrapTable('refresh');
                        layer.msg("请勿重复勾选相同的客户！");
                    }
                } else {
                    selectarray.push(row.LODEID + ":" + row.NUM + ":" + row.TYPEID_ + ":" + row.FULLNAME + ":" + row.ADDRESS + ":" + row.AREA + ":" + row.USERCODE);
                }
            },
            onUncheck: function (row) {
                for (var i = 0; i < selectarray.length; i++) {
                    if (selectarray[i] == row.LODEID + ":" + row.NUM + ":" + row.TYPEID_ + ":" + row.FULLNAME + ":" + row.ADDRESS + ":" + row.AREA + ":" + row.USERCODE) {
                        selectarray.splice(i, 1);
                    }
                }
            },
            onCheckAll: function (rows) {
                if (selectarray.length > 0) {//在已经有选中项的情况下先剔除已选中项
                    for (var i = 0; i < rows.length; i++) {
                        if (!selectarray.some(function (item, index, array) { return (item.substring(item.replace(':', 'a').indexOf(':') + 1, item.replace(':', 'a').replace(':', 'a').indexOf(":")) == rows[i].TYPEID_) })) {
                            selectarray.push(rows[i].LODEID + ":" + rows[i].NUM + ":" + rows[i].TYPEID_ + ":" + rows[i].FULLNAME + ":" + rows[i].ADDRESS + ":" + rows[i].AREA + ":" + rows[i].USERCODE);//插入新的
                        }
                    }
                } else {//在没有项被选中的情况下直接插入
                    for (var i = 0; i < rows.length; i++) {
                        selectarray.push(rows[i].LODEID + ":" + rows[i].NUM + ":" + rows[i].TYPEID_ + ":" + rows[i].FULLNAME + ":" + rows[i].ADDRESS + ":" + rows[i].AREA + ":" + rows[i].USERCODE);
                    }
                }
            },
            onUncheckAll: function (rows) {
                for (var i = 0; i < rows.length; i++) {
                    for (var j = 0; j < selectarray.length; j++) {
                        if (rows[i].LODEID + ":" + rows[i].NUM + ":" + rows[i].TYPEID_ + ":" + rows[i].FULLNAME + ":" + rows[i].ADDRESS + ":" + rows[i].AREA + ":" + rows[i].USERCODE == selectarray[j]) {
                            selectarray.splice(j, 1);
                            break;
                        }
                    }
                }
            }
        });
        //隐藏列
        $('#BtypeLodeTable').bootstrapTable('hideColumn', 'NUM');
        $('#BtypeLodeTable').bootstrapTable('hideColumn', 'LOGINID');
        $('#BtypeLodeTable').bootstrapTable('hideColumn', 'ENAME');
        $('#CustomerLineTable').bootstrapTable('hideColumn', 'NUM');
        $('#CustomerLineTable').bootstrapTable('hideColumn', 'LODEID')
        $('#CustomerLineTable').bootstrapTable('hideColumn', 'SORT')
    });
    </script>
</body>