<?php include 'include/header.php';?>

    <body class="fixed-left">
        
        <!-- Begin page -->
        <div id="wrapper">
        
            <?php include 'include/side_menu.php';

  $button = "submit";
      $btn_name = "Add Service";
      $path1 = base_url("assets/images/banner/user_b.png");
      $path2 = base_url("assets/images/banner/user_b.png");
      $path3 = base_url("assets/images/banner/user_b.png");
      $id = $this->uri->segment(4);
      if($id!=''){
        $fetch = $this->admin_common_model->get_where('services',['id'=>$id]);
        $row = $fetch[0];
        $button = "update";
        $btn_name = "Update Service";        
        if($row['image1']!=''){
          $path1 = base_url("uploads/images/".$row['image1']);
        }
        
         if($row['image2']!=''){
          $path2 = base_url("uploads/images/".$row['image2']);
        }
        
         if($row['image3']!=''){
          $path3 = base_url("uploads/images/".$row['image3']);
        }
      }

      
      
      
           

?>
    
    
      <script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.5.6/tinymce.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.5.6/jquery.tinymce.min.js"></script>

    
            <div class="content-page">
                <!-- Start content -->
                <div class="content">
                    <div class="container">

                        <!-- Page-Title -->
                        <div class="row">
                            <div class="col-sm-12">
                                <h4 class="pull-left page-title">Welcome</h4>
                                <ol class="breadcrumb pull-right">
  <li><a href="<?=base_url('admin/view_page/dashboard');?>"><?php $fetch_app_name = $this->admin_common_model->get_where('admin',['id'=>'1']);
                                echo $fetch_app_name[0]['app_name']; ?></a></li>    
                                <li class="active">Add Service</li>
                                </ol>
                            </div>
                        </div>

                        <!-- Start Widget -->
                
<div class="row">
<form method="POST" action="" enctype="multipart/form-data">
          <input type="hidden"  class="form-control" name="id" value="<?=$row['id'];?>">

                            <!-- Basic example -->
                            <div class="col-md-12">
                                <div class="panel panel-default">
 <?php if($row['id']){?>
                                    <div class="panel-heading"><h3 class="panel-title">Update Service</h3></div>
                                    <?php }else{?>
                                    <div class="panel-heading"><h3 class="panel-title">Add Service</h3></div>
                                    <?php }?>
                                                                       <div class="panel-body">
                                        <form role="form">
                                            
                                          
                                          
                                           
          
                                    <div class="form-group">
                                                <label>Country Name  *</label>
               <select class="form-control" name="country_id" onchange="getSubCategory(this.value);"> 
                                 <option value="">--select--</option>

              <?php 
               $category = $this->admin_common_model->get_all('country_map');
               
              foreach($category as $arr){ ?>
<!--                 <option value="<?=$arr['id'];?>"> <?=$arr['name'];?> </option>
-->                 
                 <option <?php if($row['country_id'] == $arr['id']){echo 'selected' ; } ?> value="<?=$arr['id']?>" ><?=$arr['name'];?></option>

              <?php } ?>
              </select>
                                            </div>
          
                                            <div class="form-group">
                                                <label>City Name *</label>
  <select class="form-control" name="city_id"> 
  

              <?php 
              
              if($row['city_id']){
                 $city_id = $row['city_id'];
              }
              else{
                  $city_id = '';
              }
              
              $fetchSubCategories = $this->admin_common_model->get_where('country_map_city',['id'=>$city_id]);
                    $subCategoriesRows = $fetchSubCategories[0];?> 
          <option value="<?php if($row['city_id']){$row['city_id'];}else{echo $subCategoriesRows['id'];} ?>"><?=$subCategoriesRows['name']?$subCategoriesRows['name']:'Select City';?></option>
       
              </select>
              </div>
                                              
                                          
                                          
                                           <div class="form-group">
                                                <label>Company Name *</label>
                                                <input type="text" class="form-control" name="company_name" required value="<?=$row['company_name'];?>">
                                            </div>
                                         
                                              <div class="form-group">
                                                <label>Company Name(in Arabic) *</label>
                                                <input type="text" class="form-control" name="company_name_ar" required value="<?=$row['company_name_ar'];?>">
                                            </div>
                                             
                                              
                                              
                                               <div class="form-group">
                                                <label>Offer Code *</label>
                                                <input type="text" class="form-control" name="discount_code" required value="<?=$row['discount_code'];?>">
                                            </div>
                                             
                                             
                                              <div class="form-group">
                                                <label>Discount Percentage (%)*</label>
                                                <input type="text" class="form-control" name="discount_percentage" required value="<?=$row['discount_percentage'];?>">
                                            </div>
                                             
                                               
                                               <div class="form-group">
                                                <label>Address *</label>
                                                <input type="text" class="form-control" name="address" required value="<?=$row['address'];?>">
                                            </div>
                                             
                                             
                                              <div class="form-group">
                                                <label>Mobile Number*</label>
                                                <input type="text" class="form-control" name="mobile" required value="<?=$row['mobile'];?>">
                                            </div>
                                               
                                               
                                               
                                                 <div class="form-group">
                                                <label>Start Date*</label>
                                                <input type="date" class="form-control" name="start_date" required value="<?=$row['start_date'];?>">
                                            </div>
                                               
                                               
                                                 <div class="form-group">
                                                <label>End Date*</label>
                                                <input type="date" class="form-control" name="end_date" required value="<?=$row['end_date'];?>">
                                            </div>
                                               
                                               
                                               
                                           <div class="form-group">
                                                <label for="exampleInputEmail1">Description *</label>

                                            <textarea class="form-control" required style="width:550px; height:150px;" cols="42" rows="5" name="description"><?php
  if($row['description']){ echo $row['description'];}else{ echo 'NA';};
?></textarea>
                                            </div>
                                            
                                            
                                               
                                           <div class="form-group">
                                                <label for="exampleInputEmail1">Description Arabic *</label>

                                            <textarea class="form-control" required style="width:550px; height:150px;" cols="42" rows="5" name="description_ar"><?php
  if($row['description_ar']){ echo $row['description_ar'];}else{echo 'NA';};
?></textarea>
                                            </div>
                                            
                                           
                                 
                                            
                                           
                                        <div class="row clearfix">
                                               <div class="col-md-4">
                                                <div class="image-view">
                                                <img src="<?=$path1;?>" name="image1" id="image1">    
                                                </div>    
                                                </div>
                                                </div>
                                              

 <div class="row clearfix">
                                              <div class="form-group col-md-6">
                                                <label>Image1</label>

                                               <input type="file" name="image1" onchange="readURL(this,'image1');" class="form-control">
                                            </div>
                                              </div>
                                           
                                           
                                           
                                        <div class="row clearfix">
                                               <div class="col-md-4">
                                                <div class="image-view">
                                                <img src="<?=$path2;?>" name="image2" id="image2">    
                                                </div>    
                                                </div>
                                                </div>
                                               
 
 <div class="row clearfix">
                                              <div class="form-group col-md-6">
                                                <label>Image2</label>

                                               <input type="file" name="image2" onchange="readURL(this,'image2');" class="form-control">
                                            </div>
                                             </div>
                                           
                                           
                                           
                                           
                                        <div class="row clearfix">
                                               <div class="col-md-4">
                                                <div class="image-view">
                                                <img src="<?=$path3;?>" name="image3" id="image3">    
                                                </div>    
                                                </div>
                                                </div> 

 <div class="row clearfix">
                                              <div class="form-group col-md-6">
                                                <label>Image3</label>

                                               <input type="file" name="image3" onchange="readURL(this,'image3');" class="form-control">
                                            </div>
                                             </div>
                                           
                                         
                                            
                                           
                                    </div><!-- panel-body -->
                                </div> <!-- panel -->
                            </div> <!-- col-->
                            

                        </div> <!-- End row -->

 <button type="submit" name="<?=$button;?>" class="btn btn-purple waves-effect waves-light">Save</button>
                                        </form>
                    </div> <!-- container -->
                               
                </div> <!-- content -->

               
            </div>
          
        </div>
        <!-- END wrapper -->


 <?php include 'include/footer.php';?>
<script>

   function readURL(input,id) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#'+id)
                    .attr('src', e.target.result);
                    
            };

            reader.readAsDataURL(input.files[0]);
        }
    }


function getSubCategory(country_id){
 $.ajax({
          url: "<?=base_url('admin/get_sub_category');?>",
          data: {'country_id': country_id}, // change this to send js object
          type: "POST",
          success: function(result){
            //alert(result);
            $("select[name='city_id']").html(result);
          }
        });
}

</script>



<?php

extract($_REQUEST);
// for add holidays
if(isset($submit)){

            $arr_data = $this->input->post();
            
    if($_FILES['image1']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info1 = pathinfo($_FILES['image1']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info1['extension'];
               move_uploaded_file($_FILES['image1']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image1'] = $img;        
      } 
      
      
        if($_FILES['image2']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info2 = pathinfo($_FILES['image2']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info2['extension'];
               move_uploaded_file($_FILES['image2']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image2'] = $img;        
      } 
      
      
        if($_FILES['image3']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info3 = pathinfo($_FILES['image3']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info3['extension'];
               move_uploaded_file($_FILES['image3']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image3'] = $img;        
      } 
          //print_r($arr_data);die;
date_default_timezone_set('Asia/Calcutta'); 

$arr_data['date_time']=date("Y-m-d H:i:s");
unset($arr_data['submit'],$arr_data['id']);
$result = $this->admin_common_model->insert_data('services',$arr_data); 
//echo $this->db->last_query(); die;
             
        
if ($result) {
echo 
"<script> swal(
  'Success',
  'Add Service Successfully',
  'success'

); 

$('.confirm').click(function(){

        window.location='".base_url('admin/view_page/service_list')."';
});

</script>";

    }else{

echo "<script> swal(
  'Error',
  'Error In Add Service',
  'error'
); 

$('.confirm').click(function(){
        window.location='';
});

</script>";

}

}// end if submit


// for update restaurant
if(isset($update)){

$arr_data = $this->input->post();

   if($_FILES['image1']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info1 = pathinfo($_FILES['image1']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info1['extension'];
               move_uploaded_file($_FILES['image1']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image1'] = $img;        
      } 
      
      
        if($_FILES['image2']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info2 = pathinfo($_FILES['image2']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info2['extension'];
               move_uploaded_file($_FILES['image2']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image2'] = $img;        
      } 
      
      
        if($_FILES['image3']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info3 = pathinfo($_FILES['image3']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info3['extension'];
               move_uploaded_file($_FILES['image3']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image3'] = $img;        
      } 
        $city_id = $arr_data['city_id'];

      if($city_id ==''){
          unset($arr_data['city_id']);
      }
      
      
      
date_default_timezone_set('Asia/Calcutta'); 

$arr_data['date_time']=date("Y-m-d H:i:s");

$arr_where = ['id'=>$arr_data['id']];
unset($arr_data['update']);
$result = $this->admin_common_model->update_data('services',$arr_data, $arr_where); 
//echo $this->db->last_query(); die;
              
        
if ($result) {
echo 
"<script> swal(
  'Success',
  'Update Service Successfully',
  'success'
); 

$('.confirm').click(function(){
        window.location='".base_url('admin/view_page/service_list')."';
});

</script>";

    }else{

echo "<script> swal(
  'Error',
  'Error In Updating Service',
  'error'
); 

$('.confirm').click(function(){
        window.location='';
});

</script>";

}// end if result




}


?>



<script>

tinymce.init({
    selector: 'textarea',
    height: 200,
    theme: 'modern',
    plugins: [
        'advlist autolink lists link image charmap print preview hr anchor pagebreak',
        'searchreplace wordcount visualblocks visualchars code fullscreen',
        'insertdatetime media nonbreaking save table contextmenu directionality',
        'emoticons template paste textcolor colorpicker textpattern imagetools codesample toc'
    ],
    toolbar1: 'undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
    toolbar2: 'print preview media | forecolor backcolor emoticons | codesample',
    image_advtab: true
});

// 
// CKEDITOR.replace('editor1', {});
// CKEDITOR.replace('editor2', {});
// CKEDITOR.replace('description', {});
</script>
