<?php
/* Status of each requested/transfer Goods */
/* ======================================= */
/*
  1:Requested
  2:Transfered Not Confirmed
  3:Transfered and Confirmed
  4:Rejected
 */
if (empty($request_details)) { ?>
<div class="alert alert-danger" role="alert">
    <button class="close" data-dismiss="alert"></button>
    <strong>Danger: </strong>you can not edit this request! 
    <br>
    This request is not available.
</div>
<?php die(); } 
$IsRejected = false;
$Confirm = false;
$IsDatePicker = false;
if ($Request[0]['Status_ID'] == 4 || $Request[0]['Status_ID'] == 3) {
    $IsRejected = true;
}
if ($Request[0]['Status_ID'] == 2) {
    $Confirm = true;
}
if ($Request[0]['Status_ID'] == 1 || $Request[0]['Status_ID'] == 2) {
    $IsDatePicker = true;
}
$reason_of_rejection = false;
if ($Request[0]['Status_ID'] == 4) {
    $reason_of_rejection = true;
}
?>
<style type="text/css">
    #HintTable thead tr th {
        background-color: #4c0870;
        color:#fff;
    }
    #TransferRequestTable thead tr th {
        color:#555;
        border-bottom: 1px solid rgba(0,0,0,0.1);
    }
    #TransferRequestTable tbody tr td {
        border:0;
        padding:3px 1px;
        position:relative;
    }
    #TransferRequestTable tbody tr:first-child td{
        padding-top:10px;
    }
    .datepicker thead tr .dow {
        color:#ed9421 !important;
    }
    .datepicker table tr td.day:hover {
        background: #ed9421 !important;
        color:#fff;
    }
    .datepicker table tr td.active {
        background-color: #ed9421 !important;
    }
    .datepicker thead tr .datepicker-switch {
        color: #ed9421  !important;
        font-size: 13px;
    }
    .datepicker table tr td span.active {
        background-color: #ed9421 !important;
    }
    .datepicker table tr td span:hover {
        background-color: #ed9421 !important;
        color:#fff;
    }
    datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #ed9421 !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    .datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #ed9421 !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    .select2-container > .select2-choice{
        background-color: #e5e5e5;
        /*        width: 150px;*/
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button.current, .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover,.dataTables_wrapper .dataTables_paginate .paginate_button:hover{
        color:#fff !important;
        border-radius: 50%;
        background: #ed9421!important;
        border-color: #4c0870
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover{
        color:#fff !important;
    }
    .red-box{
        border: 2px solid red;
    }
</style>
<script src="<?php echo THEME_PATH; ?>plugins/jquery-validation/js/jquery.validate.min.js"></script>
<?php echo Forms::ModalLoader('OrderDisabler') ?>
<form method="POST" action="" onsubmit="return false" id="TransferOrder">
    <div class="container-fluid container-fixed-lg">
        <div class="panel panel-default">
            <div class="panel-body">
                <div id="OrderSuccess" class="hidden"></div>
                <div class="invoice padding-50 sm-padding-10">
                    <div class="container-sm-height">
                        <div class="row-sm-height">
                            <div class="col-md-9 col-sm-height sm-no-padding">
                                <h2 class="font-montserrat all-caps hint-text text-center"><?php print $title; ?></h2>
                                <div class="row">
                                    <div class="col-xs-4">
                                        <strong><?php print CommonLanguage::getMessage('From'); ?></strong>
                                        <br><?php echo $Request[0][$Request[0]['Status_ID']!=3 ? 'Req_From' : 'Req_To']; ?></div>
                                    <input type="hidden" value="<?php echo $Request[0]['Request_From']; ?>" id="Req_From"/>
                                    <input type="hidden" value="<?php echo $Request[0]['Request_To']; ?>" id="Req_To"/>
                                    <div class="col-xs-4">
                                        <strong>
                                            <?php print CommonLAnguage::getMessage('To'); ?>
                                        </strong>
                                        <br><?php echo $Request[0][$Request[0]['Status_ID']!=3 ? 'Req_To' : 'Req_From']; ?></div>
                                    <div class="col-xs-4 inv-info">
                                        <h4><strong><?php print CommonLanguage::getMessage('Request'); ?> # <?php echo $Request[0]['serial_prefix'] . $Request[0]['Request_Number']; ?></strong></h4>
                                        <input type="hidden" id="Request_ID" value="<?php echo $Request_ID; ?>" />
                                        <input type="hidden" id="Status_ID" value="<?php echo $Request[0]['Status_ID']; ?>" />
                                        <span> <?php print CommonLanguage::getMessage('Request_Date'); ?>    </span><span>&nbsp;<?php echo $Request[0]['Request_Date']; ?></span>
                                        <?php if ($IsDatePicker) { ?>
                                        <div  class="form-group form-group-default input-group">
                                            <label> <?php print CommonLanguage::getMessage('Transfer_Date'); ?></label>
                                            <input id="transfer_order_date" name="transfer_order_date" class="form-control" value="<?php echo ($today); ?>" />
                                            <span class="input-group-addon bg-complete-darker"><i class="fa fa-calendar"></i></span>
                                        </div>
                                        <?php } ?>
                                    </div>
                                </div>
                                <div>
                                    <div class="clearfix"></div>
                                </div>
                                <br>
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="">
                        <div class="col-md-12   clearfix" style="margin:5px;border: 2px solid #4c0870;">
                            <h5 class="font-montserrat all-caps small no-margin hint-text bold"><?php print CommonLanguage::getMessage('Total_Quantity'); ?></h5>
                            <table class="table" cellpadding="0" cellspacing="0" id="TransferRequestTable">
                                <thead>
                                    <tr>
                                        <th class="text-center">#</th>
                                        <th class="text-center"><?php print CommonLanguage::getMessage('Code'); ?></th>
                                        <th class="text-center"><?php print CommonLanguage::getMessage('Item'); ?></th>
                                        <th class="text-center"><?php print CommonLanguage::getMessage('Quantity'); ?></th>
                                        <th class="text-center"><?php print CommonLanguage::getMessage('UOM'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $j = 1;
                                    for ($i = 0; $i < count($request_details); $i++) {
                                    ?>
                                    <tr class ="<?php echo $request_details[$i]['Request_Qty'] > $request_details[$i]['total_default_quantity'] ? 'red-box' : '' ?>" data-id="<?php echo $request_details[$i]['Product_ID']; ?>" 
                                        data-default-qty ="<?php echo $request_details[$i]['Request_Qty']; ?>" 
                                        data-current-qty ="<?php echo $request_details[$i]['Current_Quantity']; ?>" 
                                        data-qty="<?php echo $request_details[$i]['total_default_quantity']; ?>" 
                                        data-uom="<?php echo $request_details[$i]['Request_UOM'] ?>" >
                                        <td class="text-center Counter"><?php echo $j; ?></td>
                                        <td class="text-center"><?php echo $request_details[$i]['Product_Code']; ?></td>
                                        <td class="text-center"><input type="hidden" class="TypeID" /><?php echo $request_details[$i]['Product_Name']; ?></td>
                                        <td class="text-center UpdatedQuantityVal"><?php echo $request_details[$i]['Current_Quantity']; ?></td>
                                        <td class="text-center "><?php echo $request_details[$i]['UOM_Name']; ?></td>
                                    </tr>
                                    <?php $j++;
                                        $temp = array();
                                    } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <br>
                    <?php if (!$IsRejected && !$Confirm) { ?>
                    <div class="container-sm-height" >
                        <button class=" btn  btn-danger m-b-10  pull-right " id="PrintBtn" style="color:#fff; ;padding-top: 10px;top: 20px;" type="button"><i class="fa fa-print"></i> <span class="bold"><?php print CommonLanguage::getMessage('print'); ?></span></button>
                        <button class=" btn  btn-cons m-b-10 pull-right" id="ConfirmTransferButton" style="color:#fff;background-color:#ed9421;padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="fa fa-check"></i> <span class="bold"><?php print CommonLanguage::getMessage('Transfer_Goods'); ?></span></button>
                        <button class=" btn  btn-danger m-b-10 pull-left" id="RejectTransferButton" style="color:#fff; ;padding-top: 10px;top: 20px;" type="button"><i class="fa fa-close"></i> <span class="bold"><?php print CommonLanguage::getMessage('Reject'); ?></span></button>
                         
                    </div>
                    <textarea id="RejectTransferNote"  class="form-control NoteTextArea"  aria-invalid="false" style="    margin-top: 20px;"></textarea>
                    <?php } if ($Confirm) { ?>
                    <div class="container-sm-height" >
                        <button class=" btn  btn-cons m-b-10 pull-right" id="TransferANDConfirmTransferButton" style="color:#fff;background-color:#ed9421;padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="fa fa-check"></i> <span class="bold"><?php print CommonLanguage::getMessage('Confirm_Transfer'); ?></span></button>
                    </div>
                    <?php } if ($reason_of_rejection) { ?>
                    <div class="container-sm-height" >
                        <div  class="alert alert-warning">
                            <strong><?php print CommonLanguage::getMessage('Reason_of_Rejection'); ?>:</strong><br/>
                            <p><?php echo $Request[0]['Rejection_Comment']; ?></p>
                        </div>
                    </div>
                    <?php } ?>

					<div class="container-sm-height" >

                        <button class=" btn btn-danger btn-cons m-b-10 pull-right " id="PrintBtn" style="padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="fa fa-print"></i> <span class="bold"><?php print CommonLanguage::getMessage('print'); ?></span></button>
                    </div>
                   <!-- <hr>
                    <div>
                        <span class="m-l-20 text-black sm-pull-right">< ?php echo $org_details[0]['Organization_Legal_Name']; ?></span>
                        <span class="m-l-20 text-black sm-pull-right">< ?php echo $org_details[0]['Organization_Province']; ?> < ?php echo $org_details[0]['Organization_Country']; ?> - <?php echo $org_details[0]['Organization_City']; ?></span>
                        <span class="m-l-20 text-black sm-pull-right">CR: < ?php echo $org_details[0]['Organization_Commercial_Registry']; ?></span>
                        <span class="m-l-20 text-black sm-pull-right">< ?php print CommonLanguage::getMessage('Phone'); ?>: < ?php echo $org_details[0]['Organization_Phone']; ?></span>
                        <span class="m-l-40 text-black sm-pull-right">< ?php print CommonLanguage::getMessage('Email'); ?>: < ?php echo $org_details[0]['Organization_Email']; ?></span>
                    </div>-->
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var TRID = 0, totalQty = 0,
        Failed_Hint = <?php echo json_encode(CommonLanguage::getMessage('Failed_Hint')); ?>,
        Sum_of_Transfered = <?php echo json_encode(CommonLanguage::getMessage('Sum_of_Transfered')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
                check_depot_qty = <?php echo json_encode(CommonLanguage::getMessage('check_depot_qty')); ?>,
                    Reject_Order = <?php echo json_encode(CommonLanguage::getMessage('Reject_Order')); ?>,
                        Expiry_Required = <?php echo json_encode(CommonLanguage::getMessage('Expiry_Required')); ?>,
                            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
                            $(function () {
                                $("#PrintBtn").on("click", function () {
                                    var myurl ="Stock/view_transfer_details/" + $('#Request_ID').val();
                                    printExternal(myurl);
                                });
                                $("#ConfirmTransferButton, #TransferANDConfirmTransferButton").on("click", function(){

                                    $("#OrderSuccess").attr("class", "hidden");
                                    if ($("#TransferOrder").valid()) {
                                        var Data = {
                                            Delivery_Date: $("#transfer_order_date").val(),
                                            Request_Date: $("#transfer_order_date").val(),
                                            Request_ID: $("#Request_ID").val(),
                                            Status_ID: $("#Status_ID").val(),
                                            Request_From: $("#Req_From").val(),
                                            Request_To: $("#Req_To").val(),
                                            Products: []
                                        };
                                        var flag = false;
                                        $("#TransferRequestTable tbody tr").each(function (e) {
                                            var me = $(this),
                                                default_qty = parseFloat(me.attr('data-default-qty')),
                                                total_qty = parseFloat(me.attr('data-qty'));
                                            Data.Products.push({
                                                Product_ID: me.attr("data-id"),
                                                UOM_ID: me.attr("data-uom"),
                                                Request_Qty: me.attr('data-current-qty')
                                            });
                                            if (default_qty > total_qty) {
                                                flag = true;
                                            }
                                        })
                                        if (flag) {
                                            $("#OrderSuccess").attr("class", "alert alert-danger").html(check_depot_qty)
                                            $(document.body).animate({scrollTop: 0}, 800)
                                            return false;
                                        }
                                        if($(this).attr('id') == 'ConfirmTransferButton'){
                                            location.href = site_link+'Stock/RequestOrder/transfer/'+$("#Request_ID").val();
                                            return;
                                        }
                                        $("#OrderDisabler").removeClass('hidden');
                                        $.ajax({
                                            url: "StockAjax/TransferGoodsAjax",
                                            method: "POST",
                                            data: Data,
                                            complete: function (v1, v2) {
                                                if (v2 == "success") {
                                                    $("#OrderSuccess").attr("class", "alert alert-success").html(Submitted_Successfully)
                                                    $("#ConfirmTransferButton, #TransferANDConfirmTransferButton,#RejectTransferButton").attr("disabled", "disabled")
                                                } else {
                                                    $("#OrderSuccess").attr("class", "alert alert-danger").html(Failed_Submit)
                                                }
                                                $(document.body).animate({scrollTop: 0}, 800)
                                                $("#OrderDisabler").addClass('hidden');
                                            }
                                        })
                                    }
                                });
                                $("#RejectTransferButton").on("click", function () {
                                    if (!$("#RejectTransferNote").val()) {
                                        alert("You must enter a reason of rejection!")
                                        IsValidated = false;
                                    } else {
                                        var Reject = {
                                            Request_ID: $("#Request_ID").val(),
                                            Rejection_Comment: $("#RejectTransferNote").val()
                                        }
                                        IsValidated = true;
                                    }
                                    if (!IsValidated)
                                        return;
                                    $("#OrderDisabler").removeClass('hidden');
                                    $.ajax({
                                        url: "StockAjax/TransferGoodsAjax",
                                        data: Reject,
                                        complete: function (v1, v2) {
                                            if (v2 == "success") {
                                                $("#OrderSuccess").attr("class", "alert alert-warning").html(Reject_Order)
                                                $("#ConfirmTransferButton, #TransferANDConfirmTransferButton,#RejectTransferButton").attr("disabled", "disabled")
                                            } else {
                                                $("#OrderSuccess").attr("class", "alert alert-danger").html(Failed_Submit)
                                            }
                                            $(document.body).animate({scrollTop: 0}, 800)
                                            $("#OrderDisabler").addClass('hidden');
                                        }
                                    })
                                })
                            });
</script>