<script type="text/javascript" src="<?php echo base_url();?>assets/js/purchase.js"></script>
<aside class="right-side">
    <section class="content-header">
        <div class="title_bar">
            <h1 class="major_txt_color">Purchase Entry 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="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-xs-12">
                                <div class="col-md-6">
                                    <div class="form-group float-clear">
                                        <label>Supplier Name<span class="required">*</span></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->name;?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="form-group float-clear">
                                        <label>Invoice No<span class="required">*</span></label>
                                        <input type="text" class="form-control" name="invoice_no" value="<?php echo $purchase->invoice_no;?>" />
                                    </div>
                                    
                                    <div class="form-group float-clear">
                                        <label>Invoice Date<span class="required">*</span></label>
                                        <div class="input-group">
                                            <input type="text" class="form-control datepicker" name="invoice_date" placeholder="DD-MM-YYYY" 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="col-xs-12">
                                <div class="col-md-6">
                                    <h4 class="major_txt_color">Item Details<span class="required">*</span></h4>
                                </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_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_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_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_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-xs-12" style="margin-top:2%;">    
                                <div class="col-md-6">
                                    <div class="form-group float-clear">
                                        <label>Total Amount</label>
                                        <div class="input-group">
                                            <div class="input-group-addon">$</div>
                                            <input type="text" class="form-control" name="total" readonly value="<?php echo $purchase->total_amount;?>" />
                                        </div>
                                    </div>
                                    <div class="form-group float-clear">
                                        <label>Payment Status<span class="required">*</span></label>
                                        <select name="payment" class="form-control select2">
                                            <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>
                        </div>
                        <div class="box-footer form-actions float-clear">
                            <input class="btn reset" type="reset" value="Reset" />
                            <input type="submit" name="submit" value="Update" class="btn btn-success btn-primary" />
                        </div>
                    </div><!-- /.box -->
                </form>
            </div><!-- /.col-xs-12 -->
        </div><!-- /.row -->

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