  <?php 
    $this->load->view('templates/header');

    if(isset($_GET['search_field']) || isset($_GET['search_keyword']) || isset($_GET['order']))
    {
        $search_field      = $_GET['search_field'];
        $search_keyword    = $_GET['search_keyword'];
        $order             = $_GET['order'];
    }
    else
    {
        $search_field      = "";
        $search_keyword    = "";
        $order             = "";   
    }

  ?>
    <script>
        var link = "master";
        var sub_link = "rapaport";
    </script>
    <!-- Content Header (Page header) -->
    <section class="content-header">
        
        <div class="row">
            <div class="col-md-2">
                <h4>RAPAPORT<small>Master</small></h4>
            </div>
            
            <div class="col-md-2">
                <div class="upload-btn-wrapper cust_file_upload" style="width: 100% !important;">
                    <button type="button" class="btn btn-success" id="sbt_r_btn" style="width: 100% !important;margin-top: 8px;" onclick="update_rp_rate(1)">UPDATE ROUND DIAMOND PRICE</button>
                </div>
            </div>
            <div class="col-md-2">
                <button type="button" class="btn btn-success" id="sbt_f_btn" style="width: 100% !important;margin-top: 8px;" onclick="update_rp_rate(2)">UPDATE FANCY DIAMOND PRICE</button>
            </div>
            <div class="col-sm-2">
                <div class="alert_msg" style="height: 20px;"></div>
            </div>
            <div class="col-md-4" style="text-align: right;">
                <ol class="breadcrumb" style="background-color: #ecf0f5;">
                    <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                    <li><a href="#">Forms</a></li>
                    <li class="active">Rapaport Master</li>
                </ol>
            </div>
        </div>
    </section>

    <!-- Main content -->
    <section class="content" style="padding-top: 0px;">
        <div class="row">
            <div class="col-md-12">     
                <div class="box">
                    <div class="box-body">
                        <div class="row">
                            <form enctype="multipart/form-data" action="<?php echo base_url('master/masterctrl/rapaport_master')?>">
                                <div class="form-group">
                                    <div class="col-sm-2">
                                        <select name="search_field" class="form-control cust_search_ip" id="search_field" onchange="clear_search_keyword();">
                                                <?php
                                                    foreach ($search_data as $key => $value):
                                                        if($search_field != "" && $search_field == $key)
                                                            echo "<option value='$key' selected>$value</option>";
                                                        else
                                                            echo "<option value='$key'>$value</option>";
                                                    endforeach;
                                                ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-2">
                                        <input type="text" name="search_keyword" id="search_keyword" class="form-control cust_search_ip" value="<?php echo $search_keyword?>" placeholder="Enter Keyword" autocomplete="off"> 
                                    </div>
                                    <div class="col-sm-2">
                                        <select name="order" class="form-control cust_search_ip" id="order">
                                            <option value="desc" <?php echo ($order =='desc')?'selected':''?>>DESC</option>
                                            <option value="asc" <?php echo ($order =='asc')?'selected':''?>>ASC</option>
                                        </select>
                                    </div>
                                
                                    <div class="col-sm-1">
                                        <button type="submit" class="btn btn-primary" title="search" style="width: 100%;">Search</button>
                                    </div>
                                    <div class="col-md-1">
                                        <a href="<?php echo base_url('master/masterctrl/rapaport_master');?>" class="btn btn-danger" title="reset page" style="width: 100%;">Reset </a>
                                    </div>
                                    <div class="col-md-1">
                                        <button type="button" class="btn btn-primary" onClick="add_update_rapaport_popup(0);" title="add city" style="width: 100%;">Add</button>
                                    </div>
                                    <div class="col-sm-2">
                                        <button type="button" class="btn btn-primary" style="width: 100%;text-align: left;background-color: #565a5c;border-color: #565a5c;">COUNT : <?php echo !empty($total_rows)?$total_rows:'' ?></button>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <?php
                            if($rapaport_data):
                        ?>
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Diamond Type</th>
                                    <th>Clarity</th>
                                    <th>Range From</th>
                                    <th>Range To</th>
                                    <th>Color</th>
                                    <th>Result</th>
                                    <th>Edit</th>    
                                    <th>Delete</th>    
                                </tr>
                            </thead>
                            <tbody>
                        <?php
                            $count = isset($_GET['offset'])?$_GET['offset']:'0';
                            foreach ($rapaport_data as $key => $value):
                        ?>
                                <tr>
                                    <td><?php echo ++$count?></td>
                                    <td><?php echo $value['dm_code']?></td>
                                    <td><?php echo $value['rp_clarity']?></td>
                                    <td><?php echo $value['rp_range_from']?></td>
                                    <td><?php echo $value['rp_range_to']?></td>
                                    <td><?php echo $value['rp_color']?></td>
                                    <td><?php echo $value['rp_result']?></td>

                                    <td><span class="glyphicon glyphicon-pencil" style="cursor:pointer;" onClick="add_update_rapaport_popup(<?php echo $value['rp_id']?>)"></span></td>
                                    <td><span class="glyphicon glyphicon-trash" onClick="delete_master_item(<?php echo $value['rp_id']?>,'rapaport_delete');"></span></td>
                                </tr> 
                        <?php
                            endforeach;
                        ?>
                            </tbody>
                        </table>
                        <?php 
                            echo $this->pagination->create_links();
                        else:
                            echo "<h1 class='text-center'>No result found!</h1>";
                        endif;
                        ?>    
                    </div>
                </div>
            </div>
        </div>
    </section>
 

  <?php 
    $this->load->view('templates/footer');
   ?>
   <script src="<?php echo assets('js/master.js')?>"></script> 
       <script type="text/javascript">
            $('#import_csv').on('submit', function(event){
                event.preventDefault();
                $.ajax({
                    url:"<?php echo base_url(); ?>master/masterctrl/import",
                    method:"POST",
                    data:new FormData(this),
                    contentType:false,
                    cache:false,
                    processData:false,
                    beforeSend:function(){
                        $('#import_csv_btn').html('Importing...');
                    },
                    success:function(data)
                    {
                        $('#import_csv')[0].reset();
                        $('#import_csv_btn').attr('disabled', false);
                        $('#import_csv_btn').html('Import Done');
                        // load_data();

                        if (data == 1)
                        {
                            alert('Rapaport Updated Successfully');
                            location.reload();
                        }
                        else
                        {
                            alert('There is an Error, Please try again.');
                        }
                    }
                })
            });
    </script>