@model AMESCoreStudio.Web.ViewModels.PCS.PCS021ViewModel
@{
    ViewData["Title"] = "過站作業";
    Layout = "~/Views/Shared/_AMESLayout.cshtml";
}

<style type="text/css">
    .layui-table-main .layui-table-cell {
        /*height: auto !important;*/
        white-space: normal;
    }

    .layui-table img {
        max-width: 60px;
        max-height: 28px;
    }

    .layui-tree-main {
        cursor: pointer;
        padding-right: 10px;
        float: left;
        border-width: 1px;
        border-style: solid;
        border-color: #e6e6e6;
        margin: 10px 0;
    }

    .layui-form > iframe {
        height: 100%;
        width: 100%;
        border: none;
    }

    .element {
        float: left;
    }

    .layui-field-title {
        margin: auto;
    }

    .layui-form-item {
        margin-bottom: auto;
    }
</style>


<div class="layui-card" style="height:auto;">
    @* <div class="layui-card-header">
        <div class="layui-form">
                <div class="layui-form-item ">
                     <div class="layui-inline"><i class="fa fa-file-text-o fa-fw"></i> @ViewBag.Title</div>
                 </div>
             </div>
         </div>*@
    <form id="PCS021Form" method="post" asp-action="PCS021" class="layui-form">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="layui-card-body">
            <div class="layui-row">
                <div class="layui-col-md5 element">
                    <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                        <ul class="layui-tab-title">
                            <li class="layui-this">過站資料</li>
                            <li>治具資料</li>
                        </ul>
                        <div class="layui-tab-content">
                            <div class="layui-tab-item layui-show">
                                <div class="layui-col-md12">
                                    <div class="layui-form-item">
                                        <div class="layui-inline">
                                            <label class="layui-form-label">製程:</label>
                                            <div class="layui-input-inline" style="width:100px">
                                                <select id="unit" lay-filter="unit" lay-verify="required" asp-for="UnitNO" style="width:100px" lay-submit asp-items="@ViewBag.FactoryUnitList">
                                                </select>
                                            </div>
                                            <label class="layui-form-label">線別:</label>
                                            <div class="layui-input-inline" style="width:100px">
                                                <select id="line" lay-filter="line" lay-verify="required" asp-for="LineID" lay-submit>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">

                                    </div>

                                    <div class="layui-form-item">
                                        <div class="layui-inline">
                                            <label class="layui-form-label">工單號碼:</label>
                                            <div class="layui-input-inline" style="width:100px">
                                                <input type="text" onkeydown="SearchNo(event);" asp-for="WipNO" asp-action="PCS021_GetWip" class="layui-input" autocomplete="off" />
                                                <input type="hidden" asp-for="WipID" value="@Model.WipID" />
                                                <input type="hidden" asp-for="FlowRuleID" value="@Model.FlowRuleID" />
                                                <input type="submit" id="GetWipData" asp-action="PCS021_GetWip" style="display:none;" />
                                            </div>
                                            <label class="layui-form-label">作業站:</label>
                                            <div class="layui-input-inline" style="width:100px">
                                                <select id="stations" lay-filter="stations" lay-verify="required" asp-for="Station" asp-items="@ViewBag.GetRuleStationByFlowRuleIDList" lay-submit>
                                                </select>
                                            </div>


                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md12">
                                    <div class="layui-form-item">
                                        <div class="layui-inline">
                                            <label class="layui-form-label">料號:</label>
                                            <label class="layui-form-label" style="text-align: left; width: 150px;">@Model.ItemNO</label>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-inline">
                                            <label class="layui-form-label" style="width:100px;text-align:left; padding: 9px 10px">已刷數/工單數:</label>
                                            <label class="layui-form-label" style="width:150px;text-align:left"><span id="InputQty">@Model.InputQTY</span>/@Model.PlanQTY</label>
                                        </div>
                                    </div>
                                    <fieldset class="layui-elem-field layui-field-title">
                                        <legend style="color: #e18917">刷讀條碼</legend>
                                    </fieldset>
                                    <div class="layui-form-item">
                                        <div class="layui-inline">
                                            <label class="layui-form-label">刷讀條碼:</label>
                                            <div class="layui-input-inline">
                                                <input type="text" onkeydown="input(event);" id="inputtxt" asp-for="Input" class="layui-input" autocomplete="off" />
                                            </div>
                                        </div>
                                        @*<span style="color: firebrick;word-break: break-all;" class="text-danger offset-sm-3">@Html.ValidationMessage("error")</span>*@
                                    </div>
                                </div>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="color: #e18917">刷入資料</legend>
                                    <table id="tblCustomers" class="layui-table" cellpadding="0" cellspacing="0">
                                        <tbody id="tblCustomersBody">
                                        </tbody>
                                        @*<tfoot>
                                                <tr>
                                                    <td><input type="text" id="txtInput" /></td>
                                                    <td><input type="button" value="Remove" onclick="Remove(this)" /></td>
                                                </tr>
                                            </tfoot>*@

                                    </table>
                                </fieldset>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="color: #e18917">檢查項目</legend>
                                    <table id="MydataTable" class="layui-table">
                                        @{ int i = 0;}
                                        <thead>
                                            <tr>
                                                <th>
                                                    工項順序
                                                </th>
                                                <th>
                                                    工項名稱
                                                </th>
                                                <th>
                                                    工項描述
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach (var index in Model.MaterialStationsItems)
                                            {
                                                <tr>
                                                    <td>
                                                        @index.StationsItemSeq
                                                    </td>
                                                    <td>
                                                        @index.StationsItemNo
                                                    </td>
                                                    <td>
                                                        <input asp-for="@index.StationsItemDesc" name="MaterialStationsItems[@i].StationsItemDesc" readonly="readonly" />
                                                    </td>
                                                </tr>
                                                i++;
                                            }
                                        </tbody>

                                    </table>
                                </fieldset>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="color: #e18917">KeyParts</legend>
                                </fieldset>
                                <div class="layui-col-md12">
                                    <div class="layui-form-item">
                                        <table id="MydataTable" class="layui-table">
                                            @{ int j = 0;}
                                            <thead>
                                                <tr>
                                                    <th>
                                                        KP料號名稱
                                                    </th>
                                                    <th>
                                                        KP料號NO
                                                    </th>
                                                    <th>
                                                        順序
                                                    </th>
                                                    <th>
                                                        前置碼
                                                    </th>
                                                    <th>
                                                        長度
                                                    </th>
                                                    <th>
                                                        站(前段)
                                                    </th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach (var index in Model.WipKps)
                                                {
                                                    <tr>
                                                        <td>
                                                            <input type="text" asp-for="@index.KpName" name="WipKps[@j].KpName" style="width:100px" readonly="readonly" />
                                                        </td>
                                                        <td>
                                                            <select disabled asp-for="@index.KpNo" name="WipKps[@j].KpNo" asp-items="@ViewBag.ItemsList" class=""></select>
                                                        </td>
                                                        <td>
                                                            <input type="text" asp-for="@index.KpSeq" name="WipKps[@j].KpSeq" style="width:30px" readonly="readonly" />
                                                        </td>
                                                        <td>
                                                            <input type="text" asp-for="@index.Title" name="WipKps[@j].Title" style="width:50px" readonly="readonly" />
                                                        </td>
                                                        <td>
                                                            <input asp-for="@index.Length" name="WipKps[@j].Length" style="width:30px" readonly="readonly" />
                                                        </td>
                                                        <td>
                                                            @index.UnitNoName
                                                        </td>
                                                    </tr>
                                                    j++;
                                                }
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-tab-item">
                                <table id="MydataTable" class="layui-table">
                                    @{int k = 0;}
                                    <thead>
                                        <tr>
                                            <th>
                                                治具NO
                                            </th>
                                            <th>
                                                站(前段)
                                            </th>
                                            <th>
                                                序號
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var index in Model.MaterialOutfits)
                                        {
                                            <tr>
                                                <td>
                                                    @index.OutfitNo
                                                </td>
                                                <td>
                                                    @index.StationType
                                                </td>
                                                <td>
                                                    <input onkeypress="if( event.keyCode == 13 ) { return false; }" asp-for="@index.Inputs" name="MaterialOutfits[@k].Inputs" />
                                                </td>
                                            </tr>
                                            k++;
                                        }
                                    </tbody>

                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md7 element" style="height:100%">
                    <iframe frameborder="0" style="height:100%;width:100%" src=@(Model.SopPath)></iframe>
                </div>
            </div>
        </div>
    </form>
</div>

@section Scripts{
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial");
        await Html.RenderPartialAsync("_FileinputScriptsPartial"); }
    <script type="text/javascript" src="~/js/grids.min.js"></script>
    <script type="text/javascript">
           $(document).ready(function () {
        var error = '@Html.ValidationMessage("error")';
        if ($(error).text() != '') {
            parent.hg.msg(error);
        }
    });

        // Jquery 將兩邊DIV設定同高度
        jQuery(function ($) {
            $('.element').responsiveEqualHeightGrid();
            getUnitLineList($("#unit").val());
        });

        function getUnitLineList(data)
        {
            $.ajax(
                {
                    url: "@Url.Action("GetUnitLineJson", "BAS")",
                    dataType: 'json',
                    data: { "unit_no": $("#unit").val()},
                    type: 'post',
                    success: function (result)
                    {
                        $("#line").empty();//清空下拉框的值
                        $.each(result.data, function (index, item) {
                            $("#line").append($("<option>").text(item.text).val(item.value))
                        });
                         $("#line").val(@Model.LineID);
                        layui.form.render("select");//重新渲染 固定写法

                    },
                    error: function (result)
                    {
                        alert(result);
                    }
                });
        };

        function SearchNo(e) {
            if (e.keyCode == 13) {
                $("#GetWipData").click();
            }
        };

        function input(e) {
            if (e.keyCode == 13) {
                var Inputs = GetAllEmpData();
                var data = $("#PCS021Form").serialize();
                $.each(Inputs, function (i, item) {
                    data += "&Inputs[" + i + "]=" + item;
                });
                $.ajax({
                    url: "@Url.Action("PCS021_Input", "PCS")",
                    type: "POST",
                    data: data,
                    success: function (result) {
                        $("#inputtxt").val('');
                        if (!result.success) {
                            parent.hg.msg(result.msg);
                        }
                        else {
                            if (result.msg.length != 0) { parent.hg.msg(result.msg);}
                            if (result.data.length != 0) {
                                TableAddRow(result.data);
                            }
                            else {
                                $("#tblCustomersBody").empty();
                                InputQty();
                            }
                        }
                }
                });
                event.preventDefault();
                return false;
            }
        };

        layui.use(['form', 'layer'], function () {
            form = layui.form;

            form.on('select(unit)', function (data) {
                getUnitLineList(data.value);
                $("#stations").empty();//清空下拉框的值
                //getRuleStationList(data.value);
            });

            form.on('select(stations)', function (data) {
                $("#GetWipData").click();
            });

        });

        function InputQty() {
            $.ajax({
                url: "@Url.Action("getBarcodeStationByInputQty", "PCS")",
                type: "POST",
                data: { "wipNo": $("#WipNO").val(), "unitNo": $("#unit").val(), "ststionID": $("#stations").val()},
                    success: function (result) {
                        $("#InputQty").text(result.data);
                }
               });
        };

        // 新增trRow
        function TableAddRow(e) {
            //var rowCount = $('.data-contact-person').length + 0;
            var contactdiv = '<tr class="data-contact-person">' +
                '<td><input type="text" name="f-name" class="form-control td-Input" value='+ e +' /></td>' +
                '<td><a class="layui-btn layui-btn-danger layui-btn-xs layui-icon layui-icon-delete" onclick="Remove(this);">刪除</a></td>' +
                '</tr>';
            $('#tblCustomers').append(contactdiv);
            AutoWindowSize();
        };

        // Table 刪除
        function Remove(button) {
            //Determine the reference of the Row using the Button.
            var row = $(button).closest("TR");
            var table = $("#tblCustomers")[0];
            table.deleteRow(row[0].rowIndex);
            //var name = $("TD", row).eq(0).html();
            //if (confirm("Do you want to delete: " + name)) {
            //    //Get the reference of the Table.
            //    var table = $("#tblCustomers")[0];

            //    //Delete the Table row using it's Index.
            //    table.deleteRow(row[0].rowIndex);
            //}
        };

        // 紀錄表格欄位
        function GetAllEmpData() {
            var data = [];
            $('tr.data-contact-person').each(function () {
                var firstName = $(this).find('.td-Input').val();
                data.push(firstName);
            });
            return data;
        }

        function AutoWindowSize() {
            $(window).on('resize', function () {
                //AdminInit();
                //var $content = $('#nav_xbs_tab .layui-tab-content');
                //$content.height($(this).height() - 125);
                //$content.find('iframe').each(function () {
                //    $(this).height($content.height());
                //});
            }).resize();
        }
        function AdminInit() {

            var hgfull = function () {
                var layfull = $('[lay-height]');
                if (layfull.attr('lay-height')) {
                    var arr = layfull.attr('lay-height').split('-');
                    if (arr.length === 2) {
                        var height = window.innerHeight - arr[1];
                        layfull.css('height', height + 'px');
                        layfull.css('overflow-y', 'auto');
                    }
                    else if (arr[0] === 'full') {
                        layfull.css('height', window.innerHeight - 200 + 'px');
                        layfull.css('overflow-y', 'auto');
                    }
                    else if (arr[0] === 'auto') {
                        var next = layfull.next();
                        if (next.length > 0) {
                            setTimeout(function () {
                                //console.log(next.height());
                                if (next.height() === 99) {
                                    hgfull();
                                    return false;
                                }
                                layfull.children('.layui-card').css('height', next.height() + 'px');
                                layfull.children('.layui-card').css('overflow-y', 'auto');
                            }, 5);
                        }
                    }
                }
            }
            $(window).resize(function () {
                hgfull();
                if (!document.fullscreenElement) {
                    $("body").find('.fa-compress').removeClass('fa-compress').addClass('fa-arrows-alt');
                }
            });
        }
    </script>
}