@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<link href="~/Content/js/select2/select2.min.css" rel="stylesheet" />
<script src="~/Content/js/datepicker/WdatePicker.js"></script>
<script src="~/Content/js/select2/select2.min.js"></script>
<style>
    #Additem {
        margin-left: 45%;
    }

    #AddTemitem {
        margin-right: 45%;
    }
</style>
<script>

    var _Clos = new Array();
    var rfqcode = $.request("rfqcode");
    var ID = $.request("ID");
    var ApplyNegCode = $.request("ApplyNegCode");
    var WHCode = $.request("WHCode");
    $(function () {
        SetCols();
        InitControl();
        var Muser = '@NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode'
        var now = new Date();
        var date = now.toLocaleDateString();
        $("#txtMUSER").val(Muser);
        $("#txtMTIME").val(date);
        $("#txtApplyNegCode").val(ApplyNegCode);
        $("#sel_WHCode").val([WHCode]).trigger("change");

        ClearTemp();
        reloadData();
    })

    function SetCols() {
        $.ajax({
            url: "/Print/SelectColumnName?" + Math.random(),
            dataType: "json",
            async: false,
            success: function (data) {
                var cols = new Array();

               var collast = { label: '主键', name: 'ID', width: 150, align: 'left', hidden: true, key: true };
                    cols.push(collast);
                var collast = { label: 'ID', name: 'ID', hidden: true };
                    cols.push(collast);
                var collast =    {
                        label: '查看', width: 100, align: 'left',
                        formatter: btnLook
                };
                    cols.push(collast);
                var collast = { label: '退料子ID', name: 'TLZID', hidden: true };
                    cols.push(collast);
                var collast = { label: '退料单号', name: 'ApplyNegCode', width: 150, align: 'left' };
                    cols.push(collast);
                var collast = { label: '料品编码', name: 'InvCode', width: 100, align: 'left' };
                    cols.push(collast);
                var collast = { label: '物料描述', name: 'InvName', width: 100, align: 'left' };
                    cols.push(collast);
                var collast = { label: '规格型号', name: 'InvStd', width: 100, align: 'left' };
                    cols.push(collast);
                var collast = { label: '数量', name: 'Quantity', width: 100, align: 'left', editable: true, editrules: { number: true } };
                    cols.push(collast);
                var collast = { label: '辅计量数量', name: 'Amount', width: 150, align: 'left' };
                    cols.push(collast);
                var collast = { label: '单位', name: 'InvUnit', width: 100, align: 'left' };
                    cols.push(collast);
                var collast = { label: '子件ID', name: 'ZJID', width: 100, align: 'left' };
                    cols.push(collast);
                var collast = { label: '已发数量', name: 'IssueNegQuantity', width: 100, align: 'left' };
                cols.push(collast);
                var collast = { label: '自由项ID', name: 'ExtensionID', hidden: true };
                cols.push(collast);

                if (data != null && data.length > 0) {
                    DateList = data;
                    for (var i = 0; i < data.length; i++) {
                        var ColName = data[i].ColName;
                        var ColCode = data[i].ColCode;
                        var obj = new Array();
                        obj = {
                            label: ColName,
                            name: ColCode,
                            width: 80,
                            align: "left"
                        }
                        cols.push(obj);
                    }
                }
                _Clos = cols;
            }
        });
    }

    function reloadData() {
        $("#gridList").dataGrid({
            url: "/WMS/ProductionIssue/GetICSMOApplyNegDetailTemp?ApplyNegCode=" + $("#txtApplyNegCode").val() + "&" + Math.random(),
            //postData: { rfqno: $("#RFQNO").val() },
            height: $(window).height() - 200,
            width: $(window).width(),
            cellEdit: true,
            colModel: _Clos,
         //   colModel: [

         //{ label: '主键', name: 'ID', width: 150, align: 'left' ,hidden: true, key: true },
         //{ label: 'ID', name: 'ID', hidden: true },
         //  {
         //      label: '查看', width: 100, align: 'left',
         //      formatter: btnLook
         //  },
         //{ label: '退料子ID', name: 'TLZID', hidden: true },
         //{ label: '退料单号', name: 'ApplyNegCode', width: 150, align: 'left' },
         //{ label: '料品编码', name: 'InvCode', width: 100, align: 'left' },
         //{ label: '物料描述', name: 'InvName', width: 100, align: 'left' },
         //{ label: '规格型号', name: 'InvStd', width: 100, align: 'left' },
         //{ label: '数量', name: 'Quantity', width: 100, align: 'left', editable: true, editrules: { number: true } },
         //{ label: '辅计量数量', name: 'Amount', width: 150, align: 'left' },
         //{ label: '单位', name: 'InvUnit', width: 100, align: 'left' },
         //{ label: '子件ID', name: 'ZJID', width: 100, align: 'left' },
         //{ label: '已发数量', name: 'IssueNegQuantity', width: 100, align: 'left' },
         //{ label: '自由项ID', name: 'ExtensionID', hidden: true },
         //   ],
            cellsubmit: "clientArray",
            width: "100%",
            autowidth: true,
            rownumbers: true,
            viewrecords: true,
        });
    }


    function btnLook(cellvalue, options, rowObject) {
        return cellvalue = "<a class=\"btn btn-primary dropdown-text\" onclick=\"UpLoadClick('" + rowObject.ID + "','" + rowObject.TLZID + "')\">查看</a>";
    }

    function UpLoadClick(ID,TLZID) {
        debugger;

        $.modalOpen({
            id: "selectDeatil",
            title: "查看修改物料",
            url: "/WMS/ProductionIssue/SeachInventory?TLZID=" + TLZID + "&Type=" + 1 + "&TLZID=" + TLZID + "&" + Math.random(),
            width: "1000px",
            height: "1000px",
            callBack: function (iframeId) {
                //top.frames[iframeId].submitForm();
                //top.frames[iframeId].Close();
                //$("#gridList").jqGrid().setGridParam({ datatype: 'json' }, { postData: { rfqno: $("#RFQNO").val() } }).trigger('reloadGrid');

                $("#gridList").jqGrid("delRowData", ID)
                var obj = top.frames[iframeId].submitForm();
                $("#gridList").jqGrid('addRowData', obj.ID, obj, 'last');
                $(".unwritten").hide();
                top.frames[iframeId].Close();
            }
        });


    }

    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 AddRowToLast() {
        var obj = {
            ID: guid(),
        };

        $("#gridList").jqGrid('addRowData', obj.ID, obj, 'last');
        $(".unwritten").hide();
    }

    function DeleteRowToLast() {
        debugger;
        var rowid = $("#gridList").jqGridRowValue().ID
        $("#gridList").delRowData(rowid);

    }

        function InitControl() {
            var $VenCode = $("#sel_WHCode");
            $VenCode.select2({
                allowClear: true,
                escapeMarkup: function (m) {
                    return m;
                }
            });
            $.ajax({
                url: "/WMS/ProductionIssue/GetWHCode" + "?" + Math.random(),
                dataType: "json",
                async: false,
                success: function (data) {
                    $.each(data, function (index, item) {
                        $VenCode.append("<option value='" + item.WarehouseCode + "'>&nbsp;" + item.WarehouseName + "</option>");
                    });
                }
            });
        }

        function ClearTemp() {
            $.ajax({
                url: "/WMS/ProductionIssue/ClearTemp?" + Math.random(),
                datatype: "json",
                async: false
            })

        }

        function submitForm() {
            var WHCode = $("#sel_WHCode").val();
            if (WHCode == "") {
                $.modalAlertNew("WMS00041");
            return;
        }
        debugger;

        var IDlist = "";
        var RoleEnCode = '@NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode'
        var WorkPoint = '@NFine.Code.OperatorProvider.Provider.GetCurrent().Location'
            var myDate = new Date();
            var year = myDate.getFullYear(); //获取当前年
            var mon = myDate.getMonth() + 1 < 10 ? "0" + (myDate.getMonth() + 1) : myDate.getMonth() + 1; //获取当前月
            var dates = myDate.getDate() + 1 < 10 ? "0" + myDate.getDate() : myDate.getDate(); //获取当前日
            var hours = myDate.getHours() + 1 < 10 ? "0" + myDate.getHours() : myDate.getHours(); //获取当前小时
            var minutes = myDate.getMinutes() + 1 < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes(); //获取当前分钟
            var seconds = myDate.getSeconds() + 1 < 10 ? "0" + myDate.getSeconds() : myDate.getSeconds(); //获取当前秒
            var date = year + "-" + mon + "-" + dates + ' ' + hours + ':' + minutes + ':' + seconds;
        var Details = [];
        var obj_vendor = $("#gridList");
        var rowIds_vendor = obj_vendor.getDataIDs();
        var arrayData_vendor = new Array();
        if (rowIds_vendor.length > 0) {
            for (var i = 0; i < rowIds_vendor.length; i++) {
                var vendorRowData = obj_vendor.getRowData(rowIds_vendor[i]);
                if (vendorRowData.Quantity == '0' || vendorRowData.Quantity == 0) {
                    $.modalAlertNew("WMS00089");
                    return;
                }
                var obj = {
                    Sequence: i + 1,
                    DetailID:vendorRowData.TLZID,
                    InvCode: vendorRowData.InvCode,
                    Quantity: vendorRowData.Quantity,
                    Amount: vendorRowData.Amount,
                    SourceDetailID: vendorRowData.ZJID,
                    ExtensionID: vendorRowData.ExtensionID,
                };
                Details.push(obj);
            }
            var ICSASNs = {
                ID:ID,
                WHCode: WHCode,
                User: RoleEnCode,
                MTIME: date,
                WorkPoint: WorkPoint,
                Type:'1',
                Detail: Details,
            }
            var ICSASN = [];
            ICSASN.push(ICSASNs);
            $.submitForm({

                url: "/WMS/ProductionIssue/UpdateICSMOApplyNeg?" + Math.random(),
                param: { ICSASN: JSON.stringify(ICSASN) },
                success: function () {
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        }
    }





</script>
<form id="form1">
    <div class="topPanel" style="height:10px">
        <div class="btn-group">
            @*<a id="AddItemLot" style="margin-left:3px;" class="btn btn-primary" onclick="btnCreate()"><i class="fa fa-pencil-square-o"></i>添加明细</a>
                <a id="refresh" class="btn btn-primary" style="margin-left:3px;" onclick="reloadData()"><span class="glyphicon glyphicon-refresh"></span></a>*@
        </div>
    </div>
    <div style="margin-right: 20px;">
        <table class="form">
            <thead>主表信息</thead>
            <tr>
                <th class="formTitle MOPick">退料单号:</th>
                <td class="formValue MOPick">
                    <input type="text" id="txtApplyNegCode" class="form-control" />
                </td>
                <th class="formTitle">仓库编码:</th>
                <td class="formValue">
                    <select id="sel_WHCode" name="sel_WHCode" class="form-control select2" style="width: 230px" placeholder="请选择供应商编码..."></select>
                    <input type="hidden" id="hidetext" />
                </td>

            </tr>

            <tr>
                <th class="formTitle">维护人:</th>
                <td class="formValue">
                    <input id="txtMUSER" type="text" readonly="readonly" class="form-control" />
                </td>
                <th class="formTitle">维护时间:</th>
                <td class="formValue">
                    <input id="txtMTIME" type="text" readonly="readonly" class="form-control" />
                </td>
            </tr>
        </table>


    </div>
    <div class="gridPanel" style="margin-left:10px">
        <span><strong>子表信息</strong></span>
        <table id="gridList"></table>
        <div style="text-align:center">
            <button id="AddRow" class="btn btn-primary" type="button" onclick="AddRowToLast()">添加</button>
            <button id="DeleteRow" type="button" class="btn btn-primary" onclick="DeleteRowToLast()">删除</button>
        </div>
    </div>

</form>