<?php 
Class Aj_data extends AdminController
{

  function index(){
        // if(empty($this->session->userdata('admin_redirect_link'))  {
        //   echo "<script>window.location.href='login.php';</script>";
        // }

    $action = $_GET['action'];

    switch ($action) { 
     case 'chk_dup':{
      $type=$_GET['type'];
      $type = str_replace('-', '_', $type);
      $pri_id=$type.'_id';        

      $chk_fld = $_GET['chk_fld'];
      $chk_val = $_GET['chk_val'];

      $condition[] = array('name'=>$chk_fld,'value'=>$chk_val);

      if(!empty($_GET['chk_fld1']) && !empty($_GET['chk_val1']))
        $condition[] = array('name'=>$_GET['chk_fld1'],'value'=>$_GET['chk_val1']);

      if(!empty($_GET['chk_id']))
        $condition[] = array('name'=>$pri_id.' != ','value'=>$_GET['chk_id']);

      $res = $this->setting_model->get_setting($type,$condition);
               // echo $this->db->last_query();exit();
               //  echo "<pre>";print_r($res);exit();

      if(!empty($res))
       echo "E";
     else
      echo "S";
    break; 
  }
  case "vehicle_type_edit":
  case "material_type_edit":
  {
    $table = str_replace('_edit', '', $action);
    $pri_id=$table.'_id';

    $condition=array();
    $condition[] = array('name'=>$pri_id,'value'=>$_GET['id']);
    $res = $this->setting_model->get_setting($table,$condition);?>
    <input type="hidden" name="<?=$pri_id?>" value="<?= $res[$pri_id] ?>" />
    <div class="row">
      <div class="col-md-12 top15">
        <span class="badge-label">Name</span>
        <input class="form-control name" name="name" value="<?= $res['name'] ?>">
      </div>
      <div class="col-md-12 top15">
        <span class="badge-label p-2">Status</span>
        <div class="switch top10">
          <label>Inactive
            <input type="checkbox" <?= $res['status']=='1'?'checked="true"':''; ?> name="status" value="1" >
            <span class="lever"></span> Active
          </label>
        </div>
      </div>
    </div>
    <?php
    break;
  }
  case "price_edit":{
    $condition=$order=array();
    $condition[] = array('name'=>'price_id','value'=>$_GET['id']);
    $res = $this->setting_model->get_setting('price',$condition);

    $order[] = array('name'=>'price_dtl_id','value'=>'asc'); 
    $dtls = $this->setting_model->get_setting_list('price_dtl',$condition,$order);
    ?>
    <style type="text/css"> 
      .modal-dialog{
        width: 800px;
      }
    </style>
    <input type="hidden" name="price_id" value="<?= $res['price_id'] ?>" />
    <div class="row"> 
      <div class="col-md-6 top15">
        <span class="badge-label">Vehicle Type</span>
        <select class="select form-control vehicle_type_id" name="vehicle_type_id">
          <option value="0" >Select</option>
          <?php  
          $order=array();
          $order[] = array('name'=>'name','value'=>'asc');        
          $opts = $this->setting_model->get_setting('vehicle_type',false,$order);
          if( count($opts) > 0){
            foreach($opts as $opt){ ?>
              <option  <?= $res['vehicle_type_id']==$opt['vehicle_type_id']?' selected="true"':'';?> value="<?= $opt["vehicle_type_id"] ?>" ><?= $opt["name"] ?> </option>
            <?php }  ?>
          <?php }  ?>
        </select> 
      </div>
    </div>
    <br>
    <div id="dtl_cont1"> 
      <?php foreach($dtls as $key=> $dtl){ ?>
       <div class="row">  
        <div class="col-md-3">
         <span class="badge-label">Wt.From<span class="redstar">*</span></span>
         <input class="form-control num-frac-only mb25 fr_wt" value="<?= $dtl['fr_wt'] ?>" name="fr_wt[]">
       </div>    
       <div class="col-md-3">
        <span class="badge-label">Wt.To<span class="redstar">*</span></span>
        <input class="form-control num-frac-only mb25 to_wt" value="<?= $dtl['to_wt'] ?>" name="to_wt[]">
      </div> 
      <div class="col-md-3">
       <span class="badge-label">Price<span class="redstar">*</span></span>
       <input class="form-control num-frac-only mb25 price" value="<?= $dtl['price'] ?>" name="price[]">
     </div>  
     <?php if($key==0) { ?>
       <div class="col-lg-3" style="width:40px;padding-top: 35px;">
        <button type="button" id="btn_plus1" class="btn btn-primary btn-plus">+</button>
      </div>  
    <?php }  ?> 
  </div>  
<?php }  ?> 
</div>



<div class="col-md-12 top15">
  <span class="badge-label p-2">Status</span>
  <div class="switch top10">
    <label>Inactive
      <input type="checkbox" <?= $res['status']=='1'?'checked="true"':''; ?> name="status" value="1" >
      <span class="lever"></span> Active
    </label>
  </div>
</div>
</div>
<script type="text/javascript">
  $('.select').select2();
</script>
<?php
break;
} 

case "sms_edit":
{
  $table = str_replace('_edit', '', $action);
  $pri_id=$table.'_id';

  $condition=array();
  $condition[] = array('name'=>$pri_id,'value'=>$_GET['id']);
  $res = $this->setting_model->get_setting($table,$condition);?>
  <input type="hidden" name="<?=$pri_id?>" value="<?= $res[$pri_id] ?>" />
  <div class="row"> 
    <div class="col-md-12 top15">
      <span class="badge-label">URL<span class="redstar">*</span></span>
      <input class="form-control mb25 url" name="url" value="<?= $res['url'] ?>">
    </div>
    <div class="col-md-12 top15">
      <span class="badge-label">API Key<span class="redstar">*</span></span>
      <input class="form-control mb25 api_key" name="api_key" value="<?= $res['api_key'] ?>">
    </div>
    <div class="col-md-12 top15">
      <span class="badge-label">Client ID<span class="redstar">*</span></span>
      <input class="form-control mb25 client_id" name="client_id" value="<?= $res['client_id'] ?>">
    </div>
    <div class="col-md-12 top15">
      <span class="badge-label">Sender ID<span class="redstar">*</span></span>
      <input class="form-control mb25 sender_id" name="sender_id" value="<?= $res['sender_id'] ?>">
    </div>
    <div class="col-md-12 top15">
      <span class="badge-label p-2">Status</span>
      <div class="switch top10">
        <label>Inactive
          <input type="checkbox" <?= $res['status']=='1'?'checked="true"':''; ?> name="status" value="1" >
          <span class="lever"></span> Active
        </label>
      </div>
    </div>
  </div>
  <?php
  break;
}
case "printer_edit":
{
  $table = str_replace('_edit', '', $action);
  $pri_id=$table.'_id';

  $condition=array();
  $condition[] = array('name'=>$pri_id,'value'=>$_GET['id']);
  $res = $this->setting_model->get_setting($table,$condition);?>
  <input type="hidden" name="<?=$pri_id?>" value="<?= $res[$pri_id] ?>" />
  <div class="row"> 
    <div class="col-md-12 top15">
      <span class="badge-label">IP Address<span class="redstar">*</span></span>
      <input class="form-control mb25 ip_address" name="ip_address" value="<?= $res['ip_address'] ?>">
    </div>
    <div class="col-md-12 top15">
      <span class="badge-label">Port<span class="redstar">*</span></span>
      <input class="form-control mb25 port" name="port" value="<?= $res['port'] ?>">
    </div> 
  </div>
  <?php
  break;
}
case 'get_price':{
  $wt=$_GET['wt']; 
  $vt_id = $_GET['vt_id'];

  $q = "SELECT price FROM wb_price_dtl a left join wb_price b on a.price_id=b.price_id where status='1' and vehicle_type_id=".$vt_id." and fr_wt <=".$wt." and to_wt>=".$wt." and a.deleted='0' order by price_dtl_id desc limit 1";
  $res = $this->db->query($q)->row_array();   

  if(!empty($res))
   echo $res['price'];
 else
  echo "0";
break; 
}

case 'get_veh_entry':{
 $veh_no = $_GET['veh_no'];

 $q = "SELECT vt_id,mt_id,container_no,mobile1,mobile2,load_type,empty_wt,tot_wt,net_wt,net_amt FROM wb_entry where deleted='0' and veh_no='".$veh_no."' order by entry_id desc limit 1";
 $res = $this->db->query($q)->row_array();   

 if($res)
  echo json_encode($res);
else
  echo "-1";

 break; 
}

case "del_setting":{  
  $type=$_GET['type'];

  if($type=='entry')
    return false;

  if($type=='price')
    $type='price_dtl';

  $pri_id=$type.'_id';
  $id = $_GET['id'];

  if ($type !='followup' || $type !='survey' || $type !='survey_test'){
    $msg = $this->setting_model->check_setting($type,$id,$_GET['type_str']);
    if ($msg != '1'){
      echo $msg;
      return false;
    }
  } 

  // if($type=='survey'){
  //   $this->db->where('survey_id',$id);
  //   $this->db->delete('survey_dtl');           
  // }
  // elseif($type=='survey_test'){
  //   $condition = array();
  //   $condition[] = array('name'=>'survey_test_id','value'=>$id);
  //   $select=array('img');
  //   $res = $this->setting_model->get_setting('survey_test',$condition,false,false,$select);
  //   if(!empty($res['img'])){
  //     unlink('./uploads/survey_test/'.$res['img']);
  //   }

  //   $this->db->where('survey_test_id',$id);
  //   $this->db->delete('followup');           
  // }

  $save=array();
  $save['deleted'] = '1';
  $save[$pri_id] =$id; 
  $this->setting_model->save_setting($type,$save);     

  // $this->db->where($type.'.'.$pri_id,$id);
  // $this->db->delete($type);      

  $this->session->set_flashdata("alert_success",$_GET['type_str']." deleted successfully!");      
  echo "S";
  break;
}

} 
}
}

