<?php require('../connection.php');
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `dd_plant` WHERE `id`='".$_REQUEST['id']."'")or die(mysqli_error("getItems: ".$GLOBALS['con']));
$stmt->execute();$rslt=$stmt->get_result();
$d=mysqli_fetch_array($rslt);

?>
<form id="newForm">
    <div class="row">
        <div class="col-12 col-md-6 mb-2">
        <span>Code</span>
        <input type="text" class="form-control form-control-sm" aria-label="code" id="newcode" name="code" value="" />
        </div>
        <div class="col-12 col-md-6 mb-2">
        <span>Description</span>
        <input type="text" class="form-control form-control-sm" aria-label="formtype" id="newdesc" name="desc" value="" />
        </div>
        <div class="col-12 col-md-6 mb-2">
        <span>Frequency</span>
        <input type="text" class="form-control form-control-sm" aria-label="frequency" id="newfreq" name="freq" value="" />
        </div>
        <div class="col-12 col-md-6 mb-2">
        <span>Size</span>
        <input type="text" class="form-control form-control-sm" aria-label="size" name="size" value="" />
        </div>
        <div class="col-12 col-md-6 mb-2">
        <span>IU</span>
        <input type="text" class="form-control form-control-sm" aria-label="units" name="units" value="" />
        </div>
        <input type="hidden" name="mode" value="plant">
        <input type="hidden" name="FUNCTION" value="save">
    </div>
</form>
<script>
    $('#popupBox .modal-header').html('<a href="javascript:void(0)" class="sa">New Plant Entry</a>');
    $('#popupBox .modal-footer').html('<div class="col-12 text-center"><a href="javascript:void(0)" class="btn btn-info" onClick="updateEntry()"><i class="fad fa-save" aria-hidden="true"></i> Save Changes</a></div>');


    function updateEntry(){
confirmBox('Update Entry','Sure you wish to save this entry?','saveItem()')

}

function saveItem(){
event.preventDefault();
var newCode=$('#newcode').val();
var newDesc=$('#newdesc').val();
var newFreq=$('#newfreq').val();

if(newCode=="" || newDesc=="" || newFreq==""){response('warning','You must have a valid CODE, DESCRIPTION and FREQUENCY to save a new code'); $('#confirmBox').modal('hide')}
else{
$.ajax({
url:'includes/master/jump_functions.php',
type:'POST',
data:$('#newForm').serialize(),
success:function(result){
if($.trim(result)=="s"){
    $.post('includes/master/jump_new_functions.php',{FCN:'genNewLists'},function(ret){
       response('success','Your code has been saved successfully');
       $('#confirmBox').modal('hide')
       closePop()

       var myObject = $.trim(ret)
       myObject=myObject.split('[SPLIT]');
       $('#code').html(myObject[0])
       $('#desc').html(myObject[1])
       $('#code').val(newCode);
       $('#desc').val(newDesc);
    })
}
else{response('danger','The code is already in the system')}
}


});
}
}

</script>
