<?php
include("../../packages/require.php");
include("controller/controller_edit.php");
$curpage = "po";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?=$title['po'];?></title>
    <?php include($global['root']."packages/head.php");?>
</head>
<body class="<?=$global['body-css'];?>">
    <div class="wrapper">

        <?php include($global['root']."section/header.php");?>

        <?php include($global['root']."section/sidebar.php");?>

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
              <h1>
                Module Purchase Order
                <small>Track all your purchase order here</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="<?=$path['dashboard'];?>"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Purchase Order</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <!-- Default box -->
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        Edit Purchase Order (<?=correctDisplay($data[0]['po_id']);?>)
                        <?php if($data[0]['po_status'] == 'open'){ ?>
                        <span class="label label-info">Open</span>
                        <?php } else if($data[0]['po_status'] == 'progress'){ ?>
                        <span class="label label-warning">In Progress</span>
                        <?php } else if($data[0]['po_status'] == 'prepared'){ ?>
                        <span class="label label-primary">Prepared</span>
                        <?php } else if($data[0]['po_status'] == 'complete'){ ?>
                        <span class="label label-success">Completed</span>
                        <?php } else { ?>
                        <span class="label label-danger">Canceled</span>
                        <?php } ?>
                    </h3>
                </div>
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li>
                            <a href="<?=$path['po-detail'].$po_id.'/';?>">Preview Purchase Order</a>
                        </li>
                        <?php if($acsp_edit == 1){ ?>
                        <?php if($data[0]['po_status'] == 'open'){ ?>
                        <li class="active">
                            <a href="<?=$path['po-edit'].$po_id.'/';?>">Edit Purchase Order</a>
                        </li>
                        <?php } ?>
                        <?php } ?>
                        <?php if($acsp_viewact == 1){ ?>
                        <li>
                            <a href="<?=$path['po-activity'].$po_id.'/';?>">Activity</a>
                        </li>
                        <?php } ?>
                        <?php if($acsp_document == 1){ ?>
                        <?php if($data[0]['po_status'] != 'open' && $data[0]['po_status'] != 'prepared' && $data[0]['po_status'] != 'cancel'){ ?>
                        <li>
                            <a href="<?=$path['po-document'].$po_id.'/';?>">Document</a>
                        </li>
                        <?php } ?>
                        <?php } ?>
                    </ul>
                    <input type="hidden" id="input-pageurl" value="<?=$path['po-edit'];?>">
                    <input type="hidden" id="input-poid" value="<?=$data[0]['po_id'];?>">
                    <input type="hidden" id="input-poppn" value="<?=$data[0]['po_ppn'];?>">
                </div>
                <form id="form-create" action="<?=$path['po-update'];?>" enctype="multipart/form-data" method="post" onsubmit="return validateFormSave();">
                    <div id="form-insert" class="group-form">
                        <div class="box-body">
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-ponumber">Purchase Order Number</label>
                                        <input type="text" class="form-control" id="input-ponumber" name="ponumber" placeholder="Purchase Order Number" value="<?=correctDisplay($data[0]['po_order_num']);?>">
                                        <input type="hidden" name="ponumberold" value="<?=correctDisplay($data[0]['po_order_num']);?>">
                                        <div id="error-ponumber" class="error-text"></div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-region">Region *</label>
                                        <?php if($acsp_admin_region != 1){ ?>
                                        <select class="form-control input-sort" name="region" id="input-region">
                                            <option value="">Choose Region</option>
                                            <?php if(is_array($data_region)){ foreach($data_region as $region){ ?>
                                            <option <?php if($data[0]['po_ref_region'] == $region['region_id']){ echo "selected"; } ?> value="<?=$region['region_id'];?>"><?=ucfirst($region['region_title']);?></option>
                                            <?php } } ?>
                                        </select>
                                        <?php } else { ?>
                                        <select class="form-control input-sort disable-input" name="region" id="input-region">
                                            <option value="">Choose Region</option>
                                            <?php if(is_array($data_region)){ foreach($data_region as $region){ ?>
                                            <option <?php if($_SESSION['admin_region'] == $region['region_id']){ ?> selected <?php } ?> value="<?=$region['region_id'];?>"><?=ucfirst($region['region_title']);?></option>
                                            <?php } } ?>
                                        </select>
                                        <?php } ?>
                                        <div id="error-region" class="error-text"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-pic">Person In Charge *</label>
                                        <select class="form-control input-sort" name="pic" id="input-pic">
                                            <option value="">Choose PIC</option>
                                            <?php foreach($data_admin as $admin){ ?>
                                            <option <?php if($data[0]['po_ref_admin'] == $admin['admin_id']){ echo "selected"; } ?> value="<?=$admin['admin_id'];?>"><?=ucfirst($admin['admin_name']);?></option>
                                            <?php } ?>
                                        </select>
                                        <div id="error-pic" class="error-text"></div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-document_date">Document Date *</label>
                                        <div class="input-group">
                                            <input id="input-document_date" type="text" name="document_date" class="form-control" placeholder="dd-mm-yyyy" value="<?=date("d-m-Y", strtotime($data[0]['po_date']));?>"><span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span>
                                        </div>
                                        <div id="error-document_date" class="error-text"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <div class="box-center">
                            <h3 id="address-head" class="box-title">Client Info</h3>
                        </div>
                        <div class="box-body">
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-address_name">Client Name *</label>
                                        <div class="group-select">
                                            <div id="group-cname">
                                                <input type="text" class="form-control client-input" name="cname" id="input-cname" placeholder="Client Name" autocomplete="off" value="<?=correctDisplay($data[0]['client_name']);?>">
                                                <div id="view-select" class="view-select">
                                                    <ul id="list-cname"></ul>
                                                </div>
                                                <span id="cname-refresh" class="input-group-btn" style="display: none;">
                                                    <button type="button" id="btn-refresh-client" class="btn btn-info btn-flat"><i class="fa fa-refresh"></i> Reset</button>
                                                </span>
                                            </div>
                                        </div>
                                        <input type="hidden" id="input-cid" name="clientid" value="<?=$data[0]['po_ref_client'];?>">
                                        <div id="error-cname" class="error-text"></div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-cemail">Email *</label>
                                        <input type="text" class="form-control client-input" name="cemail" id="input-cemail" placeholder="Email Address" value="<?=correctDisplay($data[0]['client_email']);?>">
                                        <div id="error-cemail" class="error-text"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-cphone">Phone Number *</label>
                                        <input type="text" class="form-control client-input" name="cphone" id="input-cphone" placeholder="Phone Number" value="<?=correctDisplay($data[0]['client_phone']);?>">
                                        <div id="error-cphone" class="error-text"></div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-csub">Sub Client</label>
                                        <input type="text" class="form-control" id="input-csub" name="csub" placeholder="Purchase Order Number" value="<?=correctDisplay($data[0]['po_subclient']);?>">
                                        <div id="error-csub" class="error-text"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-picc">Person In Charge (Admin Client)</label>
                                        <select class="form-control input-sort" name="picc" id="input-picc">
                                            <option value="">Choose PIC Client</option>
                                            <?php foreach($data_admin_client as $data_adc){ ?>
                                            <option <?php if($data[0]['po_ref_admin_client'] == $data_adc['admin_id']){ echo "selected"; } ?> value="<?=$data_adc['admin_id'];?>"><?=ucfirst($data_adc['admin_name']);?></option>
                                            <?php } ?>
                                        </select>
                                        <div id="error-picc" class="error-text"></div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <label for="input-caddress">Address </label>
                                        <textarea class="form-control client-input" id="input-caddress" name="caddress" placeholder="Notes" rows="5"><?=correctDisplay($data[0]['client_address']);?></textarea>
                                        <div id="error-caddress" class="error-text"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="box-center">
                            <div class="row">
                                <div class="col-sm-4 col-xs-12">
                                    <h3 id="item-head" class="box-title xs-down10" style="padding: 9px 0 0 0;">
                                        Purchase Order Item
                                    </h3>
                                </div>
                                <div class="col-sm-8 col-xs-12 text-right">
                                    <a href="#modalAdd" class="btn btn-primary full-xs xs-down10" data-toggle="modal" data-target="#modalAdd">
                                        <i class="fa fa-plus"></i> Add Item
                                    </a>
                                </div>
                            </div>
                        </div>
                        <div class="box-body">
                            <div class="preview-order" style="background-color: #ffffff;">
                                <div class="table-responsive table-order">
                                    <table class="table table-middle table-bordered">
                                        <thead>
                                            <tr>
                                                <th class="text-center">No</th>
                                                <th class="text-center">Action</th>
                                                <th class="text-center">Description</th>
                                                <th class="text-center">Product</th>
                                                <th class="text-center">Qty</th>
                                                <th class="text-center">Unit</th>
                                                <th class="text-center">Price</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-list">
                                            <?php $num=1; $total_item = count($items); foreach($items as $item){ ?>
                                            <tr>
                                                <td class='text-center'><?=$num;?></td>
                                                <td class="text-center">
                                                    <textarea class="hide" type="hidden" id="hdesc-<?=$item['atpo_id'];?>" name="hdesc[]"><?=correctDisplay($item['atpo_desc']);?></textarea>
                                                    <input type="hidden" id="hproduct-<?=$item['atpo_id'];?>" name="hproduct[]" value="<?=$item['atpo_product'];?>">
                                                    <input type="hidden" id="hqty-<?=$item['atpo_id'];?>" name="hqty[]" value="<?=$item['atpo_qty'];?>">
                                                    <input type="hidden" id="hunitid-<?=$item['atpo_id'];?>" name="hunit[]" value="<?=$item['atpo_ref_unit'];?>">
                                                    <input type="hidden" id="hunit-<?=$item['atpo_id'];?>" name="hunittxt[]" value="<?=$item['atpo_unit'];?>">
                                                    <input type="hidden" id="hprice-<?=$item['atpo_id'];?>"name="hprice[]" value="<?=$item['atpo_price'];?>">
                                                    <div class="btn-group">
                                                        <a href="javascript:void(0);" onclick="showEditData('<?=$item['atpo_id'];?>');" class="btn btn-xs btn-info">
                                                            <i class="fa fa-edit"></i> Edit
                                                        </a>
                                                        <?php if($total_item == 1){ ?>
                                                        <a href="javascript:void(0);" class="btn btn-xs btn-danger disable-state">
                                                            <i class="fa fa-remove"></i> Remove
                                                        </a>
                                                        <?php } else { ?>
                                                        <a href="javascript:void(0);" onclick="confirmRemove('<?=$item['atpo_id'];?>');" class="btn btn-xs btn-danger">
                                                            <i class="fa fa-remove"></i> Remove
                                                        </a>
                                                        <?php } ?>
                                                    </div>
                                                </td>
                                                <td class='text-center'><?=correctDisplay($item['atpo_desc']);?></td>
                                                <td class='text-center'><?=correctDisplay($item['atpo_product']);?></td>
                                                <td class='text-center'><?=$item['atpo_qty'];?></td>
                                                <td class='text-center'><?=$item['atpo_unit'];?></td>
                                                <td class='text-right'><?=convert_idr($item['atpo_price']);?></td>
                                            </tr>
                                            <?php $num++;} ?>
                                        </tbody>
                                        <tbody id="table-loader">
                                            <tr style="background-color: #ffffff !important;">
                                                <td colspan="15">
                                                    <div class="loader-image">
                                                        <img src="<?=$global['absolute-url'].'img/load.gif';?>" align="icon">
                                                    </div>
                                                </td>
                                            </tr>
                                        </tbody>
                                        <tfooter>
                                            <tr>
                                                <td colspan="6" class="text-right bold">Subtotal</td>
                                                <td class="text-right bold txt-subtotal"><?=convert_idr($data[0]['po_subtotal']);?></td>
                                            </tr>
                                            <tr>
                                                <td colspan="6" class="text-right bold">PPN (<span id="tax-num"><?=$data[0]['po_ppn'];?></span>%)</td>
                                                <td class="text-right bold txt-ppnprice"><?=convert_idr($data[0]['po_ppn_amount']);?></td>
                                            </tr>
                                            <tr>
                                                <td colspan="6" class="text-right bold">Total</td>
                                                <td class="text-right bold txt-total"><?=convert_idr($data[0]['po_total']);?></td>
                                            </tr>
                                        </tfooter>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="box-footer clearfix">
                            <div class="row">
                                <div class="col-sm-6 col-xs-6">
                                    <div class="text-left">
                                        <a href="<?=$path['po-detail'].$po_id.'/';?>" class="btn btn-default">
                                            <i class="fa fa-arrow-left"></i> Back
                                        </a>
                                    </div>
                                </div>
                                <div class="col-sm-6 col-xs-6">
                                    <input type="hidden" id="redir-url" value="<?=$path['po'];?>">
                                    <input type="hidden" name="poid" value="<?=$data[0]['po_id'];?>">
                                    <input type="hidden" id="total-list" name="total_item" value="<?=count($items);?>">
                                    <div class="text-right">
                                        <button id="btn-submit" class="btn btn-success" type="submit">
                                            <i class="fa fa-save"></i> Save Data
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <!-- /.box-footer-->
            </div>
        </div>
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<?php include($global['root']."section/footer.php");?>

</div>

<div id="modalAdd" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog madd-dialog" role="document">
        <div class="modal-content madd-content">
            <div class="modal-header madd-header" style="background-color: #398439;">
                Add Purchase Order Item
            </div>
            <form id="form-add" action="<?=$path['po-edit'].'?action=addItem';?>" enctype="multipart/form-data" method="post" onsubmit="return validateFormItem();">
                <div class="modal-body madd-body" id="quarantine-list">
                    <div class='group-qrt'>
                        <div class="row">
                            <div class="col-sm-12 down1">
                                <div class="form-group">
                                    <label for="input-adesc">Description *</label>
                                    <textarea class="form-control input-madd" id="input-adesc" name="adesc" placeholder="Description" rows="5"></textarea>
                                    <div id="error-adesc" class="error-text"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12 down1">
                                <div class="form-group">
                                    <label for="input-aproduct">Product *</label>
                                    <input type="text" class="form-control" id="input-aproduct" name="aproduct" placeholder="Product">
                                    <div id="error-aproduct" class="error-text"></div>
                                </div>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-sm-6 down1'>
                                <div class="form-group">
                                    <label for="input-aqty">Qty *</label>
                                    <input type="number" name="aqty" id="input-aqty" class="form-control input-madd" min="1" max="" value="" placeholder="0">
                                    <div id="error-aqty" class="error-text"></div>
                                </div>
                            </div>
                            <div class="col-sm-6 down1">
                                <div class="form-group">
                                    <label for="input-aunit">Unit *</label>
                                    <select class="form-control input-madd" name="aunit" id="input-aunit">
                                        <option value="">Choose Unit</option>
                                        <?php foreach($data_unit as $unit){ ?>
                                        <option value="<?=$unit['unit_id'];?>"><?=ucfirst($unit['unit_title']);?></option>
                                        <?php } ?>
                                    </select>
                                    <div id="error-aunit" class="error-text"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12 down1">
                                <label for="input-aprice">Price *</label>
                                <div class="input-group">
                                    <span class="input-group-addon">IDR</span>
                                    <input type="number" id="input-aprice" name="aprice" class="form-control input-madd" min="1" max="" value="" placeholder="0">
                                </div>
                                <div id="error-aprice" class="error-text"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer madd-footer">
                    <div class="row">
                        <div class="col-sm-6 col-xs-6">
                            <div class="text-left">
                                <a href="#" data-dismiss="modal" class="btn btn-default">
                                    <i class="fa fa-arrow-left"></i> Cancel
                                </a>
                            </div>
                        </div>
                        <div class="col-sm-6 col-xs-6">
                            <div class="text-right">
                                <input type="hidden" value="<?=$data[0]['po_id'];?>" name="po_id">
                                <input type="hidden" value="<?=$data[0]['po_ppn'];?>" name="po_ppn">
                                <input type="hidden" id="input-aunittxt" value="" name="aunittxt">
                                <button id="btn-create" class="btn btn-success" type="submit">
                                    <i class="fa fa-save"></i> Create Item
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div id="modalEdit" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog madd-dialog" role="document">
        <div class="modal-content madd-content">
            <div class="modal-header madd-header" style="background-color: #0369e8;">
                Edit Purchase Order Item
            </div>
            <form id="form-edit" action="<?=$path['po-edit'].'?action=editItem';?>" enctype="multipart/form-data" method="post" onsubmit="return validateFormItemEdit();">
                <div class="modal-body madd-body" id="quarantine-list">
                    <div class='group-qrt'>
                        <div class="row">
                            <div class="col-sm-12 down1">
                                <div class="form-group">
                                    <label for="input-edesc">Description *</label>
                                    <textarea class="form-control input-medit" id="input-edesc" name="edesc" placeholder="Description" rows="5"></textarea>
                                    <div id="error-edesc" class="error-text"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12 down1">
                                <div class="form-group">
                                    <label for="input-eproduct">Product *</label>
                                    <input type="text" class="form-control" id="input-eproduct" name="eproduct" placeholder="Product">
                                    <div id="error-eproduct" class="error-text"></div>
                                </div>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-sm-6 down1'>
                                <div class="form-group">
                                    <label for="input-eqty">Qty *</label>
                                    <input type="number" name="eqty" id="input-eqty" class="form-control input-medit" min="1" max="" value="" placeholder="0">
                                    <div id="error-eqty" class="error-text"></div>
                                </div>
                            </div>
                            <div class="col-sm-6 down1">
                                <div class="form-group">
                                    <label for="input-eunit">Unit *</label>
                                    <select class="form-control input-medit" name="eunit" id="input-eunit">
                                        <option value="">Choose Unit</option>
                                        <?php foreach($data_unit as $unit){ ?>
                                        <option value="<?=$unit['unit_id'];?>"><?=ucfirst($unit['unit_title']);?></option>
                                        <?php } ?>
                                    </select>
                                    <div id="error-eunit" class="error-text"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12 down1">
                                <label for="input-eprice">Price *</label>
                                <div class="input-group">
                                    <span class="input-group-addon">IDR</span>
                                    <input type="text" id="input-eprice" name="eprice" class="form-control input-medit" value="">
                                </div>
                                <div id="error-eprice" class="error-text"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer madd-footer">
                    <div class="row">
                        <div class="col-sm-6 col-xs-6">
                            <div class="text-left">
                                <a href="#" data-dismiss="modal" class="btn btn-default">
                                    <i class="fa fa-arrow-left"></i> Cancel
                                </a>
                            </div>
                        </div>
                        <div class="col-sm-6 col-xs-6">
                            <div class="text-right">
                                <input type="hidden" value="<?=$data[0]['po_id'];?>" name="po_id">
                                <input type="hidden" value="<?=$data[0]['po_ppn'];?>" name="po_ppn">
                                <input type="hidden" id="input-eunittxt" value="" name="eunittxt">
                                <input type="hidden" id="input-erow" value="" name="atpo_id">
                                <button id="btn-edit" class="btn btn-info" type="submit">
                                    <i class="fa fa-save"></i> Edit Item
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include($global['root']."packages/footer.php");?>
<script type="text/javascript">
    $(document).ready(function() {
        <?php if(is_array($vmessage)){ ?>
        <?php $c=0; foreach($vmessage as $vm){ if($c > 0){ $time =  5000 + ($c*1000); } else { $time = 5000; } ?>
            var alertText = "<?=$vm;?>";
            <?php if($valert[$c] != "success"){ ?>
            errorAlertTimer(alertText,<?=$time;?>);// error alert
            <?php } else { ?>
            successAlertTimer(alertText,<?=$time;?>);// success alert
            <?php } ?>
        <?php $c++;} ?>
        <?php } ?>
    });
</script>
<script src="<?=$global['absolute-url'];?>module/po/js/edit.js?<?=mt_rand(10,1000);?>"></script>
</body>
</html>