@{
    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">
                <tr>
                    <td>客户名称:</td>
                    <td>
                        <input type="hidden" id="clientTypeID" />
                        <input type="text"   id="rsNum" value="" readonly="true" /></td>
                    <td><button onclick="ClientName()" class="glyphicon glyphicon-plus btn btn-default"></button>
                        @*<button class="glyphicon glyphicon-minus btn btn-default" onclick="Clear()"></button>*@
                    </td>
                    @*<td>
                        <button class="btn btn-default" id="btnSearch" onclick="Search()"><i class="glyphicon glyphicon-zoom-in"></i> 查询</button>
                    </td>*@
                    <td>
                        <button id="btnSelPtype" class="btn btn-default"><i class="fa fa-plus"></i> 选择商品</button>
                        <button id="btnSave" class="btn btn-default"><i class="glyphicon glyphicon-saved"></i> 保存设置</button>
                        <input type="hidden" id="CommodityBtypeid" />
                    </td>
                </tr>
            </table>
            <table id="businessTable"></table>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#businessTable').bootstrapTable({
            url: '../DataQuery/GetCusPtypeAlarmSet',         //请求后台的URL（*）
            method: 'post',                      //请求方式（*）
            toolbar: '',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            pagination: true,                   //是否显示分页（*）
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 15,                       //每页的记录行数（*）
            pageList: [15, 30, 50],        //可供选择的每页的行数（*）
            minimumCountColumns: 2,  //最少允许的列数
            //height: "100%",               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            clickToSelect: true,              //是否启用点击选中行
            undefinedText: " ",
            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: [
                {
                    field: "index", title: "序号", align: "center", width: 50, edit: false, formatter: function (value, row, index) {
                        return row.index = index + 1; //返回行号
                    }
                },
               { field: 'UserCode', title: '商品编号' },
               { field: 'FullName', title: '商品名称' },
               { field: 'Standard', title: '规格' },
               { field: 'Type', title: '型号' },
               { field: 'unitname', title: '单位' },
               { field: 'max', title: '上限' },
               { field: 'min', title: '下限' },
               {
                   field: 'Del', title: '删除', formatter: function (value, row, index) {
                       return "<button class='btn' style='color:red'><i class='glyphicon glyphicon-trash'></i> 删除</button>"
                   }
               }
            ], onClickCell: function (field, value, row, td) {
                if (field == "Del") {
                    $.ajax({
                        async: false,
                        url: "../DataQuery/DelCusPtypeAlarmSet",
                        data: {
                            btypeid: row["typeId"],
                            billnumberid: row["billnumberid"]
                        },
                        datatype: "text",
                        type: "post",
                        success: function (data) {
                            if (data == "ok") {
                                $('#businessTable').bootstrapTable("refresh", {
                                    url: '../DataQuery/GetCusPtypeAlarmSet?btypeid=' + $("#clientTypeID").val()
                                })
                            }
                        }
                    })
                }
                else if (field == "unitname") {
                    var typeid = row["typeId"]
                    var select;
                    $.ajax({
                        async: false,
                        url: "../WLTManage/GetUnitName",
                        data: {
                            typeid: typeid,
                        }, datatype: "text",
                        type: "post",
                        success: function (data) {
                            if (data == "err") {
                                return;
                            }
                            select = data;
                        }
                    })
                    layer.confirm(select,
                        {
                            title: "单位",
                            success: function () { /*弹框成功回调*/
                                $("#UnitName").val(row["unitid"]);
                            }
                        },
                        function (idx) { /*确定按钮 点击事件*/
                            if ($("#UnitName").find("option:selected").text().trim() == row["unitname"].trim()) {
                                layer.close(idx);
                            } else {
                                var unitName = $("#UnitName").find("option:selected").text();
                                var unitId = $("#UnitName").val();
                                $.ajax({
                                    url: "../DataQuery/UpdatePtypeAlarmSetValue",
                                    data: {
                                        ptypeid: row["typeId"],
                                        fieldValue: unitId,
                                        fieldValue2: unitName,
                                        type: '1',
                                        billnumberid: row["billnumberid"]
                                    },
                                    datatype: "text",
                                    type: "post",
                                    success: function (data) {
                                        if (data == "ok") {
                                            row["unitname"] = unitName;
                                            row["unitid"] = unitId;
                                            $('#businessTable').bootstrapTable('updateRow', { index: row.index - 1, row: row });/*更行行数据*/
                                        }
                                        layer.close(idx);
                                    }, error: function () {
                                        layer.msg("连接超时", { time: 2000 });
                                    }
                                })
                            }
                        });
                }
                else if (field == "max") {/*编辑 上限*/
                    layer.confirm("<input id=\"max\" type=\"text\" value='" + row["max"] + "' />", { title: "上限" }, function (idx) {
                        var max = $("#max").val().trim();
                        if (max == row["max"].trim()) {
                            layer.close(idx);
                        } else {
                            var reg = /^\d{1,}\.{0,1}\d{0,}$/;
                            if (reg.test(max)) {
                                max = $.NumberBzQz(max, 8);
                            } else {
                                return;
                            };
                            $.ajax({
                                url: "../DataQuery/UpdatePtypeAlarmSetValue",
                                data: {
                                    ptypeid: row["typeId"],
                                    fieldValue: max,
                                    fieldValue2: max,
                                    type: '2',
                                    billnumberid: row["billnumberid"]
                                },
                                datatype: "text",
                                type: "post",
                                success: function (data) {
                                    if (data == "ok") {
                                        row["max"] = max;
                                        $('#businessTable').bootstrapTable('updateRow', { index: row.index - 1, row: row });/*更行行数据*/
                                        
                                    }
                                    layer.close(idx);
                                }
                            })
                        }
                    });
                }
                else if (field == "min") {/*编辑 下限*/
                    layer.confirm("<input id=\"min\" type=\"text\" value='" + row["min"] + "' />", { title: "下限" }, function (idx) {
                        var min = $("#min").val().trim();
                        if (min == row["min"].trim()) {
                            layer.close(idx);
                        } else {
                            var reg = /^\d{1,}\.{0,1}\d{0,}$/;
                            if (reg.test(min)) {
                                min = $.NumberBzQz(min, 8);
                            } else {
                                return;
                            };
                            $.ajax({
                                url: "../DataQuery/UpdatePtypeAlarmSetValue",
                                data: {
                                    ptypeid: row["typeId"],
                                    fieldValue: min,
                                    fieldValue2: min,
                                    type: '3',
                                    billnumberid: row["billnumberid"]
                                },
                                datatype: "text",
                                type: "post",
                                success: function (data) {
                                    if (data == "ok") {
                                        row["min"] = min;
                                        $('#businessTable').bootstrapTable('updateRow', { index: row.index - 1, row: row });/*更行行数据*/
                                       
                                    }
                                    layer.close(idx);
                                }
                            })
                        }
                    });
                }
            }
        });
    });
    /*选择商品*/
    $("#btnSelPtype").on("click", function () {
        var empid = $("#clientTypeID").val();
        if (empid == "") {
            layer.tips('请先选择客户！', '#rsNum', {
                tips: [2, "red"]
            })
            return;
        }
        layer.open({
            type: 2,
            title: '选择商品',
            shadeClose: false,
            shade: 0.5,
            area: ['86%', '90%'],
            content: '../CommonSelector/CommoditySelectorMore',
            end: function () {
                var commodityBtypeid = $("#CommodityBtypeid").val();
                if (commodityBtypeid != "") {
                    $('#businessTable').bootstrapTable("refresh", {
                        url: '../DataQuery/GetCusPtypeAlarmSet?btypeid=' + empid + "&ptypeid=" + commodityBtypeid
                    })
                    $.ajax({
                        url: "../DataQuery/SavePtypeAlarmSet",
                        data: {
                            btypeid: $("#clientTypeID").val()
                        },
                        datatype: "text",
                        type: "post",
                        success: function (data) {
                            $('#businessTable').bootstrapTable("refresh", {
                                url: '../DataQuery/GetCusPtypeAlarmSet?btypeid=' + $("#clientTypeID").val()
                            })
                        }, error: function () {

                        }
                    })
                }
            }
        });
    });
    /*保存设置*/
    $("#btnSave").on("click", function () {
        var empid = $("#rsNum").val();
        if (empid == "") {
            layer.tips('请先选择客户！', '#rsNum', {
                tips: [2, "red"]
            })
            return;
        }
        $.ajax({
            url: "../DataQuery/CheckCusPtypeAlarmSet",
            data: {
                btypeid: $("#clientTypeID").val()
            },
            datatype: "text",
            type: "post",
            success: function (data) {
                parent.layer.msg(data, { icon: 1, time: 2000 });
                $('#businessTable').bootstrapTable("refresh", {
                    url: '../DataQuery/GetCusPtypeAlarmSet?btypeid=' + $("#clientTypeID").val()
                })
            }, error: function () {

            }
        })
    });
    //选择客户
    function ClientName() {
        layer.open({
            type: 2,
            title: '客户信息',
            shadeClose: false,
            shade: 0.5,
            area: ['80%', '70%'],
            content: '../CommonSelector/ClientSelector',
            end: function () {
                var empid = $("#clientTypeID").val();
                if (empid!="") {
                    $('#businessTable').bootstrapTable("refresh", {
                        url: '../DataQuery/GetCusPtypeAlarmSet?btypeid=' + empid
                    })
                }
            }
        });
       
    }
    //清除
    function Clear() {
        $("#rsNum").val("");
        $("#clientTypeID").val("");
    }

</script>

