@{
    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>
    debugger;
    var BID = $.request("ID");
    var BRGCode = $.request("BRGCode");
    var BRGDesc = decodeURI($.request("BRGDesc"));
    $(function () {

        if (BID != "") {
            $("#BRGCode").val(BRGCode);
            $("#BRGDesc").val(BRGDesc);
            gridListColorFastness2();
        } else {
            gridListColorFastness();
        }


    });


    function gridListColorFastness() {

        var $gridList = $("#gridListColorFastness");
        $gridList.dataGrid({
            url: "/WMS/BasicSettings/GetICSBadCodeGroupdetil?" + Math.random(),
            height: $(window).height() -50,
            width: $(window).width() - 400,
            cellEdit: true,
            cellsubmit: "clientArray",
            colModel: [
                { label: "主键", name: "ID", hidden: true, key: true },
                { label: '不良代码编码', name: 'BadCode', width: 400, align: 'left', editable: true, },
                { label: '不良代码描述', name: 'BadDesc', width: 400, align: 'left', 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 gridListColorFastness2() {

        var $gridList = $("#gridListColorFastness");
        $gridList.dataGrid({
            url: "/WMS/BasicSettings/GetICSBadCodeGroupdetil2?ID=" + BID + "&" + Math.random(),
            height: $(window).height() - 50,
            width: $(window).width() - 400,
            cellEdit: true,
            cellsubmit: "clientArray",
            colModel: [
                { label: "主键", name: "ID", hidden: true, key: true },
                { label: 'ID', name: 'ID', hidden: true },
                { label: '不良原因编码', name: 'BadCode', width: 400, align: 'left', editable: true, },
                { label: '不良原因描述', name: 'BadDesc', width: 400, align: 'left', 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(),
            BadReasonCode: "",
            BadReasonDesc: ""

        };
        $("#gridListColorFastness").jqGrid('addRowData', obj.ID, obj, 'last');
        $(".unwritten").hide();
    }
    function DelRows_ColorFastness() {
        var rowid = $("#gridListColorFastness").jqGridRowValue().ID

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

            }
        });
    }

    function submitForm() {
        debugger;
        $("#gridListColorFastness").find($("td[aria-describedby='gridListColorFastness_rn']")).click();//提交取消编辑状态
        var BRGCodes = $("#BRGCode").val();
        var BRGDescs = $("#BRGDesc").val();
        if (BRGCodes == '') {
            $.modalAlertNew("WMS00007");
            return;
        }
        if (BRGDescs == '') {
            $.modalAlertNew("WMS00008");
            return;
        }
        var obj_Item1 = $("#gridListColorFastness");
        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.BadCode == '') {
                    $.modalAlertNew("WMS00009");
                    return;
                }
                if (RowData_Item1.BadDesc == '') {
                    $.modalAlertNew("WMS00009");
                    return;
                }
                var obj = {
                    ID: RowData_Item1.ID,
                    BadReasonCode: RowData_Item1.BadCode,
                    BadReasonDesc: RowData_Item1.BadDesc,
                };
                arrayData_Item1.push(obj);
            }
        }
        var arrayItem1 = JSON.stringify(arrayData_Item1);
        debugger;
        
        if (BID != "") {
            $.ajax({
                url: "/WMS/BasicSettings/UpdateICSBadCodeGroup?BID=" + BID + "&BRGCode=" + BRGCodes + "&BRGDesc=" + BRGDescs + "&" + Math.random(),

                data: {
                    arrayShellFabricItem: arrayItem1,
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    layer.msg(data.message);
                    window.setTimeout(function () {
                        $.modalClose();
                    }, 2500);
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                },

            });
        } else {
            $.ajax({
                url: "/WMS/BasicSettings/SubICSBadCodeGroupAdd?BRGCode=" + BRGCodes + "&BRGDesc=" + BRGDescs + "&" + Math.random(),
                data: {
                    arrayShellFabricItem: arrayItem1,
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    layer.msg(data.message);
                    window.setTimeout(function () {
                        $.modalClose();
                    }, 2500);
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                },

            });
        }


    }

</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="BRGCode" type="text" class="form-control" placeholder="不良代码组编码" style="width: 300px;">
                </td>
                <th class="formTitle">不良代码组描述:</th>
                <td class="formValue">
                    <input id="BRGDesc" type="text" class="form-control" placeholder="不良代码组描述" style="width: 300px;">
                </td>
            </tr>
        </table>
    </div>
</form>
<!--色牢度-->
<div class="gridPanel" style="width: 100% ">
    <div class="ibox-title">
        子表
    </div>
    <div class="gridPanel"> 
        <div id="gridPagerColorFastness">
            <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="gridListColorFastness"></table>
    </div>
</div>