@{
    ViewBag.Title = "客户变价销售表";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<div class="topdiv">
    <div class="ibox-content">
        <table class="table table-bordered table-striped table-hover">
            <tr>
                <td>日期:</td>
                <td>
                    <input type="text" class="form-control" onfocus="WdatePicker({isShowClear:false,readOnly:true})" id="pmStartDate" value="@ViewBag.pmStartDate" />
                </td>
                <td>
                    <input type="text" class="form-control" onfocus="WdatePicker({isShowClear:false,readOnly:true})" id="pmEndDate" value="@ViewBag.pmEndDate" />
                </td>
                <td>业务员:</td>
                <td>
                    <input id="btypeid" type="hidden" />
                    <input type="text" class="form-control" id="Ufullname" readonly="true" />
                </td>
                <td>
                    <button onclick="ClientEName()" class="glyphicon glyphicon-plus btn btn-default"></button>
                    <button onclick="ClearE()" class="glyphicon glyphicon-minus btn btn-default"></button>
                </td>
                <td>客户:</td>
                <td>
                    <input id="uploadliginid" type="hidden" />
                    <input type="text" class="form-control" id="rsNum" readonly="true" />
                </td>
                <td>
                    <button onclick="ClientName()" class="glyphicon glyphicon-plus btn btn-default"></button>
                    <button onclick="Clear()" class="glyphicon glyphicon-minus btn btn-default"></button>
                </td>
                <td>
                    <input type="hidden" id="rowIndex" />
                    <button class="btn btn-default" id="btnSearch"><i class="glyphicon glyphicon-search"></i> 查询</button>
                </td>
                <td>
                    <button class="btn btn-default" id="btnExcel"><i class="glyphicon glyphicon-download-alt"></i> 导出</button>
                       <input id="hidCount" type="hidden" />
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="centerdiv">
    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins">
            <div class="ibox-content form-inline ">
                <table id="GetSaleReportPriceTable"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#GetSaleReportPriceTable').bootstrapTable({
            url: '../DataQuery/GetSaleReportPriceList',         //请求后台的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: $(".centerdiv").height()-20,               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "nub",            //每一行的唯一标识，一般为主键列
            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, //页码
                    stardate: $("#pmStartDate").val(),
                    enddate: $("#pmEndDate").val(),
                };
            },//传递参数（*）
            columns: [
                {
                    field: "index", title: "序号",width:100, edit: false, formatter: function (value, row, index) {
                        if (value) {
                            return value;
                        }
                        return row.index = index + 1; //返回行号
                    }
                },
               { field: 'BillDate', title: '单据日期' },
               { field: 'BillCode', title: '单据编号' },
               { field: 'BillName', title: '单据类型' },
               { field: 'BFullName', title: '往来单位' },
               { field: 'EFullName', title: '业务员' },
                { field: 'KFullname', title: '仓库' },
               { field: 'SaleTotal', title: '合计金额' },
               { field: 'NewTotal', title: '原价金额' },
               { field: 'Total', title: '变价差额' },
               { field: 'TotalMoney', title: '收款金额' },
               { field: 'Comment', title: '附加说明' },
               
               {
                   field: "state", title: "是否核对", formatter: function (value, row, index) {//是否核对
                       if (row["index"] == "合计：") {
                           return "";
                       }
                       if (row["STATE"] == '1') {
                           var strHtml = '<i class="glyphicon glyphicon-ok"></i>';
                           return strHtml;
                       } else {
                           var strHtml = '<a href="javascript:void(0);" onclick="IsCheck(\'' + row["BillNumberId"] + '\',\'' + row["STATE"] + '\')"><i class="glyphicon glyphicon-remove"></i></a>';
                           return strHtml;
                       }
                   }
               },
               {
                   field: "View", title: "单据明细", formatter: function (value, row, index) {
                       if (row["index"] == "合计：") {
                           return "";
                       }
                       return "<button class='btn'><i class='glyphicon glyphicon-edit'></i> 查看</button>";
                   }
               }
            ], onClickCell: function (field, value, row) {
                if (field == "View") {
                    $("#rowIndex").val(row["index"] - 1);
                    layer.open({
                        type: 2,
                        title: '<i class="glyphicon glyphicon-list"></i> 详情',
                        shadeClose: false,
                        shade: 0.5,
                        area: ['90%', '90%'],
                        content: '../DataQuery/SaleReportPriceDetail?billid=' + row["BillNumberId"]
                    })
                }
            },
            onLoadSuccess: function (data) {
                $("#hidCount").val(data.total);
                if (data.total < 1) return;
                var datatab = $('#GetSaleReportPriceTable').bootstrapTable("getData");
                var SaleTotal = 0;
                var NewTotal = 0;
                var Total = 0;
                var TotalMoney = 0;
                for (var i = 0; i < datatab.length; i++) {
                    if (datatab[i]["SaleTotal"]) {
                        SaleTotal = SaleTotal + parseFloat(datatab[i]["SaleTotal"]);
                    } if (datatab[i]["NewTotal"]) {
                        NewTotal = NewTotal + parseFloat(datatab[i]["NewTotal"]);
                    } if (datatab[i]["Total"]) {
                        Total = Total + parseFloat(datatab[i]["Total"]);
                    } if (datatab[i]["TotalMoney"]) {
                        TotalMoney = TotalMoney + parseFloat(datatab[i]["TotalMoney"]);
                    }
                }
                var data1 = [{
                    "BillNumberId": "",
                    "SaleTotal": SaleTotal,
                    "NewTotal": NewTotal,
                    "Total": Total,
                    "Comment": "",
                    "BillCode": "",
                    "BFullName": "",
                    "KFullname": "",
                    "EFullName": "",
                    "BillName": "",
                    "BillDate": "",
                    "TotalMoney": TotalMoney,
                    "STATE": "", "index": "合计："
                }];
                $('#GetSaleReportPriceTable').bootstrapTable("append", data1);
            }
        });
        $(window).resize(function () {
            $('#GetSaleReportPriceTable').bootstrapTable('resetView');
        });
    });


    //是否核对数据
    function IsCheck(billid, state) {
        $.ajax({
            url: "../DataQuery/IsCheckReport",
            dataType: "text",
            data: { billid: billid },
            type: "post",
            success: function (data) {//操作成功后刷新table
                if (data == "操作成功！") {
                    if (state == "1") {
                        layer.msg("未核对", { time: 2000 });
                        $('#GetSaleReportPriceTable').bootstrapTable("refresh");
                    }
                    else {
                        layer.msg("已核对", { time: 2000 });
                        $('#GetSaleReportPriceTable').bootstrapTable("refresh");
                    }
                } else {
                    layer.msg(data, { time: 2000 });
                    $('#GetSaleReportPriceTable').bootstrapTable("refresh");
                }
            },
            error: function (data) {
                layer.msg(data, { time: 2000 });
            }
        })
    }

    //详情
    function Detail(billid) {
        layer.open({
            type: 2,
            title: '<i class="glyphicon glyphicon-list"></i> 详情',
            shadeClose: false,
            shade: 0.5,
            area: ['80%', '80%'],
            content: '../DataQuery/SaleReportPriceDetail?billid=' + billid
        })
    }

    //Excel导出
    $("#btnExcel").on("click", function () {
        var stardate = $("#pmStartDate").val();
        var enddate = $("#pmEndDate").val();
        var userid = $("#btypeid").val();
        var btypeid = $("#uploadliginid").val();
        window.location.href = '../DataQuery/GetExcelSaleReportPriceList?stardate=' + stardate + "&enddate=" + enddate + "&userid=" + userid + "&btypeId=" + btypeid + "&pagesize=" + parseInt($("#hidCount").val()) + "&page=0"
    });

    //列表查询
    $("#btnSearch").on("click", function () {
        var stardate = $("#pmStartDate").val();
        var enddate = $("#pmEndDate").val();
        var userid = $("#btypeid").val();
        var btypeid = $("#uploadliginid").val();
        $('#GetSaleReportPriceTable').bootstrapTable("refresh", {
            url: "../DataQuery/GetSaleReportPriceList?stardate=" + stardate + "&enddate=" + enddate + "&userid=" + userid + "&btypeId=" + btypeid
        })
    });

    //选择客户
    function ClientName() {
        layer.open({
            type: 2,
            title: '@ViewBag.Title 客户信息',
            shadeClose: false,
            shade: 0.5,
            area: ['80%', '70%'],
            content: '../CommonSelector/ClientSelector'
        });
    }

    //清除选择的客户
    function Clear() {
        $("#rsNum").val("");
        $("#uploadliginid").val("");
    }

    //选择业务员
    function ClientEName() {
        layer.open({
            type: 2,
            title: '@ViewBag.Title 业务员',
            shadeClose: false,
            shade: 0.5,
            area: ['80%', '70%'],
            content: '../CommonSelector/EmployeeSelector'
        });
    }

    //清除选择的业务员
    function ClearE() {
        $("#btypeid").val("");
        $("#Ufullname").val("");
    }
</script>
