@{
    ViewBag.Title = "新增拜访规划";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<head>
    <link href="~/BootStrap/ReOrder/bootstrap-table-reorder-rows.css" rel="stylesheet" />
</head>
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="col-lg-4 col-md-4 col-sm-12" style="margin-top:5px">
                <table class="table table-bordered table-striped table-hover">
                    <tr>
                        <td style="height:55px">
                            拜访时间：<input type="text" class="form-control" style="width:200px" onFocus="WdatePicker({isShowClear:true,readOnly:true})" id="txtDate" value="@ViewBag.date" />
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-12" style="margin-top:5px">
                <table class="table table-bordered table-striped table-hover">
                    <tr>
                        <td style="height:55px">
                            <select id="ddlSalesman" data-placeholder="请选择业务员" class="chosen-select" style="width: 200px">
                                <option value=""></option>
                                @{
                                    if (@ViewBag.ulist != null && @ViewBag.ulist.Rows.Count > 0)
                                    {
                                        for (int i = 0; i < @ViewBag.ulist.Rows.Count; i++)
                                        {
                                            <option @(@ViewBag.ulist.Rows[i]["TypeID_"].ToString() == @ViewBag.etypeid ? "selected" : "") value=@ViewBag.ulist.Rows[i]["TypeID_"].ToString()>@ViewBag.ulist.Rows[i]["FullName"].ToString()</option>
                                        }
                                    }
                                }
                            </select>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-12" style="margin-top:5px">
                <table class="table table-bordered table-striped table-hover">
                    <tr>
                        <td style="text-align:right;height:55px">
                            <button id="btnSave" onclick="Save()" class="btn btn-default"><i class="glyphicon glyphicon-save"></i>保存</button>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12">
                <input type="hidden" id="etypeid" />
                <input type="hidden" id="btypeid" />
                <input type="hidden" id="bnum" />
                <input type="hidden" id="strbtypeids" />
                <div id="toolbar">
                    <div class="alert alert-info alert-dismissable">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        <strong>注意! </strong>按住鼠标可拖动排序.修改后需保存提交
                    </div>
                    <button id="btnNewCallCustomer" onclick="NewCallCustomer()" class="btn btn-default"><i class="glyphicon glyphicon-plus-sign"></i>新增拜访客户</button>
                </div>
                <table id="NewCallPlanningTable" data-use-row-attr-func="true" data-reorderable-rows="true"></table>
                <input id="billid" type="hidden" value="@ViewBag.billid" />
            </div>
        </div>
    </div>
    <script type="text/javascript">
        function today() {//取当前时间
            var today = new Date();
            var h = today.getFullYear();
            var m = today.getMonth() + 1;
            var d = today.getDate();
            if (m < 10) {
                m = "0" + m;
            }
            if (d < 10) {
                d = "0" + d;
            }
            return h + "-" + m + "-" + d;
        }
        function Save() {//保存
            $("#btnSave").attr("disabled", true);
            var data = $("#NewCallPlanningTable").bootstrapTable("getData");
            var obj = document.getElementById("ddlSalesman"); //定位id
            var index = obj.selectedIndex; // 选中索引
            var etypename = obj.options[index].text; // 选中文本
            var cusinfo = "";
            if ($.trim($("#txtDate").val()).length <= 0) {
                layer.msg("请选择拜访时间！"); return;
            }
            if ($.trim($("#ddlSalesman").val()).length <= 0) {
                layer.msg("请选择业务员！"); return;
            }
            if ($('#NewCallPlanningTable').bootstrapTable('getData').length <= 0) {
                layer.msg("请选择客户线路后再提交保存！");
            } else {
                for (var i = 0; i < data.length; i++) {
                    if (i == data.length - 1) {
                        cusinfo += data[i].LODEID + ":" + data[i].FULLNAME + ":" + data[i].CustomerName + ":" + data[i].Customerid + ":" + data[i].area + ":" + data[i].Address;
                    } else {
                        cusinfo += data[i].LODEID + ":" + data[i].FULLNAME + ":" + data[i].CustomerName + ":" + data[i].Customerid + ":" + data[i].area + ":" + data[i].Address + ",";
                    }
                }
                $.ajax({
                    url: "../Shop/SaveSeniorCallPlanning",
                    dataType: "text",
                    data: {
                        date: $("#txtDate").val(),
                        etypeid: $.trim($("#ddlSalesman").val()),
                        etypename: etypename,
                        billid: $("#billid").val(),
                        customer: cusinfo

                    },
                    type: "post",
                    success: function (data) {
                        parent.layer.msg(data);
                        parent.$('#CallPlanningTable').bootstrapTable("refresh");
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    },
                    error: function (data) {
                        layer.msg(data);
                    }
                })
            }
        }
        function Del(customerid) {//行删除按钮
            layer.confirm("确认删除？", function (enter) {//删除前提示
                if (enter) {
                    $("#NewCallPlanningTable").bootstrapTable('removeByUniqueId', customerid);
                    parent.$("#isornodel").val("yes");
                    layer.msg("删除成功！", { time: 2000 });
                    var index = layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    layer.close(index);
                }
            })
        }
        function NewCallCustomer() {//新增拜访客户
            if ($.trim($("#txtDate").val()).length <= 0) {
                layer.msg("请选择拜访时间！", { time: 2000 }); return;
            }
            if ($.trim($("#ddlSalesman").val()).length <= 0) {
                layer.msg("请选择业务员！", { time: 2000 }); return;
            }
            var cusid = '@ViewBag.cusid';
            var calldate = '@ViewBag.calldate'
            var cusids = cusid.split(",");
            var calldates = calldate.split(",");
            var olddate = '@ViewBag.date';
            if (olddate != $("#txtDate").val()) {
                for (var i = 0; i < cusids.length; i++) {
                    if (cusids[i] == $("#ddlSalesman").val() && calldates[i] == $("#txtDate").val()) {
                        layer.msg("已存在该时间段的拜访！", { time: 2000 }); return;
                    }
                }
            }
            layer.open({
                type: 2,
                title: '<i class="glyphicon glyphicon-plus-sign"></i> 新增拜访客户',
                shadeClose: false,
                shade: 0.5,
                area: ['90%', '95%'],
                content: '../Shop/V_SelectCallPlanning?etypeid=' + $.trim($("#ddlSalesman").val()) + '&date=' + $("#txtDate").val(),
                end: function () {//关闭弹窗后

                }
            });
        }
        $(function () {
            if ("@ViewBag.billid" == "") {
                $("#txtDate").val(today());
            }
            $('#NewCallPlanningTable').bootstrapTable({//线路信息
                url: '../Shop/GetNewCallPlanningList?billid=' + "@ViewBag.billid" + '&etypeid=' + "@ViewBag.etypeid" + '&date=' + "@ViewBag.date",         //请求后台的URL（*）
                method: 'post',                      //请求方式（*）
                toolbar: 'toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                pagination: false,                   //是否显示分页（*）
                sidePagination: "server",
                minimumCountColumns: 0,  //最少允许的列数
                //height: "100%",               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "Customerid",            //每一行的唯一标识，一般为主键列
                clickToSelect: true,              //是否启用点击选中行s
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: false,
                undefinedText: ' ',
                showColumns: false,            //是否显示所有的列
                showRefresh: false,              //是否显示刷新按钮
                showToggle: false,                     //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: [
                    { field: "num", title: "num" },
                    { field: "BillId", title: "BillId" },
                    { field: "sort", title: "sort" },
                    { field: "LODEID", title: "LODEID" },
                    {
                        title: '序号', width: '3%', align: 'center',
                        formatter: function (value, row, index) {
                            var pageNumber = $('#NewCallPlanningTable').bootstrapTable('getOptions').pageNumber;
                            var pageSize = $('#NewCallPlanningTable').bootstrapTable('getOptions').pageSize;
                            return (pageNumber - 1) * pageSize + index + 1;
                        }
                    },
                    //{ field: "BillDate", title: "日期", width: '10%' },
                    { field: "FULLNAME", title: "线路名称", width: '15%', align: 'center' },
                    { field: "CustomerName", title: "客户名称", width: '15%', align: 'center' },
                    { field: "Customerid", title: "Customerid" },
                    //{ field: "typename", title: "客户类型", width: '10%' },
                    {
                        field: "area", title: "地区", width: '20%', align: 'center', formatter: function (value, row, index) {
                            if (value == "null" || value == "undefined" || value == "") {
                                return "";
                            } else if (value) {
                                return value;
                            }
                            else {
                                return "";
                            }
                        }
                    },
                    { field: "Address", title: "地址", width: '30%' },
                    {
                        field: "Edit", title: "操作", width: '10%', align: 'center', formatter: function (value, row, index) {
                            var strHtml = '<button id="btnDelete" class="btn btn-default" onclick="Del(\'' + row.Customerid + '\')"><i class="glyphicon glyphicon-trash"></i> 删除</button>';
                            return strHtml;
                        }
                    }
                ],
                onLoadSuccess: function () {
                }
            });
            //隐藏列
            $('#NewCallPlanningTable').bootstrapTable('hideColumn', 'num');
            $('#NewCallPlanningTable').bootstrapTable('hideColumn', 'sort');
            $('#NewCallPlanningTable').bootstrapTable('hideColumn', 'BillId');
            $('#NewCallPlanningTable').bootstrapTable('hideColumn', 'Customerid');
            $('#NewCallPlanningTable').bootstrapTable('hideColumn', 'LODEID');
        });
    </script>
</body>
