<?php
$this->load->view ( $left );
$this->load->view ( $header );
?>
<style type="text/css">
    .ibox-content {
        border-style : none;
    }

    .hr-line-dashed {
        margin: 5px 0;
    }
</style>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2><?php echo ($item_id > 0) ? 'Edit Item' : 'Add Item';?></h2>
    </div>
</div>
<div class="wrapper wrapper-content">
    <div class="row text-center animated fadeInRightBig">
        <div class="col-lg-12">
            <form method="post" class="form-horizontal" id="invoice_data">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Item Name</label>
                            <div class="col-sm-10">
                                 <input type="text" name="item_name" id="item_name" class="form-control" placeholder="Item Name" value="<?php echo !empty($item_details->item_name) ? $item_details->item_name : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Description</label>
                            <div class="col-sm-10">
                                <input type="text" name="item_description" id="item_description" class="form-control" placeholder="Item Description" value="<?php echo !empty($item_details->item_description) ? $item_details->item_description : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Image</label>
                            <div class="col-sm-10">
                                <input name="image" id="image" type="file" accept="image/*;capture=camera" /> <img src="http://jeweldokan.in/sukhenjewel/<?php echo !empty($item_details->image) ? $item_details->image : '';?>" alt="image" width="100"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">UOM</label>
                            <div class="col-sm-10">
                                <select name="unit" id="unit" class="form-control">
                                    <option value="Pice" <?php echo (!empty($item_details->unit) && ($item_details->unit=='Pice')) ? 'selected="selected"' : '';?>>Pice</option>
                                    <option value="Pair" <?php echo (!empty($item_details->unit) && ($item_details->unit='Pair')) ? 'selected="selected"' : '';?>>Pair</option>
                                    <option value="Set" <?php echo (!empty($item_details->unit) && ($item_details->unit=='Set')) ? 'selected="selected"' : '';?>>Set</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Purchase Price</label>
                            <div class="col-sm-10">
                                <input type="text" name="rate_purchase" id="rate_purchase" class="typeahead_3 form-control" placeholder="Purchase Price" value="<?php echo !empty($item_details->rate_purchase) ? $item_details->rate_purchase : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Sales Rate</label>
                            <div class="col-sm-10">
                                <input type="text" name="rate_sale" id="rate_sale" class="form-control" placeholder="Sales Rate" value="<?php echo !empty($item_details->rate_sale) ? $item_details->rate_sale : '';?>">
                            </div>
                        </div>
                        <div class="form-group ">
                            <label class="col-sm-2 control-label">HSN Code</label>
                            <div class="col-sm-4">
                                 <input type="text" name="hsn_code" id="hsn_code" class="form-control" placeholder="HSN Code" value="<?php echo !empty($item_details->hsn_code) ? $item_details->hsn_code : '';?>">
                            </div>
                            <label class="col-sm-2 control-label">GST%</label>
                            <div class="col-sm-4">
                                <input type="text" name="gst_rate" id="gst_rate" class="form-control" placeholder="GST" value="<?php echo !empty($item_details->gst_rate) ? $item_details->gst_rate : '';?>">
                            </div>
                        </div>  
                        <div class="form-group ">
                            <label class="col-sm-2 control-label">Metal Used</label>
                            <div class="col-sm-4">
                                 <input type="text" name="metal_used" id="metal_used" class="form-control" placeholder="Metal" value="<?php echo !empty($item_details->metal_used) ? $item_details->metal_used : '';?>">
                            </div>
                            <label class="col-sm-2 control-label">Stone Used</label>
                            <div class="col-sm-4">
                                <input type="text" name="stone_used" id="stone_used" class="form-control" placeholder="Stone" value="<?php echo !empty($item_details->stone_used) ? $item_details->stone_used : '';?>">
                            </div>
                        </div>  
                        <div class="form-group ">
                            <label class="col-sm-2 control-label">Manufacturing</label>
                            <div class="col-sm-4">
                                 <input type="text" name="manufacturing_charge" id="manufacturing_charge" class="form-control" placeholder="Metal" value="<?php echo !empty($item_details->manufacturing_charge) ? $item_details->manufacturing_charge : '0';?>">
                            </div>
                            <label class="col-sm-2 control-label">Rhodium</label>
                            <div class="col-sm-4">
                                <input type="text" name="rhodium_charge" id="rhodium_charge" class="form-control" placeholder="Stone" value="<?php echo !empty($item_details->rhodium_charge) ? $item_details->rhodium_charge : '0';?>">
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="ibox">
                    <div class="ibox-content">
                        <div class="row">
                            <div class="col-lg-6 pull-right">
                                <div class="form-group">
                                    <div class="col-sm-12 col-sm-offset-2">
                                        <button class="btn btn-info save_item" type="button" data-status="0"><i class="fa fa-dropbox"></i><span class="m-l-xs">Save</span></button>
                                        <button class="btn btn-danger back_listing" type="button">Cancel</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="http://sastadokan.in/assets/js/bootstrap3-typeahead.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#item_name').focusout(function(){
            var item_name = $(this).val();
             $.ajax({
                url: '<?php echo site_url('items/add_items_name_check');?>',
                type: "POST",
                data:{'item_name' : item_name },
                success: function(data){ 
                  if(data == "1"){ alert("The name already exists");}
                }
            });
            
            
        });
        
        $('.save_item').on('click',function(){
            if ($('#item_name').val() == '') {
                $('#item_name').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#item_name').parent().parent().removeClass('has-error');
            }

            if ($('#item_description').val() == '') {
                $('#item_description').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#item_description').parent().parent().removeClass('has-error');
            }

            if ($('#unit').val() == '') {
                $('#unit').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#unit').parent().parent().removeClass('has-error');
            }

            if ($('#rate_purchase').val() == '') {
                $('#rate_purchase').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#rate_purchase').parent().parent().removeClass('has-error');
            }

            if ($('#hsn_code').val() == '') {
                $('#hsn_code').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#hsn_code').parent().parent().removeClass('has-error');
            }

            if ($('#gst_rate').val() == '') {
                $('#gst_rate').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#gst_rate').parent().parent().removeClass('has-error');
            }

            if ($('#rate_sale').val() == '') {
                $('#rate_sale').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#rate_sale').parent().parent().removeClass('has-error');
            }

            var item_name = $('#item_name').val();
            var item_description = $('#item_description').val();
            var unit = $('#unit').val();
            var rate_purchase = $('#rate_purchase').val();
            var hsn_code = $('#hsn_code').val();
            var gst_rate = $('#gst_rate').val();
            var rate_sale = $('#rate_sale').val();
            var metal_used = $('#metal_used').val();
            var stone_used = $('#stone_used').val();
            var manufacturing_charge = $('#manufacturing_charge').val();
            var rhodium_charge = $('#rhodium_charge').val();
            var image = $('#image')[0].files[0];

            var item_id = '<?php echo $item_id;?>';
            var status = $(this).data('status');

            var formData = new FormData();
            formData.append('item_id', item_id);
            formData.append('item_name', item_name);
            formData.append('item_description', item_description);
            formData.append('unit', unit);
            formData.append('rate_purchase', rate_purchase);
            formData.append('hsn_code', hsn_code);
            formData.append('gst_rate', gst_rate);
            formData.append('metal_used', metal_used);
            formData.append('stone_used', stone_used);
            formData.append('manufacturing_charge', manufacturing_charge);
            formData.append('rhodium_charge', rhodium_charge);
            formData.append('rate_sale', rate_sale);
            formData.append('image', image);

            $.ajax({
                url: '<?php echo site_url('items/add_items');?>',
                type: "POST",
                data:formData,
                processData: false,
                contentType: false,
                success: function(data){ 
                  window.location.href = '<?php echo site_url("items");?>';
                }
            });
        });
    });
</script>
<?php $this->load->view($footer); ?>
