<?php 
include("../config.php");
if(!empty($_POST)){
    $remark                 = mysqli_real_escape_string($conn, $_POST['remark']);
    $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'];

if($_POST["id"] != ''){
    $type               = 'lr_permit_update';
    $user_id            = $_SESSION['user_detail']['id'];
    $data               = serialize($_POST);

    $config['attachment']['rc_folder_path']     = '../assets/upload/truck_document/';
    $config['attachment']['valid_types']        = array('pdf', 'doc', 'docx',  'png',  'jpg', 'jpeg');
    $config['attachment']['file_size']          = 1000000000000000000000000000; //
    
    if($_POST['old_rc_img'] != ''){
        if($_FILES["rc_img"]["name"] != ''){
            $rc_front_image = '';
            if($_FILES["rc_img"]["name"] != ''){
                $rc_front_file_extension = pathinfo($_FILES["rc_img"]["name"], PATHINFO_EXTENSION);
                if (!in_array($rc_front_file_extension, $config['attachment']['valid_types'])) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                            die();
                }else if ($config['attachment']['file_size'] < $_FILES["rc_img"]["size"]) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                        die();
                }
                $rc_img             = rand(1,99999).'.'.basename($_FILES['rc_img']['name']);
                $rc_front_upload_file_path= $config['attachment']['rc_folder_path'] . $rc_img;
                @move_uploaded_file($_FILES["rc_img"]["tmp_name"], $rc_front_upload_file_path);
                $rc_front_image = $rc_img;
            }
        }else{
            $rc_img  = $_POST['old_rc_img'];    
        }
    }else{
        $rc_front_image = '';
        if($_FILES["rc_img"]["name"] != ''){
            $rc_front_file_extension = pathinfo($_FILES["rc_img"]["name"], PATHINFO_EXTENSION);
            if (!in_array($rc_front_file_extension, $config['attachment']['valid_types'])) {
                echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                        die();
            }else if ($config['attachment']['file_size'] < $_FILES["rc_img"]["size"]) {
                echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                    die();
            }
            $rc_img             = rand(1,99999).'.'.basename($_FILES['rc_img']['name']);
            $rc_front_upload_file_path= $config['attachment']['rc_folder_path'] . $rc_img;
            @move_uploaded_file($_FILES["rc_img"]["tmp_name"], $rc_front_upload_file_path);
            $rc_front_image = $rc_img;
        }
    }

    $insert = mysqli_query($conn, "INSERT INTO `image_master`(`ref_no`, `image`, `remark`, `flag`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$_POST['id']."','".$rc_img."','".$remark."','TRUCK_DOCUMENT','".$_SESSION['branch_id']."','".$_SESSION['company_id']."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
    
    // update
    // $user_query = "UPDATE `lr_mst` SET `per_date`='".$per_date."',`act_rec_date`='".$act_rec_date."',`status`='".$status."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE `lr_no`='".$_POST['id']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    // $user_result = mysqli_query($conn,$user_query) or die( mysqli_error($conn) );
    
    // activity
    $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);

  }

  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=truck_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => BASE_URL . 'index.php?page=truck_mst/permit_add.php') );die();
}  
?>