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

?>

<script>
//Javascript
function white_space(field)
{
     field.value = (field.value).replace(/^\\s*/g,'');
}
</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 New 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">
                                    <div class="panel-heading"><h3 class="panel-title"><?php echo $btn_name;?></h3></div>
                                    <div class="panel-body">
                                        <form role="form">
                                            
                                          <!--<div class="form-group">
            <label>User Name *</label> 

                <select id="chkveg" multiple="multiple" class="form-control"   name="user_id[]" required> 
               

              <?php 
              
               
             
              $category = $this->admin_common_model->get_where('users',['type'=>'USER']); 
             
              foreach($category as $category_name){ ?>
                  <option  <?php if($row['user_id'] == $category_name['id']){echo 'selected' ; } ?> value="<?=$category_name['id']?>" ><?=$category_name['first_name'] .' '.$category_name['last_name'];?></option>
                 
              <?php } ?>
              </select>
            
          </div>
                       -->                     
                                            
                                            <div class="form-group">
                                                <label>Code</label>
                                                <input type="text" class="form-control" name="code"  value="<?=$row['code'];?>">
                                            </div>


                                           <div class="form-group">
                                                <label for="exampleInputEmail1">Percentage *</label>
                                                <input type="number" class="form-control" name="percentage" required value="<?=$row['percentage'];?>" >
                                            </div>


                                             <div class="form-group">
                                                <label>End Date</label>
                                                <input type="date" class="form-control" name="end_date"  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'];
                                               ?></textarea>
                                            </div>
                                            
                                            
                                               <div class="form-group">
                                                <label for="exampleInputEmail1">Description (In 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'];
                                               ?></textarea>
                                            </div>
                                            
                                              <div class="form-group col-md-8">
                                                  
                                                  <div class="image-view">
                                                    <img src="<?=$path;?>" name="image" id="image">    
                                                    </div>
                                                <label>Offer Image</label>

                                               <input type="file" name="image" onchange="readURL(this,'image');" class="form-control">
                                            </div> 
                                        </div>

                                    </div><!-- panel-body -->
                                </div> <!-- panel -->
                            </div> <!-- col-->
                            
<!--                            <div class="col-md-6">
                            <div class="image-view">
                            <img src="<?=$path;?>" name="image" id="image">    
                            </div>    
                            </div>
-->                           

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

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

                <footer class="footer text-right">
                    2020 © Edamah.
                </footer>

            </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]);
        }
    }

</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 = "OFFER_IMG" . $n .'.'.$path_info['extension'];
                        move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $img);
                        $arr_data['image'] = $img; 

            }

          //print_r($arr_data);die;

unset($arr_data['submit'],$arr_data['id']);
$result = $this->admin_common_model->insert_data('coupons',$arr_data); 
//echo $this->db->last_query(); die;
             
        
if ($result) {
    
    
    
    
   
              
              
echo 
"<script> swal(
  'Success',
  'Add Offer Successfully',
  'success'

); 

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

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

</script>";

    }else{

echo "<script> swal(
  'Error',
  'Error In Add 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 = "OFFER_IMG" . $n .'.'.$path_info['extension'];
                        move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $img);
                        $arr_data['image'] = $img; 

            }


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

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

</script>";

    }else{

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

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

</script>";

}// end if result




}


?>

<style>
     .dialog {
    padding-right: 18px;
    float: left;
    position: relative;
}
.close-thik{
   background: #FFF none repeat scroll 0% 0%;
color: #0A0A0A;
font: 14px/100% arial,sans-serif;
position: absolute;
right: 20px;
text-decoration: none;
text-shadow: 0px 1px 0px #FFF;
top: 5px; 
}
.dialog label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}
.upload_file {
    width: 120px;
    height: 100px;
    border: 1px solid #0070BC;
}
.dialog input{
        visibility: hidden;
    position: absolute;

}
.form-group input[type="checkbox"] {
    display: block;
}
.multiselect.dropdown-toggle.btn.btn-default {
    width: 715px;
    background-color: 
#fff;
box-shadow: none;
border-color:
    #ccc;
}
ul.multiselect-container.dropdown-menu {
    overflow-x: hidden;
    height: 125px;
    overflow-y: scroll;
}
     </style>

<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css"
        rel="stylesheet" type="text/css" />
    <script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"
        type="text/javascript"></script>
<script>
    $(function() {

        $('#chkveg').multiselect({
        
            includeSelectAllOption: true
        
        });
    });
</script> 


