<script>
  function showMessage(status, client_id){
     if(status == 1){
         $title      = 'Great !!';
         $message    = 'TAX UPDATED SUCCESSFUL';
         $type       = 'success';
     } else {
         $title      = 'Opps...';
         $message    = 'Error in adding tax. Please try again';
         $type       = 'error';
     }

     swal({
         title: $title,
         text: $message,
         type: $type,
         showCancelButton: false,
         confirmButtonColor: "#DD6B55",
         confirmButtonText: "OK"
     },
     function(){
         if(status == 1){
             window.location.href = http_host + '?page=tax_mst/list.php';
         } else {
             window.location.href = http_host + '?page=tax_mst/list.php';    
         }
     });
  }
</script>
<?php 
  $status = '';
  $msg = '';
  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update     = "SELECT * FROM tax_mst WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
  }
  if(isset($_POST['insert'])){
    $sign                 = $_POST['sign'];
    $account_group        = $_POST['account_group'];
    $module               = $_POST['module'];
    $descr                = $_POST['descr'];
    $type                 = 'update_tax';
    $user_id              = $_SESSION['user_detail']['id'];
    $data                 = serialize($_POST);
    $company_id           = $_SESSION['company_id'];
    $branch_id            = $_SESSION['branch_id'];
    $created_datetime     = date('Y-m-d H:i:s');
    $created_id           = $_SESSION['user_detail']['id'];
    $updated_datetime     = date('Y-m-d H:i:s');
    $updated_id           = $_SESSION['user_detail']['id'];
    
    $user_query = "UPDATE tax_mst SET sign='$sign',account_group='$account_group',module='$module',descr='$descr',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_REQUEST['eid']."'";

    $user_result = mysqli_query($conn,$user_query) or die( mysqli_error($conn) );

    $activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    $acttivity_res = mysqli_query($conn,$activiti);
       
    if($user_result){ 
      echo "<script type='text/javascript'> showMessage(1, 1); </script>";
    } else {
      echo "<script type='text/javascript'> showMessage(0, 0); </script>";
    }

    if( !hasPermission($_SESSION['user_detail']['role_id'], 'tax_edit') ) {
      echo '<script>window.location.href="index.php?page=no_permission.php"</script>';
      die();
    }
  }
?>
<section class="content-header hitz-braed-crumb">
   <h1 class="hitz-border">UPDATE TAX</h1>
   <ol class="breadcrumb">
      <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'tax_list') ) { ?>
      <a class="btn btn-sm btn-primary btn-create btn hitz-bread-btn" href="index.php?page=tax_mst/list.php">&nbsp; Tax LIST</a>
      <?php } ?>
   </ol>
</section>
<section class="" style="margin-top: 30px;">
   <div class="row">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
               <form class="" id="signupForm" method="post">
                  <div class="row">
                     <div class="col-md-1">
                        <div class="form-group">
                           <label class="capital">SIGN</label><span style="color: red">*</span>
                           <select class="form-control select2" name="sign" id="sign">
                              <option value="">SIGN</option>
                              <option value="+"<?php if($update_row['sign'] == '+') { ?> selected="selected"<?php } ?>>+</option>
                              <option value="-"<?php if($update_row['sign'] == '-') { ?> selected="selected"<?php } ?>>-</option>
                           </select>
                        </div>
                     </div>
                     <div class="col-md-5">
                        <div class="form-group">
                           <label class="capital">Description</label><span style="color: red">*</span>
                           <textarea name="descr" id="descr" class="form-control"><?php echo $update_row['descr'];?></textarea>
                        </div>
                     </div>
                     <div class="col-md-3">
                        <div class="form-group">
                           <label class="capital">ACCOUNT</label><span style="color: red">*</span>
                           <select class="form-control select2" name="account_group" id="account_group">
                              <option></option>
                              <?php 
                                $ag = "SELECT * FROM `act_master`";
                                $ag_res = mysqli_query($conn,$ag);
                                while ($ag_row=mysqli_fetch_array($ag_res)) { ?>
                                <option  <?php if($ag_row['id'] == $update_row['account_group']) { echo "selected='selected'"; } ?>  value="<?php echo $ag_row['id'] ?>"><?php echo $ag_row['ac_name'];?></option>
                              <?php } ?>
                           </select>
                        </div>
                     </div>
                     <div class="col-md-3">
                        <div class="form-group">
                           <label class="capital">MODULE</label>
                           <select class="form-control select2" name="module" id="module">
                              <option></option>
                              <?php foreach ($module as $modules) { ?>
                              <option  <?php if($modules['title'] == $update_row['module']) { echo "selected='selected'"; } ?>  value="<?php echo $modules['title'] ?>"><?php echo $modules['title'];?></option>

                              <?php } ?>
                           </select>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <p>Mark (*) fields are compulsory</p>
                     </div>
                  </div>
                  <hr>
                  <div class="row" align="center">
                     <button name="insert" class="btn btn-primary submit capital" type="submit"> UPdate & LIST</button>
                     <a href="index.php?page=tax_mst/list.php" class="btn btn-danger capital"> Cancle</a>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('input').iCheck({
       checkboxClass: 'icheckbox_flat',
       radioClass: 'iradio_flat'
     });
    $('#account_group').select2();
    $('#module').select2();
    $('#sign').select2();
  });
</script>