@{
    ViewBag.Title = "捐赠方式";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="col-md-6">
                <table class="table table-bordered table-striped 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>
                            <span class="glyphicon glyphicon-plus btn btn-default" onclick="ShowPeriodPromotion()"></span>
                            <span class="glyphicon glyphicon-minus btn btn-default" onclick="ClearPtype()"></span>
                        </td>
                        <td>
                            录单日期：
                        </td>
                        <td>
                            <input type="text" class="form-control" onfocus="WdatePicker({isShowClear:false,readOnly:true})" id="rsStartDate" value="" />
                        </td>
                        <td>
                            <input type="text" class="form-control" onfocus="WdatePicker({isShowClear:false,readOnly:true})" id="rsEndDate" value="" />
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-bordered table-striped 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="fa fa-search"></i> 查 询</button>
                        </td>
                        <td>
                            <button id="btnNew" class="btn btn-default"><i class="fa fa-plus"></i> 新 增</button>
                        </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/GetPromoGive',         //请求后台的URL（*）
                method: 'post',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                pagination: true,                   //是否显示分页（*）
                sortOrder: "asc",                   //排序方式
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 15,                       //每页的记录行数（*）
                pageList: [15, 30, 50],        //可供选择的每页的行数（*）
                minimumCountColumns: 2,  //最少允许的列数
                //height: $(".centerdiv").height() - 35,                //行高，如果没有设置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: "Nid", title: "序号", align: "center", width: 50, edit: false, formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    { field: 'CreateDate', title: '录单日期' },
                    { field: 'BillCode', title: '单据编号' },
                    { field: 'PromoName', title: '促销单名称', width: "10%" },
                    {
                        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 button-rounded btn-sm' onclick='javascript:indexset("+index+")'><i class='glyphicon glyphicon-edit'></i> 编辑</button>";
                        }
                    },
                    {
                        field: 'Del', title: '操作', formatter: function (value, row, index) {
                            return "<button class='btn button-rounded btn-sm' 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();
            })
            //$('#collapseOne').on('shown.bs.collapse', function () {
            //    $('#SelectPeriodPromotionTable').bootstrapTable('resetView');
            //    document.getElementById("footertitle").innerHTML = "<i class='glyphicon glyphicon-chevron-up'></i> 折叠操作栏";
            //})
            //$('#collapseOne').on('hidden.bs.collapse', function () {
            //    $('#SelectPeriodPromotionTable').bootstrapTable('resetView');
            //    document.getElementById("footertitle").innerHTML = "<i class='glyphicon glyphicon-chevron-down'></i> 显示操作栏";
            //})
        });
        function indexset(index) {
            $("#rowIndex").val(index);
        }
        /*编辑新增*/
        function AddOrEdit() {
            layer.open({
                type: 2,
                title: '搭赠方式',
                shadeClose: false,
                shade: 0.5,
                area: ['95%', '95%'],
                content: '../WLTManage/NewWaysOfGiving',
                cancel: function (index) {
                    layer.confirm("信息<span style='color:red;'>未保存</span>,确定关闭？", { titli: "提示"}, function (idx) {
                        clearpromo();
                        layer.close(idx);
                        layer.close(index);
                    })
                    return false;
                }
            });
        }
        /*清空临时表信息*/
        function clearpromo() {
            $.ajax({
                url: "../WLTManage/ClearPromo",
                data: {},
                datatype: "text",
                type: "post",
            })
        }
        //
        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/GetPromoGive?ptypeId=' + ptypeId + "&strDateLD=" + strDateLD + "&endDateLD=" + endDateLD + "&strDateCX=" + strDateCX + "&endDateCX=" + endDateCX
            });
        }

        //一键导入按钮
        $("#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/ImportWaysOfGivingData",
                  data: {
                      staDate: staDate,
                      endDate: endDate
                  },
                  datatype: "text",
                  type: "post",
                  success: function (data) {
                      if (data == "ok") {
                          $('#SelectPeriodPromotionTable').bootstrapTable("refresh");
                          parent.layer.msg("导入成功!", { time: 2000 });
                      }
                      else {
                          layer.msg(staDate + "至" + endDate + "期间无搭赠记录!", { time: 2000 });
                      }
                      layer.close(index);
                  }, error: function () {
                      layer.close(index);
                  }
              });
          });
        })
    </script>
</body>