<script type="text/javascript" src="<?php echo base_url();?>assets/js/inventory_purchase.js"></script>
<style>
.form-horizontal .nopadding {
	padding: 0px;
}
.form-horizontal .form-control {
	width: 100%;
	min-width: 100%;
	max-width: 100%;
}
.form-horizontal .input-group {
	max-width: 100%;
	min-width: 100%;
}
</style>
<aside class="right-side">
    <section class="content-header">
        <div class="title_bar">
            <h1 class="major_txt_color">Inventory – Purchase Management Edit</h1>
            <div class="breadcrumb_container"><?php echo breadcrumb_links();?></div>
        </div>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <form action="<?php echo $form_action;?>" method="post" name="purchase_form" class="form-horizontal edit_form" enctype="multipart/form-data" method="post">    
                    <input type="hidden" name="hidden_id" value="<?php echo $purchase->id;?>" />    
                    <div class="box box-danger">
                        <div class="box-body">
                            <div class="box-body table-responsive" style="display: none;">
                                <div class="alert alert-danger alert-dismissable">
                                    <i class="fa fa-thumbs-o-down"></i>
                                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                    <h4></h4>
                                </div>
                            </div>
                            <ul class="error_container"></ul>
                            <div class="col-md-12">
                              <div class="form-group">
                                <div class="col-md-4">
                                    <label>Supplier Name: </label>
                                    <select name="supplier_id" class="form-control select2">
                                        <option value="">Select Supplier Name</option>
                                        <?php foreach($suppliers as $key=>$supplier) {?>
                                        <option value="<?php echo $supplier->id;?>" <?php echo ($supplier->id == $purchase->supplier_id) ? 'selected' : '';?>><?php echo $supplier->ven_company;?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                	<label>Purchase Ref No: </label>
                                    <input type="text" class="form-control" name="purchase_ref_no" placeholder="Purchase Ref No" title="Purchase Ref No" alt="Purchase Ref No" value="<?php echo $purchase->purchase_ref_no; ?>" />
                                </div>
                              </div>
                              <div class="form-group">
                                <div class="col-md-4">
                                	<label>Invoice No: </label>
                                  	<input type="text" class="form-control" name="invoice_no" placeholder="Invoice No" value="<?php echo $purchase->invoice_no;?>" />
                                </div>
                                <div class="col-md-4">
                                  <div class="input-group">
                                  	<label>Invoice Date: </label>
                                    <div class="input-group">
                                        <input type="text" class="form-control datepicker" name="invoice_date" placeholder="Invoice Date" value="<?php echo date('d-m-Y',strtotime($purchase->invoice_date));?>" />
                                        <div class="input-group-addon"><i class="fa fa-calendar"></i></div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                              <div class="form-group">
                                <div class="col-md-4">
                                	<label>Purchase for: </label>
                                  	<input type="text" class="form-control" name="purchase_for" placeholder="Purchase for" value="<?php echo $purchase->purchase_for;?>" />
                                </div>
                                <div class="col-md-4">
                                  <div class="input-group">
                                  	<label>Receivable Date: </label>
                                    <div class="input-group">
                                        <input type="text" class="form-control datepicker" name="received_date" placeholder="Receivable Date" value="<?php echo ($purchase->receivable_date != "0000-00-00") ? date('d-m-Y',strtotime($purchase->receivable_date)) : "";?>"/>
                                        <div class="input-group-addon"><i class="fa fa-calendar"></i></div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                            
                            <div class="col-xs-12">
                                <div class="col-md-6">
                                    <h4 class="major_txt_color">Item Details<span class="required">*</span></h4>
                                </div>
                                
                                <div class="row">
                                    <div class="col-xs-12">        
                                        <div class=" float-clear">
                                            <div>
                                                <div class="col-md-3">
                                                    <div class="form-group"><label>Category Name</label></div>
                                                </div> 
                                                
                                                <div class="col-md-3">
                                                    <div class="form-group"><label>Item Name</label></div>
                                                </div>
                                                
                                                <div class="col-md-1">
                                                    <div class="form-group"><label>UOM</label></div>
                                                </div>
                                                
                                                <div class="col-md-2">
                                                    <div class="form-group"><label>Price</label></div>
                                                </div>
                                                
                                                <div class="col-md-2">
                                                    <div class="form-group"><label>Amount</label></div>
                                                </div>            
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div> 
                            
                            <div class="col-xs-12">
                                <?php $length = count($used_po_items); ?>
                                <?php foreach($used_po_items as $k=> $items){
                                       $readonly = ($items->status == 9 || $items->not_issued_qty != $items->quantity) ? 'readonly' : '';
                                       $item_details = $this->purchase_management_model->get_items_cat_id($items->category_id,$items->item_id);
                                       ?>
                                    <input type="hidden" class="edit_item_id" name="edit_item_id[<?php echo $k;?>]" value="<?php echo $items->id;?>"/>
                                    <input type="hidden" name="inv_item_ids[<?php echo $k; ?>]" value="<?php echo $items->item_id; ?>" />
                                    <input type="hidden" name="old_qty[<?php echo $items->id;?>]" value="<?php echo $items->quantity;?>">
                                    
                                    <div class=" float-clear">
                                        <div>
                                            <input type="hidden" name="poi_id[<?php echo $k;?>]" class="poi_id" value="<?php echo $items->id;?>" />
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                   <?php if(empty($readonly)) { ?>
                                                        <select name="category_id[<?php echo $k;?>]" class="form-control select2 category_id" onchange="get_item_by_cat_id(this);">
                                                            <option value="">Select Category Name</option>
                                                            <?php foreach($categories as $key=>$category) {?>
                                                                <option value="<?php echo $category->id;?>" <?php echo ($category->id == $items->category_id) ? 'selected' : '';?>><?php echo $category->name;?></option>
                                                            <?php } ?>
                                                        </select>
                                                   <?php } else {?>
                                                        <select name="category_id[<?php echo $k;?>]" class="form-control select2 category_id" onchange="get_item_by_cat_id(this);" disabled>
                                                            <option value="">Select Category Name</option>
                                                            <?php foreach($categories as $key=>$category) {?>
                                                                <option value="<?php echo $category->id;?>" <?php echo ($category->id == $items->category_id) ? 'selected' : '';?>><?php echo $category->name;?></option>
                                                            <?php } ?>
                                                        </select>
                                                        <input type="hidden" class="form-control category_id" name="category_id[<?php echo $k;?>]" value="<?php echo $items->category_id;?>"/>
                                                   <?php } ?>
                                                </div>
                                            </div> 
                                            
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <?php if(empty( $readonly )) { ?>
                                                        <select name="item_id[<?php echo $k;?>]" class="form-control select2 item_id" onchange="get_unit_by_item_id(this);">
                                                            <?php $item = $this->purchase_management_model->get_items_by_cat_id($items->category_id);?>
                                                            <option value="">Select Item Name</option>
                                                            <?php foreach($item as $key=>$value) {?>
                                                                <option value="<?php echo $value->id;?>" <?php echo ($value->id == $items->item_id) ? 'selected' : '';?>><?php echo $value->item_name;?></option>
                                                            <?php } ?>
                                                        </select>
                                                    <?php } else {?>
                                                        <select name="item_id[<?php echo $k;?>]" class="form-control select2 item_id" onchange="get_unit_by_item_id(this);" disabled>
                                                            <?php $item = $this->purchase_management_model->get_items_by_cat_id($items->category_id);?>
                                                            <option value="">Select Item Name</option>
                                                            <?php foreach($item as $key=>$value) {?>
                                                                <option value="<?php echo $value->id;?>" <?php echo ($value->id == $items->item_id) ? 'selected' : '';?>><?php echo $value->item_name;?></option>
                                                            <?php } ?>
                                                        </select>
                                                        <input type="hidden" class="form-control item_id" name="item_id[<?php echo $k;?>]" value="<?php echo $items->item_id;?>"/>
                                                   <?php } ?>
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-1">
                                                <div class="form-group">
                                                    <input type="text" class="form-control cls-w-70 qty integer_value" name="qty[<?php echo $k;?>]" placeholder="Qty" onkeyup="calculate_amount_by_qty(this)" value="<?php echo $items->quantity;?>" <?php echo $readonly;?> />
                                                    
                                                    <?php if(!empty($items->unit_name)) { ?><span class="uom" style="color:#0B45B3;"><b>UOM:<?php echo $items->unit_name;?></b></span><?php } ?>
                                                    <input type="hidden" class="form-control hidden_uom" name="hidden_uom[<?php echo $k;?>]" value="<?php echo $items->uom;?>" />
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-2">
                                                <div class="form-group">
                                                    <div class="input-group cls-w-100">
                                                        <div class="input-group-addon">$</div>
                                                        <input type="text" class="form-control float_value cls-w-100 price" name="price[<?php echo $k;?>]" placeholder="Price" onkeyup="calculate_amount_by_price(this);" value="<?php echo $items->unit_price;?>" <?php echo $readonly;?> />
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-2">
                                                <div class="form-group">
                                                    <div class="input-group cls-w-100">
                                                        <div class="input-group-addon">$</div>
                                                        <input type="text" class="form-control cls-w-100 amount sub_total" name="amount[<?php echo $k;?>]" placeholder="Amount" readonly value="<?php echo $items->amount;?>" <?php echo $readonly;?> />
                                                    </div>
                                                </div>
                                            </div>
                                            
                                        </div>
                                    </div>
                                <?php } ?>
                            </div> <br/>
                            
                            <div class="col-xs-12 item_container">
                                <?php $length = count($po_items); ?>
                                <?php foreach($po_items as $k=> $items) { ?>
                                    <input type="hidden" class="edit_item_id" name="edit_item_id[<?php echo $k;?>]" value="<?php echo $items->id;?>"/>
                                    <input type="hidden" name="inv_item_ids[<?php echo $k; ?>]" value="<?php echo $items->item_id; ?>" />
                                    <input type="hidden" name="old_qty[<?php echo $items->id;?>]" value="<?php echo $items->quantity;?>">
                                    
                                    <div class="item float-clear">
                                        <div class="item_details">
                                            <input type="hidden" name="poi_id[<?php echo $k;?>]" class="poi_id" value="<?php echo $items->id;?>" />
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <select name="category_id[<?php echo $k;?>]" class="form-control select2 category_id" onchange="get_item_by_cat_id(this);">
                                                        <option value="">Select Category Name</option>
                                                        <?php foreach($categories as $key=>$category) {?>
                                                            <option value="<?php echo $category->id;?>" <?php echo ($category->id == $items->category_id) ? 'selected' : '';?>><?php echo $category->name;?></option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                            </div> 
                                            
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <select name="item_id[<?php echo $k;?>]" class="form-control select2 item_id" onchange="get_unit_by_item_id(this);">
                                                        <?php $item = $this->purchase_management_model->get_items_by_cat_id($items->category_id);?>
                                                        <option value="">Select Item Name</option>
                                                        <?php foreach($item as $key=>$value) {?>
                                                            <option value="<?php echo $value->id;?>" <?php echo ($value->id == $items->item_id) ? 'selected' : '';?>><?php echo $value->item_name;?></option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-1">
                                                <div class="form-group">
                                                    <input type="text" class="form-control cls-w-70 qty integer_value" name="qty[<?php echo $k;?>]" placeholder="Qty" onkeyup="calculate_amount_by_qty(this)" value="<?php echo $items->quantity;?>" />
                                                    
                                                    <?php if(!empty($items->unit_name)) { ?><span class="uom" style="color:#0B45B3;"><b>UOM:<?php echo $items->unit_name;?></b></span><?php } ?>
                                                    <input type="hidden" class="form-control hidden_uom" name="hidden_uom[<?php echo $k;?>]" value="<?php echo $items->uom;?>" />
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-2">
                                                <div class="form-group">
                                                    <div class="input-group cls-w-100">
                                                        <div class="input-group-addon">$</div>
                                                        <input type="text" class="form-control float_value cls-w-100 price" name="price[<?php echo $k;?>]" placeholder="Price" onkeyup="calculate_amount_by_price(this);" value="<?php echo $items->unit_price;?>" />
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-2">
                                                <div class="form-group">
                                                    <div class="input-group cls-w-100">
                                                        <div class="input-group-addon">$</div>
                                                        <input type="text" class="form-control cls-w-100 amount sub_total" name="amount[<?php echo $k;?>]" placeholder="Amount" readonly value="<?php echo $items->amount;?>" />
                                                    </div>
                                                </div>
                                            </div>
                                            
                                        </div>
                                        <div class="add_row_action">
                                            <label class="">
                                                <?php if ($length == ($k + 1)) {?>
                                                    <i class="fa fa-2x fa-plus-circle add-row addRow_color"></i>
                                                <?php }?>
                                                <?php if ($length > 1) {?>
                                                    <i class="fa fa-2x fa-minus-circle remove-row addRow_color"></i>
                                                <?php }?>
                                            </label>
                                        </div>
                                    </div>
                                <?php } ?>
                            </div> <br/>
                            
                            <div class="col-md-12" style="margin-top:2%;">              
                              <div class="col-md-4">                              
                                <div class="form-group">
                                  <div class="input-group cls-w-100">
                                    <div class="input-group-addon">$</div>
                                    <input type="text" class="form-control" name="total" placeholder="Total Amount" value="<?php echo $purchase->total_amount;?>" readonly />
                                  </div>
                                </div>
                                <div class="form-group">
                                  <select name="payment" class="form-control select2">
                                    <option value="">Select Payment Status</option>
                                    <option value="1" <?php echo ($purchase->payment == 1) ? 'selected' : '';?>>Unpaid</option>
                                    <option value="2" <?php echo ($purchase->payment == 2) ? 'selected' : '';?>>Paid</option>
                                  </select>
                                </div>
                              </div>
                              <div class="col-md-6">
                                <div class="form-group">
                                  <textarea class="form-control" name="desc" style="min-width: 100%; height:85px;" placeholder="Description"><?php echo $purchase->desc; ?></textarea>
                                </div>
                              </div>
                            </div>
                        </div>
                        <div class="box-footer form-actions float-clear">
                        	<input type="submit" name="submit" value="Update" class="btn btn-primary" style="padding: 5px 50px;"/>
            				<input class="btn btn-primary reset" type="reset" value="Reset" style="padding: 5px 50px;"/>
                        </div>
                    </div><!-- /.box -->
                </form>
            </div><!-- /.col-xs-12 -->
        </div><!-- /.row -->

    </section><!-- /.content -->
</aside>