<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
require "../inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL); 
if(!empty($_POST)){
    $error_ = 0;
    if(empty($_POST['vendor_id'])){
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Please enter code !</div>');
    }
    if(!empty($_POST['vendor_id']) and $error_ == 0){
        if($_GET['action'] == "add"){
            $commonFunctions->addData('vndr_po'," (reference_number,vendor_id,payment_terms_id,payment_type,date_of_delivery,project_id,incoterms,currency_id,note_public,note_private ) VALUES ('".$_POST['reference_number']."','".$_POST['vendor_id']."','".$_POST['payment_terms_id']."','".$_POST['payment_type']."','".$_POST['date_of_delivery']."','".$_POST['project_id']."','".$_POST['incoterms']."','".$_POST['currency_id']."','".$_POST['note_public']."','".$_POST['note_private']."') ",'purchase-order.php?state=form');
        }
    }else{
        echo "<script>location.href='purchase-order.php?state=form';</script>";
    }
}
?>    

    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col">
                        <h4 class="page-title">New Purchase Order</h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Vendor & Suppliers</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">New Purchase Order <?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></a></li>
                        </ol>
                    </div><!--end col-->
                      
                </div><!--end row-->                                                              
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <!-- end page title end breadcrumb -->
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">New Purchase Order <?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></h4>
                    <?php echo !empty($msg)?$msg:''; ?>        
                    <?php echo !empty($msg1)?$msg1:''; ?>        
                </div>
        
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="main-card mb-3 ">
                                <!-- <div class="card-body"> -->
                                <?php if(!empty($_GET['state']) and $_GET['state'] == "form"): ?>
                                    <form enctype="multipart/form-data" action="purchase-order.php?state=form&action=add" method="post" name="formsoc" autocomplete="off"> 
                                        <div class="form-row">
                                            <div class="position-relative form-group col-md-4">
                                                <label for="ref" class="">Ref. <span class="error">*</span></label>
                                                <input type="text" maxlength="128" name="ref" id="ref" value="Draft" class="form-control" readonly="">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="vendor_id" class="">Vendor<span class="error">*</span></label>
                                                <?php $vendorData = $commonFunctions->getData('vndr_details',' '); ?>
                                                <select name="vendor_id" class="form-control">
                                                    <option value="0">Select</option>
                                                    <?php foreach ($vendorData as $rw) { ?>
                                                        <option value="<?php echo $rw['id'];?>" ><?php echo $rw['name'];?></option>
                                                    <?php } ?>
                                                </select>
                                                <b><a href="vendor.php?state=form"> Create Vendor & Supplier</a></b>
                                            </div>  
                                            <div class="position-relative form-group col-md-4">
                                                <label for="reference_number" class="">Ref. Vendor</label>
                                                <input type="text" name="reference_number" id="reference_number" value="" class="form-control" >
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="payment_terms_id" class="">Payment Terms</label>
                                                <?php $paymentData = $commonFunctions->getData('fin_accnt_payment_trems',' WHERE status = 0 '); ?>
                                                <select name="payment_terms_id" class="form-control">
                                                    <option value="0">Select</option>
                                                    <?php foreach ($paymentData as $rw) { ?>
                                                        <option value="<?php echo $rw['id'];?>" ><?php echo $rw['label'];?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="payment_type" class="">Payment Types</label>
                                                <?php $paymentTYData = $commonFunctions->getData('fin_accnt_payment_mode',' WHERE status = 0 '); ?>
                                                <select name="payment_type" class="form-control">
                                                    <option value="0">Select</option>
                                                    <?php foreach ($paymentTYData as $rw) { ?>
                                                        <option value="<?php echo $rw['id'];?>" ><?php echo $rw['label'];?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>

                                            <div class="position-relative form-group col-md-4">
                                                <label for="date_of_delivery" class="">Planned Date of Delivery</label>
                                                <input type="date" name="date_of_delivery" id="date_of_delivery" value="" class="form-control" >
                                            </div>

                                            <div class="position-relative form-group col-md-4">
                                                <label for="project_id" class="">Project</label>
                                                <select name="project_id" class="form-control">
                                                    <option value="0">Select</option>
                                                </select>
                                                <b><a href="project.php?state=form"> Create New Project</a></b>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="incoterms">Incoterms </label>
                                                <?php $IncotermsData = $commonFunctions->getData('tbl_incoterms',' WHERE status = 0 '); ?>
                                                <select id="incoterms" class="flat incoterms form-control" name="incoterms">
                                                    <option value="0">Incoterm</option>
                                                    <?php foreach ($IncotermsData as $rw) { ?>
                                                        <option value="<?php echo $rw['id'];?>" ><?php echo $rw['label']."(".$rw['code'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="currency_id" class=""> Currency </label>
                                                <?php $currencyData = $commonFunctions->getData('fin_accnt_currencies',' WHERE active = 1 '); ?>
                                                <select id="currency_id" name="currency_id" class="flat currency_id form-control">
                                                    <option value="0">Select</option>
                                                    <?php foreach ($currencyData as $rw) { ?>
                                                        <option value="<?php echo $rw['id'];?>" ><?php echo $rw['label']."(".$rw['code_iso'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="note_public" class="">Note (Public)</label>
                                                <textarea class="form-control" name="note_public"></textarea>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="note_private" class="">Note (Private)</label>
                                                <textarea class="form-control" name="note_private"></textarea>
                                            </div>
                                            <div class="position-relative form-group col-md-12">
                                                <button type="submit" class="btn btn-success">Create</button>
                                            </div>
                                        </div>
                                    </form>
                                <?php endif; ?>
                                    
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->
    
<?php require "../inc/footer.php"; ?>
<script type="text/javascript">
/*function format ( d ) {
    return 'Full name: '+d.first_name+' '+d.last_name+'<br>'+
        'Salary: '+d.salary+'<br>'+
        'The child row can contain any data you wish, including links, images, inner tables etc.';
}
 
$(document).ready(function() {
    var dt = $('.datatable_t').DataTable( {
        "processing": false,
        "serverSide": false,
        "columns": [ 
            {
                "class":          "details-control",
                "orderable":      false,
                "data":           null,
                "defaultContent": ""
            },
        ],
        "order": [[1, 'asc']]
    } );
 
    // Array to track the ids of the details displayed rows
    var detailRows = [];
 
    $('.datatable_t tbody').on( 'click', 'tr td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = dt.row( tr );
        var idx = $.inArray( tr.attr('id'), detailRows );
 
        if ( row.child.isShown() ) {
            tr.removeClass( 'details' );
            row.child.hide();
 
            // Remove from the 'open' array
            detailRows.splice( idx, 1 );
        }
        else {
            tr.addClass( 'details' );
            row.child( format( row.data() ) ).show();
 
            // Add to the 'open' array
            if ( idx === -1 ) {
                detailRows.push( tr.attr('id') );
            }
        }
    } );
 
    // On each draw, loop over the `detailRows` array and show any child rows
    dt.on( 'draw', function () {
        $.each( detailRows, function ( i, id ) {
            $('#'+id+' td.details-control').trigger( 'click' );
        } );
    } );
} );*/
</script>