<?php 
$it=$_REQUEST['it'];
###### GET ITEM ######
$queItem=$db->prepare('SELECT *FROM tbl_items WHERE item_id="'.$it.'"');
$queItem->execute();
$rr=$queItem->fetch();
$itemid=$rr['item_id'];
$item_name=$rr['item_name']; 
###### END GET ITEM ######
?>

<div class="subheader">
    <h1 class="subheader-title" style="font-weight: bold;">
        <i class='subheader-icon fa fa-bars text-primary'></i><?php echo $title;?> 
    </h1>
    <div class="d-flex mr-4">
    <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
    </div>
</div>

<div class="card mb-g border shadow-0">
    <div class="card-header bg-white p-0 header">
        <div class="p-3 d-flex flex-row">
            <div class="d-block ml-2">
                <span class="h6 font-weight-bold text-uppercase d-block m-0">
                    <?php echo $item_name;?>
                </span>
                <div class="clearfix"></div>                  
            </div>
        </div>
    </div>

    <div class="card-body ">
    <br>

    <form class="row" method="post" action="boqsave" id="boqsave" enctype="multipart/form-data" autocomplete="off">
     <br>
        <label class="col-md-1"></label>
        <label class="col-md-1 col-form-label form-control-label">TYPE<span class="required" style="color:red">*</span></label>
        <div class="col-sm-2">
            <select class="select2 form-control w-100" name="typeid" id="typeid" style="width: 90%;" data-placeholder="- choose type -" onchange="mymat(this.value)" required>
                <option></option>
                <?php
                $stmt = $db->query('SELECT *FROM tbl_typesitem WHERE sttype=1 ORDER BY item_type_id ASC');
                try
                {
                    while ($rrr = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    ?>
                        <option value="<?php echo $rrr['item_type_id']; ?>"><?php echo $rrr['itypeName']; ?></option>
                    <?php
                    }
                    }
                    catch (PDOException $ex) {
                    //Something went wrong rollback!
                    echo $ex->getMessage();
                }
                ?>	
            </select>
        </div>

        <label class="col-md-1 col-form-label form-control-label">Material<span class="required" style="color:red">*</span></label>
        <div class="col-sm-2">
            <select class="select2 form-control w-100" name="matid" id="matid" style="width: 90%;" data-placeholder="- select item -" required>
                <option></option>                
            </select>
        </div>
        
        <label class="col-md-1 col-form-label form-control-label">Qty<span class="required" style="color:red">*</span></label>
        <div class="col-sm-2">
            <input type="text" name="qty" placeholder="0.0" required style="width: 50%;">
        </div>
            <input type="hidden" name="itmid" value="<?php echo $itemid;?>" required>
        <div class="col-sm-2">
            <button type="submit" class="btn btn-primary btn-sm" id="addbtn" style="border-radius: 4px;height: 25px;"><i class="fa fa-plus"></i></button>
        </div>
    </form>
    
    <br/><br/>

    <form action="trashboq" id="trashboq" method="post" enctype="multipart/form-data" autocomplete="off">
        <table class="table w-100" border="2" style="font-size: 11px;">
            <thead class="header bg-primary">
                <tr>
                    <th style="font-weight: bold;width: 5px;"></th>            
                    <th style="font-weight: bold;">DESCRIPTION</th>
                    <th class="text-right" style="font-weight: bold;">QTY</th>
                    <th class="text-right" style="font-weight: bold;">UNIT PRICE</th>
                    <th class="text-right" style="font-weight: bold;">TOT.</th>
                </tr>
            </thead>

            <tbody id="data"></tbody>                      
        </table>
    
    </div>

    <div class="card-footer">
        <div class="d-flex align-items-center">
            <span class="text-sm text-muted font-italic">
                <button type="submit" class="btn btn-danger btn-sm" name="clear" style="border-radius: 4px;" title="Delete data">
                    <i class="fa fa-trash-o" aria-hidden="true"></i>
                </button>
            </span>
            <a href="gate?b=itm" class="flex-shrink-0 ml-auto" style="font-weight: bold;">Go back <i class="fa fa-mail-reply ml-2"></i> </a>
        </div>
    </div>
</div>  
</form>
       
<script>
  	function mymat(data){
		const ajaxreq =new XMLHttpRequest();
		ajaxreq.open('GET','getVal?typeid='+data,'TRUE');
		ajaxreq.send();
		ajaxreq.onreadystatechange= function(){

			if (ajaxreq.readyState== 4 && ajaxreq.status == 200) {

				document.getElementById('matid').
				innerHTML = ajaxreq.responseText;
				// alert(data2);
			}
		}

	}
</script>

<script>
//  SAVE ORDER
    $(document).ready(function(){
    displayData();
    $("#boqsave").submit(function(e){
        e.preventDefault();

    var formdata = new FormData(this);
        $.ajax({
            url: "boqsave",
            type: "POST",
            data: formdata,
            mimeTypes:"multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function(formData){
            // chkall(false);
            displayData();
            pop_up_request(formData);
            },error: function(){
                alert("okey");
            }
         });
      });
      
    // Notification  
    function pop_up_request(feedback) {
    jQuery(function validation(){
    swal("Well done! ", "Data saved successfully.", "success", {
    button: "ok",
      });
    });
    }
     
    //  TRASH ORDER
    $("#trashboq").submit(function(e){
        e.preventDefault();

        var formdata = new FormData(this);
        $.ajax({
            url: "trashboq",
            type: "POST",
            data: formdata,
            mimeTypes:"multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function(formData){
            // chkall(false);
            displayData();
            },error: function(){
                alert("okey");
            }
         });
      });
      
    
     //FETCH DATA
	function displayData(){
	$.ajax({
		type: 'POST',
		url: 'boqdata',
		data: {res: <?php echo $itemid;?>},
		success: function(data){
			$('#data').html(data)
		}
	});
	}
   });

</script>