@{
    ViewBag.Title = "时段促销";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="col-md-6">
                <table id="toptab" class="table table-bordered table-hover" style="margin-top: 10px">
                    <tr>
                        <td>促销商品：</td>
                        <td>
                            <input type="hidden" id="btypeid" />
                            <input type="text" class="form-control" id="PtypeFullName" disabled="disabled">
                        </td>
                        <td>
                            <button class="btn btn-default glyphicon glyphicon-plus" onclick="ShowPeriodPromotion()"></button>
                            <button class="btn btn-default glyphicon glyphicon-minus" onclick="ClearPtype()"></button>
                        </td>
                        <td>录单日期：
                        </td>
                        <td>
                            <input type="text" class="form-control" onfocus="WdatePicker({ isShowClear: false, readOnly: true})" id="rsStartDate" value="@ViewBag.pmStartDate" />
                        </td>
                        <td>
                            <input type="text" class="form-control" onfocus="WdatePicker({ isShowClear: false, readOnly: true})" id="rsEndDate" value="@ViewBag.pmEndDate" />
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table id="toptab" class="table table-bordered table-hover" style="margin-top: 10px">
                    <tr>
                        <td>促销日期：
                        </td>
                        <td>
                            <input type="text" class="form-control" onfocus="WdatePicker({isShowClear:false,readOnly:true})" id="pmStartDate" value="" />
                        </td>
                        <td>
                            <input type="text" class="form-control" onfocus="WdatePicker({isShowClear:false,readOnly:true})" id="pmEndDate" value="" />
                        </td>
                        <td>
                            <button class="btn btn-default" id="btnSearch" onclick="Search()"><i class="glyphicon glyphicon-search"></i>查  询</button>
                        </td>
                        <td>
                            <button id="btnNew" class="btn btn-default"><i class="fa fa-plus"></i>新  增</button>
                        </td>
                        <td id="btn">
                            @{
                                if (ViewBag.isHHorERPorA8)
                                {
                                <button id="btnImport" class="btn btn-default"><i class="glyphicon glyphicon-copy"></i>一键导入</button>
                                }
                                else if (ViewBag.isCM)
                                {
                                <button id="btnImportCM" class="btn btn-default"><i class="glyphicon glyphicon-copy"></i>一键导入</button>
                                <input type="hidden" id="ClientBtypeid" />
                                }
                            }
                        </td>

                    </tr>
                </table>
            </div>
            <div class="col-md-12">
                <table id="SelectPeriodPromotionTable"></table>
            </div>
        </div>
    </div>

    <input type="hidden" id="rowIndex" />
    <input type="hidden" id="pagtype" value="Add" />
    <script type="text/javascript">
        //删除促销数据
        function Del(id) {
            layer.confirm('确定删除?', { icon: 3, title: '提示', closeBtn: 2, }, function (index) {
                $.ajax({
                    url: "../WLTManage/DelPrmnotionData",
                    data: {
                        id: id
                    },
                    datatype: "text",
                    type: "post",
                    success: function (data) {
                        if (data == "ok") {
                            layer.msg("删除成功！", { icon: 1, time: 2000 });
                            $('#SelectPeriodPromotionTable').bootstrapTable("refresh");
                        } else {
                            layer.msg("操作失败", { icon: 2, time: 2000 });
                        }
                    }, error: function () {
                        layer.msg("连接超时", { icon: 2, time: 2000 });
                    }
                });
                layer.close(index);
            });
        }
        $(function () {
            $("#rsStartDate").val($.GetToday());
            $("#rsEndDate").val($.GetToday())
            $('#SelectPeriodPromotionTable').bootstrapTable({
                url: '../WLTManage/GetPromoRule',         //请求后台的URL（*）
                method: 'post',                      //请求方式（*）
                toolbar: '',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                pagination: true,                   //是否显示分页（*）
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 15,                       //每页的记录行数（*）
                pageList: [15, 30, 50],        //可供选择的每页的行数（*）
                minimumCountColumns: 2,  //最少允许的列数
                //height: $(".centerdiv").height()-20,               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "btypeid",            //每一行的唯一标识，一般为主键列
                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, //页码
                        strDateLD: $("#rsStartDate").val(),
                        endDateLD: $("#rsEndDate").val(),
                    };
                },//传递参数（*）
                columns: [
                    {
                        field: "index", title: "序号", align: "center", width: 50, edit: false, formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    { field: 'CreateDate', title: '录单日期' },
                    { field: 'BillCode', title: '单据编号' },
                    { field: 'PromoName', title: '时段促销单名称' },
                    {
                        field: 'StartDate', title: '促销日期', formatter: function (value, row, index) {
                            return row["StartDate"] + "到" + row["EndDate"];
                        }
                    },
                    {
                        field: 'StartTime', title: '促销时间', formatter: function (value, row, index) {
                            return row["StartTime"] + "到" + row["EndTime"];
                        }
                    },
                    { field: 'PromoDayDetail', title: '促销周期' },
                    { field: 'PromoPtypeQty', title: '促销品种数' },
                    { field: 'FullName', title: '制单人' },
                    { field: 'Commet', title: '摘要' },
                    {
                        field: 'Edit', title: '编辑', formatter: function (value, row, index) {
                            return "<button class='btn btn-default' onclick='javascript:indexset(" + index + ");'><i class='glyphicon glyphicon-edit'></i> 编辑</button>";
                        }
                    },
                    {
                        field: 'Del', title: '操作', formatter: function (value, row, index) {
                            return "<button class='btn btn-default' onclick=\"Del('" + row["BillNumberID"] + "')\"><i class='glyphicon glyphicon-trash' ></i> 删除</button>";
                        }
                    }
                ], onClickCell: function (field, value, row, td) {
                    if (field == "Edit") {
                        clearpromo();

                        $("#pagtype").val("Edit");
                        AddOrEdit();
                    }
                }
            });
            $(window).resize(function () { $('#SelectPeriodPromotionTable').bootstrapTable('resetView'); });
            //新增按钮
            $("#btnNew").on("click", function () {
                $("#pagtype").val("Add");
                clearpromo();
                AddOrEdit();
            })
        });
        function indexset(index) {
            $("#rowIndex").val(index);
        }
        function AddOrEdit() {
            layer.open({
                type: 2,
                title: '时段促销',
                shadeClose: false,
                shade: 0.5,
                area: ['95%', '95%'],
                content: '../WLTManage/NewPeriodPromotion',
                cancel: function (index) {
                    layer.confirm("信息<span style='color:red;'>未保存</span>,确定关闭？", { titli: "提示" }, function (idx) {
                        clearpromo();
                        layer.close(idx);
                        layer.close(index);
                    })
                    return false;
                }
            });
        }
        //选择促销商品
        function ShowPeriodPromotion() {
            layer.open({
                type: 2,
                title: '选择促销商品',
                shadeClose: false,
                shade: 0.5,
                area: ['95%', '95%'],
                content: '../CommonSelector/CommoditySelector'
            });
        }
        //清除
        function ClearPtype() {
            $("#btypeid").val("");
            $("#PtypeFullName").val("");
        }
        //查询
        function Search() {
            var ptypeId = $("#btypeid").val();
            var strDateLD = $("#rsStartDate").val();
            var endDateLD = $("#rsEndDate").val();
            var strDateCX = $("#pmStartDate").val();
            var endDateCX = $("#pmEndDate").val();
            $('#SelectPeriodPromotionTable').bootstrapTable("refresh", {
                url: '../WLTManage/GetPromoRule?ptypeId=' + ptypeId + "&strDateLD=" + strDateLD + "&endDateLD=" + endDateLD + "&strDateCX=" + strDateCX + "&endDateCX=" + endDateCX
            });
        }
        function clearpromo() {
            $.ajax({
                url: "../WLTManage/ClearPromo",
                data: {},
                datatype: "text",
                type: "post",
            })
        }
        //辉煌和分销一键导入按钮
        $("#btnImport").on("click", function () {
            layer.confirm('<table class="table table-bordered table-striped table-hover"><tr><td>开始时间：</td><td><input type="text"   onfocus="WdatePicker({isShowClear:false,readOnly:true,minDate: \'%y-%M-%d\'})" id="staDate" value="" /></td></tr>'
               + '<tr><td>结束时间：</td><td><input type="text"   onfocus="WdatePicker({isShowClear:false,readOnly:true,minDate: \'%y-%M-%d\'})" id="endDate" value="" /></td></td></tr></table>',
               { title: "导入时间段" }, function (index) {
                   var staDate = $("#staDate").val();
                   var endDate = $("#endDate").val();
                   if (staDate != "" && endDate != "") {
                       if (!$.DateComparison(staDate, endDate)) {
                           layer.tips('结束时间不能小于开始时间', '#endDate', { tips: [1, "red"], icon: 1, time: 2000 })
                           return false;
                       }
                   }
                   else {
                       if (staDate == "") {
                           layer.tips('请选择日期', '#staDate', { tips: [1, "red"], icon: 1, time: 2000 })
                       } else if (endDate == "") {
                           layer.tips('请选择日期', '#endDate', { tips: [1, "red"], icon: 1, time: 2000 })
                       }
                       return false;
                   }
                   $.ajax({
                       url: "../WLTManage/ImportPeriodPromotionData",
                       data: {
                           staDate: staDate,
                           endDate: endDate,
                           btypeids: ''
                       },
                       datatype: "text",
                       type: "post",
                       success: function (data) {
                           if (data == "ok") {
                               $('#SelectPeriodPromotionTable').bootstrapTable("refresh");
                               layer.msg("导入成功!", { time: 2000 });
                               layer.close(index);
                           }
                           else {
                               layer.msg(staDate + "至" + endDate + "期间无促销记录!", { time: 2000 });
                           }
                           layer.close(index);
                       }, error: function () {
                           layer.close(index);
                       }
                   });
               })
        })

        //财贸一键导入按钮
        $("#btnImportCM").on("click", function () {
            layer.open({
                type: 2,
                title: '客户信息',
                shadeClose: false,
                shade: 0.5,
                area: ['80%', '70%'],
                content: '../CommonSelector/ClientSelectorMore',
                end: function () {
                    var btypeids = $("#ClientBtypeid").val();
                    $.ajax({
                        url: "../WLTManage/ImportPeriodPromotionData",
                        data: {
                            btypeids: btypeids
                        },
                        datatype: "text",
                        type: "post",
                        success: function (data) {
                            if (data == "ok") {
                                $('#SelectPeriodPromotionTable').bootstrapTable("refresh");
                                layer.msg("导入成功!", { time: 2000 });
                                layer.close(index);
                            }
                            else {

                                layer.msg("没有新的客户协议价!", { time: 2000 });
                            }
                            layer.close(index);
                        }, error: function () {
                            layer.close(index);
                        }
                    });
                }
            });
        })
    </script>
</body>
