@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<div class="wrapper wrapper-content">
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <table class="table table-bordered table-striped table-hover" style="margin:0px;">
                <tr>
                    <td >
                        <select id="selType" class="form-control" style="width:inherit;">
                            <option value="2">按客户全名查询</option>
                            <option value="1">按客户编号查询</option>
                        </select>
                    </td>
                    <td>
                        <input type="text" id="nameORcode" onkeyup="cesi()" style="width:200px" class="form-control" />
                    </td>
                    <td >
                        <button id="btnSeach" onclick="Search()" class="btn btn-default"><i class="layui-icon">&#xe615;</i> 查询</button>
                        <button id="btnselect" class="btn btn-default" onclick="btnselect()"><i class="layui-icon">&#xe618;</i> 确定选择</button>
                        <button id="btnBack" class="btn btn-default" onclick="back()">返回上层</button>
                    </td>
                </tr>
            </table>
            <table id="businessTable"></table>
            <input type="hidden" id="parid" />
        </div>
    </div>
</div>
<script type="text/javascript">
    function back() {
        var selType = $("#selType option:selected").val();
        var nameORcode = $("#nameORcode").val();
        var parid = $("#parid").val()
        if (parid == "root" || parid == "0") return;
        $('#businessTable').bootstrapTable('refresh', {
            url: "../CommonSelector/GetBackClientName?selType=" + selType + "&nameORcode=" + nameORcode + "&parid=" + parid
        })
    }
    $(function () {
        $('#businessTable').bootstrapTable({
            url: '../CommonSelector/GetClientName',         //请求后台的URL（*）
            method: 'post',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            pagination: true,                   //是否显示分页（*）
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 15, 30],        //可供选择的每页的行数（*）
            minimumCountColumns: 2,  //最少允许的列数
            clickToSelect: true,              //是否启用点击选中行
            sortable: true,                     //是否启用排序
            silentSort: true,
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: false,
            paginationFirstText: '首页',
            paginationLastText: '末页',
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            showColumns: false,            //是否显示所有的列
            showRefresh: false,              //是否显示刷新按钮
            showToggle: false,                     //是否显示详细视图和列表视图的切换按钮
             cardView: false,                    //是否显示详细视图
            detailView: false,                   //是否显示父子表
            queryParams: function (params) {
                return {
                    pagesize: params.limit,//页面大小
                    page: params.offset, //页码
                };
            },//传递参数（*）
            columns: [
                { checkbox: "true" },
                { field: 'usercode', title: '编号' },
                {
                    field: 'fullname', title: '名称', formatter: function (value, row, index) {
                        $("#parid").val(row.parid);
                        return "<span style='cursor:pointer'>" + row.fullname + "</span>";
                    }
                },
                { field: 'Area', title: '地区' },
                { field: 'address', title: '地址' }
            ],
            onClickCell: function (field, value, row) {
                if (field == 'fullname') {
                    if (row["sonnum"] == "0") {
                        layer.msg("已到最底层", { icon: 1, time: 2000 });
                        return;
                    }
                    var selType = $("#selType option:selected").val();
                    var nameORcode = $("#nameORcode").val();
                    var parid = row["TypeID_"];
                    $("#parid").val(parid);
                    $('#businessTable').bootstrapTable('refresh', {
                        url: "../CommonSelector/GetClientName?selType=" + selType + "&parid=" + parid
                    })
                }
            }
        });
    });
    function btnselect() {
        var selectRows = $('#businessTable').bootstrapTable("getSelections");
        var btypeid = "";
        var strbtypeids = "";
        var bnum = "";
        for (var i = 0; i < selectRows.length; i++) {
            btypeid += selectRows[i].TypeID_ + "," + selectRows[i].sonnum + ";";
            if (i == selectRows.length - 1)
            { strbtypeids += "" + selectRows[i].TypeID_ + ""; bnum += "" + selectRows[i].sonnum + ""; }
            else
            { strbtypeids += "" + selectRows[i].TypeID_ + "" + ","; bnum += "" + selectRows[i].sonnum + "" + ","; }
        }
        btypeid = btypeid.substring(0, btypeid.lastIndexOf(';'));
        if (btypeid == "") {
            layer.msg("未选择任何客户", { icon: 2, time: 2000 });
            return;
        }
        parent.$("#ClientBtypeid").val(btypeid); //经销商版用户权限设置用户
        parent.$("#bnum").val(bnum);
        parent.$("#strbtypeids").val(strbtypeids);
        $.ajax({
            url: '../Shop/GetSimpleCustomeres?customeres=' + btypeid,
            dataType: "text",
            type: "post",
            success: function (data) {
                var old = parent.$("#NewSimpleCPTable").bootstrapTable("getData");//新增简单拜访页面的数据
                var old2 = parent.$("#CustomerLineTable").bootstrapTable("getData");//线路客户中线路下的客户数据
                var newarr = [];
                var newarr2 = [];
                var data1 = JSON.parse(data);

                //新增简单拜访中选择客户
                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");

                }

                //线路客户中选择客户
                if (old2.length > 0) {
                    for (var i = 0; i < data1.length; i++) {
                        var t = 0;
                        for (var j = 0; j < old2.length; j++) {
                            if (data1[i].btypeid == old2[j].btypeid) {
                                t++;
                            }
                        }
                        if (t < 1) {
                            newarr2.push(data1[i]);
                        }
                    }
                    parent.$("#CustomerLineTable").bootstrapTable('append', newarr2);
                    parent.$("#CustomerLineTable").bootstrapTable('scrollTo', 'bottom');
                    parent.$("#isornosave").val("no");
                } else {
                    parent.$("#CustomerLineTable").bootstrapTable('append', data1);
                    parent.$("#CustomerLineTable").bootstrapTable('scrollTo', 'bottom');
                    parent.$("#isornosave").val("no");

                }
                parent.layer.msg("保存成功！", { time: 2000 });
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
            },
            error: function (data) {
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }
        })
    }
    function Search() {
        var selType = $("#selType option:selected").val();
        var nameORcode = $("#nameORcode").val();
        $('#businessTable').bootstrapTable('refresh', {
            url: "../CommonSelector/GetClientName?selType=" + selType + "&nameORcode=" + nameORcode
        })
    }
</script>