@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>
    .formTitle {
        text-align: right;
        width: 10%;
    }

    .formActions {
        text-align: left;
        width: 15%;
    }

    .project-list table tr td {
        padding: 5px;
    }

    .th_classAdd {
        text-align: center !important;
    }

    .th_classMar {
        padding: 10px;
        vertical-align: inherit;
    }

    .ui-jqgrid .ui-jqgrid-view {
        height: 500px;
    }

    .ui-jqgrid .ui-jqgrid-bdiv {
        height: 500px !important;
    }

    .bootstrap-select:not([class*="col-"]):not([class*="form-control"]):not(.input-group-btn) {
        width: 100%;
    }

    .lr-layout-center {
        padding: 0 5px;
    }

    .th_classMar {
        padding: 0;
    }

    .lr-layout-tool {
        height: 50px;
    }

    .lr-layout-center .lr-layout-wrap.lr-layout-wrap-notitle {
        padding-top: 50px;
    }

    .wrapper .nav-tabs {
        padding: 0;
    }

    .webuploader-pick {
        padding: 7px 15px;
    }

    .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
        padding: 3px;
    }

    .form .formTitle {
        padding-right: 8px;
    }

    .form-control, .single-line {
        padding: 3px;
    }

    .ibox-title {
        height: 20px;
        line-height: 20px;
        padding: 0;
        padding-left: 15px;
    }

    .table {
        margin-bottom: 0;
    }

    .bootstrap-select > .dropdown-toggle {
        height: 22px;
        padding: 2px 6px;
    }

        .bootstrap-select > .dropdown-toggle.bs-placeholder, .bootstrap-select > .dropdown-toggle.bs-placeholder:hover, .bootstrap-select > .dropdown-toggle.bs-placeholder:focus, .bootstrap-select > .dropdown-toggle.bs-placeholder:active {
            height: 22px;
            padding: 2px 6px;
        }

    .form-control, .single-line {
        padding: 3px;
        height: 22px;
        padding: 0 3px;
    }
</style>
<link href="~/Content/js/select2/select2.min.css" rel="stylesheet" />
<script src="~/Content/js/select2/select2.min.js"></script>
<script src="~/Content/js/layer/layer.js"></script>
<script>
    var BID = $.request("ID");
    var InvCode = $.request("InvCode");
    var GroupName = decodeURI($.request("GroupName"));
  
    var Flag = '';
    $(function () {
        debugger;
        InitControl1();
        if (BID != "") {
            $("#txtInvCode").val(InvCode);
          
            $("#sel_GroupCode option:contains('" + GroupName + "')").attr('selected', true).trigger("change");
            gridList2();
        } else {
            gridList();
        }
        $("#sel_GroupCode").change(function () {
            var InvCode = $("#txtInvCode").val();
            if (InvCode=='') {
                $.modalAlertNew("WMS00042");
                return;
            }
            debugger;
            jQuery("#gridList").jqGrid("clearGridData");
            var ID = $("#sel_GroupCode").children('option:selected').val();
            $.ajax({
                url: "/WMS/BasicSettings/GetInspectionListInfo?ID=" + ID,
                dataType: "json",
                async: false,
                success: function (data) {
                    debugger;
                    if (data == "" || data == null) {
                        $.modalAlertNew("WMS00022");
                        return;
                    }
                   
                    for (var i = 0; i < data.rows.length; i++) {
                        var obj = {
                            ID: data.rows[i].ID,
                            ListCode: data.rows[i].ListCode,
                            ListName: data.rows[i].ListName,
                            Unit: data.rows[i].Unit,
                            SetValueMax: data.rows[i].SetValueMax,
                            SetValueMin: data.rows[i].SetValueMin,
                            Enable: data.rows[i].Enable,
                        };
                        $("#gridList").jqGrid('addRowData', obj.ID, obj, 'first');
                    }

                    //最后一行新增数据
                    $(".unwritten").hide();


                }


            });

        });
    })


        function gridList() {
            var $gridList = $("#gridList");
            $gridList.dataGrid({
                url: "/WMS/BasicSettings/GetICSInventoryInspectionList?" + Math.random(),
                height: $(window).height() -50,
                width: $(window).width() - 400,
                cellEdit: true,
                cellsubmit: "clientArray",
                colModel: [
                    { label: "主键", name: "ID", hidden: true, key: true },
                    { label: '检验项目代码', name: 'ListCode', width: 135, align: 'left', editable: true, },
                    { label: '检验项目名称', name: 'ListName', width: 135, align: 'left', editable: true, },
                    { label: '单位', name: 'Unit', width: 135, align: 'left', editable: true, },
                    { label: '标准上限', name: 'SetValueMax', width: 135, align: 'left', editable: true, },
                    { label: '标准下限', name: 'SetValueMin', width: 135, align: 'left', editable: true, },
                   // { label: '是否启用', name: 'Enable', width: 135, align: 'left', editable: true, },
                    { label: '是否启用', name: 'Enable', width: 80, align: 'center', edittype: 'select', editoptions: { value: GetItemCode(), }, editable: true },
                ],
                afterSaveCell: function (rowid, cellname, value) {
                    if (cellname === 'ProjectCN') {
                        $gridList.jqGrid("setCell", rowid, 'ProjectID', value);
                    }
                },
                width: "100%",

                autowidth: true,
                rownumbers: true,
                viewrecords: true,
            });
        }
        function gridList2() { 
            var $gridList = $("#gridList");
            $gridList.dataGrid({
                url: "/WMS/BasicSettings/GetICSInventoryInspectionList2?BID=" + BID + "&" + Math.random(),
                height: $(window).height() - 50,
                width: $(window).width() - 400,
                cellEdit: true,
                cellsubmit: "clientArray",
                colModel: [
                  { label: "主键", name: "ID", hidden: true, key: true },
                    { label: '检验项目代码', name: 'ListCode', width: 135, align: 'left', editable: true, },
                    { label: '检验项目名称', name: 'ListName', width: 135, align: 'left', editable: true, },
                    { label: '单位', name: 'Unit', width: 135, align: 'left', editable: true, },
                    { label: '标准上限', name: 'SetValueMax', width: 135, align: 'left', editable: true, },
                    { label: '标准下限', name: 'SetValueMin', width: 135, align: 'left', editable: true, },
                   // { label: '是否启用', name: 'Enable', width: 135, align: 'left', editable: true, },
                    { label: '是否启用', name: 'Enable', width: 80, align: 'center', edittype: 'select', editoptions: { value: GetItemCode(), }, editable: true },
                ],
                afterSaveCell: function (rowid, cellname, value) {
                    if (cellname === 'ProjectCN') {
                        $gridList.jqGrid("setCell", rowid, 'ProjectID', value);
                    }
                },
                width: "100%",

                autowidth: true,
                rownumbers: true,
                viewrecords: true,
            });
        }


        function guid() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
        }

        function AddRows_ColorFastness() {
            var obj = {
                ID: guid(),
                ListCode: "",
                ListName: "",
                SetValueMax: "",
                SetValueMin: "",
                Enable: "",
            };
            $("#gridList").jqGrid('addRowData', obj.ID, obj, 'last');
            $(".unwritten").hide();
        } gridList
        function DelRows_ColorFastness() {
            var rowid = $("#gridList").jqGridRowValue().ID

            $.ajax({
                url: "/WMS/BasicSettings/DeleteINvItem?" + Math.random(),
                data: { ID: rowid },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#gridList").delRowData(rowid);

                }
            });
        }

        function submitForm() {
            debugger;
            $("#gridList").find($("td[aria-describedby='gridList_rn']")).click();//提交取消编辑状态
            var InvCode = $("#txtInvCode").val();
            var ID = $("#sel_GroupCode").children('option:selected').val();
            var Enables = '';
            if (InvCode == '') {
                $.modalAlertNew("WMS00010");
                return;
            }
            if (ID == '') {
                $.modalAlertNew("WMS00011");
                return;
            }
            var obj_Item1 = $("#gridList");
            var rowIds_Item1 = obj_Item1.getDataIDs();
            var arrayData_Item1 = new Array();
            if (rowIds_Item1.length > 0) {
                debugger;
                for (var i = 0; i < rowIds_Item1.length; i++) {
                    var RowData_Item1 = obj_Item1.getRowData(rowIds_Item1[i]);
                    if (RowData_Item1.ListCode == '') {
                        $.modalAlertNew("WMS00012");
                        return;
                    }
                    if (RowData_Item1.ListName == '') {
                        $.modalAlertNew("WMS00012");
                        return;
                    }
                    if (RowData_Item1.Unit == '') {
                        $.modalAlertNew("WMS00012");
                        return;
                    }
                    if (RowData_Item1.SetValueMax == '') {
                        $.modalAlertNew("WMS00012");
                        return;
                    }
                    if (RowData_Item1.SetValueMin == '') {
                        $.modalAlertNew("WMS00012");
                        return;
                    }
                    if (RowData_Item1.Enable == '') {
                        $.modalAlertNew("WMS00012");
                        return;
                    }
                    if (RowData_Item1.Enable == '是') {
                        Enables = '1';
                    } else {
                        Enables = '0';
                    }
                    
                    var obj = {
                        ID: RowData_Item1.ID,
                        ListCode: RowData_Item1.ListCode,
                        ListName: RowData_Item1.ListName,
                        Unit: RowData_Item1.Unit,
                        SetValueMax: RowData_Item1.SetValueMax,
                        SetValueMin: RowData_Item1.SetValueMin,
                        Enable: Enables,
                    };
                    arrayData_Item1.push(obj);
                }
            }
            var arrayItem1 = JSON.stringify(arrayData_Item1);
            debugger;

            if (BID != "") {
                $.ajax({
                    url: "/WMS/BasicSettings/UpdateICSInventoryInspectionGroup?BID=" + BID + "&InvCode=" + InvCode + "&ID=" + ID + "&" + Math.random(),

                    data: {
                        arrayShellFabricItem: arrayItem1,
                    },
                    //type: "post",
                    dataType: "json",
                    success: function (data) {
                        layer.msg(data.message);
                        if (data.message == '修改成功!') {
                            window.setTimeout(function () {
                                $.modalClose();
                            }, 2500);
                        }
                       
                        $.currentWindow().$("#gridList").trigger("reloadGrid");
                    },

                });
            } else {
                $.ajax({
                    url: "/WMS/BasicSettings/SubICSInventoryInspectionGroupAdd?ID=" + ID + "&InvCode=" + InvCode + "&" + Math.random(),
                    data: {
                        arrayShellFabricItem: arrayItem1,
                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        layer.msg(data.message);
                        if (data.message == '保存成功!') {
                            window.setTimeout(function () {
                                $.modalClose();
                            }, 2500);
                        }
                        $.currentWindow().$("#gridList").trigger("reloadGrid");
                    },

                });
            }


        }


        function btnSearch() {
            $.modalOpen({
                id: "ICSSearchInventory",
                title: "查询物料",
                url: "/WMS/BasicSettings/ICSSearchInventory?" + Math.random(),
                width: "1000px",
                height: "1000px",
                callBack: function (iframeId) {
                    var obj = top.frames[iframeId].submitForm();
                    $("#txtInvCode").val(obj.invcode);
                    top.frames[iframeId].Close();
                }

            })
        }

        function InitControl1() {
            var $GroupCode = $("#sel_GroupCode");
            $GroupCode.select2({
                allowClear: true,
                escapeMarkup: function (m) {
                    return m;
                }
            });
            $.ajax({
                url: "/WMS/BasicSettings/GetGroupCode" + "?" + Math.random(),
                dataType: "json",
                async: false,
                success: function (data) {
                    $.each(data, function (index, item) {
                        $GroupCode.append("<option value='" + item.ID + "'>&nbsp;" + item.GroupName + "</option>");
                    });
                }
            });
        }

        function GetItemCode() {
            var str = ":;是:是;否:否";

            /*debugger;*/
            return str;
        }

</script>

<form id="form1">

    <div style="padding-top: 20px; margin-right: 20px;">
        <div class="ibox-title">
            主表
        </div>
        <table class="form">
            <tr>
                <th class="formTitle">料品编码:</th>
                <td class="formValue">
                    <input id="txtInvCode" type="text" class="form-control"  style="width: 200px;" disabled="disabled" >
                </td>
                <td class="formValue">
                    <a id="Getinv" authorize="yes" style="margin-left:3px;" class="btn btn-info" onclick="btnSearch()"><i class="fa fa-pencil-square-o"></i>查询</a>
                </td>
                <th class="formTitle">检验项目组:</th>
                <td class="formValue">
                    <select id="sel_GroupCode" name="sel_GroupCode" class="form-control select2" style="width: 120px" ></select>
                </td>
            </tr>
        </table>
    </div>
</form>
<!--色牢度-->
<div class="gridPanel" style="width: 100% ">
    <div class="ibox-title">
        子表
    </div>
    <div class="gridPanel">
        <div id="gridPager">
            <label class="radio-inline">
                <a style="margin-left:3px;" class="btn btn-next" onclick="AddRows_ColorFastness()"><i class="fa fa-plus" style="margin-right:5px"></i>新增</a>
            </label>
            <label class="radio-inline">
                <a style="margin-left:3px;" class="btn btn-next" onclick="DelRows_ColorFastness()"><i class="fa fa-minus" style="margin-right:5px"></i>删除</a>
            </label>
        </div>
        <table id="gridList"></table>
    </div>
</div>


n