<?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 Company Offer";
      $path = base_url("assets/images/banner/user_b.png");
      $id = $this->uri->segment(4);
      if($id!=''){
        $fetch = $this->admin_common_model->get_where('company_offer',['id'=>$id]);
        $row = $fetch[0];
        $button = "update";
        $btn_name = "Update Company Offer";        
        if($row['image']!=''){
          $path = base_url("uploads/images/".$row['image']);
        }
      }

      
      
      
           

?>
    
    
    
      <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 Company Offer</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 Company Offer</h3></div>
                                    <?php }else{?>
                                    <div class="panel-heading"><h3 class="panel-title">Add Company Offer</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>Offer Category Type *</label>
                                                 <?php $category = $this->admin_common_model->get_all('company_offer_category');  ?>
              <select name = "category_id" class="form-control" required>
                  <option value="">--select--</option>
                <?php foreach($category as $category_name){?>
                  <option <?php if($row['category_id'] == $category_name['id']){echo 'selected' ; } ?> value="<?=$category_name['id']?>" ><?=$category_name['category_name'];?></option>
                <?php } ?>
              </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>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="<?=$path;?>" name="image" id="image">    
                                                </div>    
                                                </div>
                                                </div>

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

                                               <input type="file" name="image" onchange="readURL(this,'image');" class="form-control">
                                            </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['image']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info = pathinfo($_FILES['image']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info['extension'];;
               move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image'] = $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('company_offer',$arr_data); 
//echo $this->db->last_query(); die;
             
        
if ($result) {
echo 
"<script> swal(
  'Success',
  'Add Company Offer Successfully',
  'success'

); 

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

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

</script>";

    }else{

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

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

</script>";

}

}// end if submit


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

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

  if($_FILES['image']['name']!=''){
      
               $n = rand(0, 100000);
               $path_info = pathinfo($_FILES['image']['name']);
               $img = "Company_offer_" . $n .'.'.$path_info['extension'];;
               move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image'] = $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('company_offer',$arr_data, $arr_where); 
//echo $this->db->last_query(); die;
             
        
if ($result) {
echo 
"<script> swal(
  'Success',
  'Update Company Offer Successfully',
  'success'
); 

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

</script>";

    }else{

echo "<script> swal(
  'Error',
  'Error In Updating Company Offer',
  '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>

