@extends('adminlte::page') @section('content')
    <?php Fun::lang() ?>
    <div class="title-page">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
                <li><a href="#">Purchase Order</a></li>
                <li><a href="#">Verify</a></li>
            </ul>
        </div>
    </div>
    <div class="page">
        <div class="top-content">
            <div class="row">
                <div class="col-sm-12 col-xs-12">
                    <div class="title-page">
                        <div class="pull-left">
                            <h3>Verify Purchase</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12 col-xs-12">

                <div class="form-wrapper">
                    {!! Form::model($purchase, ['method' => 'PATCH','route' => ['construction-purchase.request_ajust_purchase', $purchase->id],'enctype'=>'multipart/form-data']) !!}
                    <div class="row">
                        @if($purchase->purchase_type==0)
                            <div class="form-group col-sm-4 col-xs-12">
                                <label class="required">BOQ Request</label>
                                {!! Form::select('boq_id',$boq, null, array('class' => 'form-control select2 boq','required','readonly')) !!}
                            </div>
                        @endif
                        <input type="hidden" name = 'purchase_type' value="{{ $purchase->purchase_type }}">
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Project</label>
                            {!! Form::select('project_id',$projects, null, array('class' => 'form-control select2 project','required')) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Request By</label>
                            {!! Form::text('request_by', null, array('placeholder' => 'Please Select','class' => 'form-control ','required')) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Stock Position</label>
                            {!! Form::text('stock_position', null, array('placeholder' => 'Please Select','class' => 'form-control ','required')) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Supply Company</label>
                            {!! Form::select('supplier_id',$supplier, null, array('placeholder' => 'Please Select','class' => 'form-control select2')) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12" style="display: none">
                            <label class="">Phone Number</label>
                            {!! Form::text('phone_number', null, array('placeholder' => 'Please Select','class' => 'form-control ')) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="">Date</label>
                            {!! Form::text('date', date('d-m-Y'), array('placeholder' => 'Date','class' => 'form-control date_picker','autocomplete'=>'off')) !!}
                        </div>

                        <div class="form-group col-sm-12 col-xs-12">
                            <table class="table boq_detail">
                                <thead>
                                    <th>#</th>
                                    <th>Item Name</th>
                                    <th>Unit</th>
                                    <th>Qty</th>
                                    <th>Retail</th>
                                    <th>Total</th>
                                    <th>Purpose and Location</th>
                                    <th>Check All <input type="checkbox" class="check_all"></th>
                                </thead>
                                <tbody >
                                    <?php
                                        $tbody = "";
                                        foreach($detail as $key=>$values){
                                            $tbody .='<tr>
                                                <td>'.($key+1).'</td>
                                                <td data-product-id = "'.$values->product_id.'">'.$values->product_name.'</td>
                                                <td data-unit_name = "'.$values->unit_name.'">'.$values->unit_name.'<input type="hidden" name="unit_id['.$values->product_id.']" value="'.$values->unit_id.'"></td>
                                                <td><input type="text" class="number-only form-control qty_per_house" name="qty_per_house['.$values->product_id.']" value="'.$values->qty.'"></td>
                                                <td><input type="text" class="number-only form-control retail" name="retail['.$values->product_id.']" value="'.$values->price.'"></td>
                                                <td>$ <span class="total_amount">'.$values->qty*$values->price.'</span></td>
                                                <td><textarea name="pur_location['.$values->product_id.']" id="" class="form-control" rows="2" style="resize: none;">'.$values->purpose_and_location.'</textarea></td>
                                                <td>
                                                    <input type="checkbox" name="checkbox[]" class="check_here" checked value="'.$values->product_id.'">
                                                    <button type="button" class="btn btn-danger btn_remove" style="display: none"><span class="fa fa-times"></span></button>
                                                </td>
                                            </tr>';
                                        }
                                     ?>
                                    {!! $tbody !!}
                                </tbody>
                            </table>
                        </div>

                        <div class="col-sm-12 col-xs-12">
                            <div class="form-group">
                                <button type="submit" class="button_sm button_success pull-right" style="display: none;" id="btn-edit-submit"><span
                                    class="fa fa-chevron-circle-right"></span> submit
                                </button>
                                <button type="button" class="button_sm button_success pull-right" onclick="submit_form()"><span
                                    class="fa fa-chevron-circle-right"></span> Request Ajustment
                                </button>
                                <button type="button" class="button_sm button_primary pull-right btn_submit" data-id="{{ $purchase->id }}"><span
                                            class="fa fa-chevron-circle-right"></span> Verified
                                </button>
                                <button type="button" onClick="javascript:history.go(-1)"
                                        class="button_sm button_warning pull-right">
                                    <span class=" fa fa-arrow-circle-left"></span> Back
                                </button>
                                
                            </div>
                        </div>
                    </div>

                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
@endsection
@section('js')
    <script type="text/javascript">
        function check_make_order()
        {
            var boq_id = $(".boq").val();
            var row = $(".boq_detail").find('tbody').find('tr');
            var status_error = 0;
            $(row).each(function(index,value)
            {
                if($(this).closest('tr').find('td:eq(7)').find('input').prop('checked')==true){
                    var product_id = $(this).closest('tr').find('td:eq(1)').data('product-id');
                    var product_qty = $(this).closest('tr').find('td:eq(3)').find('input').val();
                    var route = "{{ route('construction.check_product_qty_boq_verify_product') }}";
                    var product_name = $(this).closest('tr').find('td:eq(1)').text();
                    var unit_name = $(this).closest('tr').find('td:eq(2)').data('unit_name');
                    var purchase_id = "{{ $purchase->id }}";
                    $.ajax({
                        url:route,
                        type:"POST",
                        dataType:"JSON",
                        async:false,
                        data:{
                            "_token":"{{ csrf_token() }}",
                            product_id:product_id,
                            product_qty:product_qty,
                            boq_id:boq_id,
                            purchase_id:purchase_id
                        },
                        success:function(data)
                        {
                            status_error = data['status'];
                            $("#main-spinner").hide();
                            if(data['status']==0){

                            }
                            else if(data['status']==1){
                                $.alert({
                                    title: '',
                                    type: 'red ',
                                    content :product_name+" isn't in BOQ .Please check again before submit !",
                                    useBootstrap: false,
                                    boxWidth: '20%',
                                    theme: 'modern',
                                    buttons: {
                                        close: {
                                            text: 'close',
                                            btnClass: 'button_warning',
                                        }
                                    }
                                });
                            }
                            else if(data['status']==2)
                            {
                                $.alert({
                                    title: '',
                                    type: 'red ',
                                    content :product_name+" is missing ( Balance : "+data['balance']+unit_name+" ).Please check again before submit !",
                                    useBootstrap: false,
                                    boxWidth: '20%',
                                    theme: 'modern',
                                    buttons: {
                                        close: {
                                            text: 'close',
                                            btnClass: 'button_warning',
                                        }
                                    }
                                });
                            }
                            else{

                            }
                        },
                        complete:function(){

                        }
                    });
                }
                if(status_error!=0) return false;
            });
            if(status_error==1){
                $("#main-spinner").hide();
                $.alert({
                    title: '',
                    type: 'red ',
                    content :"Please Select Product Before Submit !",
                    useBootstrap: false,
                    boxWidth: '20%',
                    theme: 'modern',
                    buttons: {
                        close: {
                            text: 'close',
                            btnClass: 'button_warning',
                        }
                    }
                });
            }
            return status_error;
        }
        function submit_form(){
            var check_status = 0;
            check_status = check_make_order();
            if(check_status==0){
                $("#btn-edit-submit").click();
            }
        }
        var tbody = {!! json_encode($tbody) !!};
        $(document).ready(function () {
            
            $(document).on('click','.btn_remove',function () {
                $(this).closest('tr').remove();
            });
            $(document).on('click','.check_all',function () {
                let check_all = $(this).prop('checked');
                $(document).find('.check_here').attr('checked',check_all);
            });
            $(document).on('keyup','.number-only',function () {

                let qty = $(this).closest('tr').find('.qty_per_house').val();
                let price = $(this).closest('tr').find('.retail').val();
                let total = parseFloat(price?price:0)*parseFloat(qty?qty:0);
                $(this).closest('tr').find('.total_amount').text(total.toFixed(2));
            });
            $('.btn_submit').on('click',function () {
                let purchase_id = $(this).data('id');
                
                $.confirm({
                    title:'Are you sure ?',
                    content:'',
                    theme:'modern',
                    buttons:{
                        cancel:{
                            btnClass:'btn btn-warning',
                            text:'Cancel',
                            action:function () {

                            }
                        },
                        submit:{
                            btnClass:'btn btn-success',
                            text:'Submit',
                            action:function () {
                                    window.location.replace('{{route("construction-purchase.verify_order_product")}}?purchase_id='+purchase_id);
                            }
                        },
                    },
                });
            });
        });
    </script>
@endsection