<?php

// FILE FOR AUTHENTICATION
/*
All possible functions
- Dashboard
- Manage Content
- contact us
- broadcast
- chat report
*/

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $action = $_GET['action'];

    include('config.php');
    include('common_functions.php');
    include('common_functions_app.php');

    switch ($action) {

        case 'get_contact': {
                getUserContact($_GET);
                break;
            }
        default:
            $record = array('success' => false, 'msg' => 'Invalid Action', 'data' => array());
            jsonSendEncode($record);
    }
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $action = $_POST['action'];

    include('config.php');
    include('common_functions.php');
    include('common_functions_app.php');

    switch ($action) {
        case 'edit_content': {
                updateContent($_POST);
                break;
            }
        case 'add_broadcast': {
                AddBroadcast($_POST);
                break;
            }

        case 'send_mail': {
                sendMail($_POST);
                break;
            }
        
       
        case 'send_reply': {
                sendReply($_POST);
                break;
            }
        default:
            include_once('language_message.php');
            $record = array('success' => false, 'msg' => $msgValidAction, 'data' => array());
            jsonSendEncode($record);
    }
}
function updateContent($data) {
    global $mysqli;
    include('language_message.php');
    // echo"data";print_r($data);die;
    if(empty($data['content_id']) && empty($data['content'])) {
        $record=array('success'=>false, 'msg'=>$msgEmptyAll, 'data'=>array());
        jsonSendEncode($record);
    } 
    else {
         // $role            = $data['role']; //0=user , 1=driver
         // $language_select = $data['language_select']; //0=english , 1= arabic
         $content_id      = $data['content_id'];
         $content         = $data['content'];
         $arabic_content  = $data['content_1'];
        
         $updatetime      = date('Y-m-d H:i:s');
    
        $update_content_all=$mysqli->prepare("UPDATE content_master SET content=?,content_1=?, updatetime=? WHERE content_id=?");
        $update_content_all->bind_param("sssi",$content,$arabic_content,$updatetime, $content_id);
        $update_content_all->execute();
        //echo 'error='.mysqli_error($mysqli);
        $update_content=$mysqli->affected_rows;
        if($update_content<=0){
            $record=array('success'=>false,'msg' =>$msgContentNotUpdate, 'data'=>array());
            jsonSendEncode($record);
        }
    }
    $record=array('success'=>true,'msg' =>$msgContentSuccessUpdate, 'data'=>array());
    jsonSendEncode($record);
 }
function AddBroadcast($data)
{

    //error_reporting(0);
    global $mysqli;
    include("1_signal_final.php");
    include('language_message.php');
    include_once('common_functions.php');
    include_once('common_functions_app.php');
     //echo "data";print_r($data);die;
    if (empty($data['title']) || empty($data['message'])) {
        $record = array('success' => false, 'msg' => $msgEmptyAll, 'data' => array());
        jsonSendEncode($record);
    } else {
        $select_broadcast = $data['broadcast'];
        $title            = $data['title'];
        $message          = $data['message'];
        if ($select_broadcast != "") {
            if ($select_broadcast == 'all') {
                $select_all_user_id = $mysqli->prepare("SELECT user_id FROM user_master  WHERE user_type =1 AND delete_flag='0' and profile_complete=1");
                $select_all_user_id->execute();
                $select_all_user_id->store_result();
                $select_all_user_id_num_row = $select_all_user_id->num_rows;
                // echo $select_all_user_id_num_row;die;
                if ($select_all_user_id_num_row <= 0) {
                    $record = array('success' => false, 'msg' => $msgUserNotExist, 'data' => array());
                    jsonSendEncode($record);
                } else {
                    $notification_arr = array();
                    $user_playerIdArr = array();
                    $select_all_user_id->bind_result($all_user_id);
                    while ($select_all_user_id->fetch()) {
                        $user_id_notification = 1;
                        $other_user_id_notification = $all_user_id;
                        $action_id = $all_user_id;

                        $action = 'broadcast';
                        //$action_id = $product_name;
                        $title   =  $title;
                        $title_2 =  $title;
                        $title_3 =  $title;
                        $title_4 =  $title;
                        $message   = $message;
                        $message_2 = $message;
                        $message_3 = $message;
                        $message_4 = $message;
                        $action_data = array('user_id' => $user_id_notification, 'other_user_id' => $other_user_id_notification, 'action_id' => $action_id, 'action' => $action);
                        // $action_json=json_encode($action_data);
                        //set aray
                        // print_r($action_data);die;
                        $notification_arr_check = getNotificationArrSingle($user_id_notification, $other_user_id_notification, $action, $action_id, $title, $title_2, $title_3, $title_4, $message, $message_2, $message_3, $message_4, $action_data);
                        if ($notification_arr_check != 'NA') {
                            $notification_arr[] = $notification_arr_check;
                        }
                        // print_r($notification_arr_check);die;
                        //-------------------------- Notification array end -----------------------
                    }
                    $record = array('success' => true, 'msg' => $msgBroadcastAddedSuccess, 'data' => array('notification_arr' => $notification_arr));
                    jsonSendEncode($record);
                    if (empty($notification_arr)) {
                        $notification_arr = 'NA';
                    }
                }
            } else
            
       if ($select_broadcast == 'all_store') {
                $select_all_user_id = $mysqli->prepare("SELECT user_id FROM user_master  WHERE user_type =2 AND delete_flag='0' and profile_complete=1");
                $select_all_user_id->execute();
                $select_all_user_id->store_result();
                $select_all_user_id_num_row = $select_all_user_id->num_rows;
                if ($select_all_user_id_num_row <= 0) {
                    $record = array('success' => false, 'msg' => $msgUserNotExist, 'data' => array());
                    jsonSendEncode($record);
                } else {
                    $notification_arr = array();
                    $user_playerIdArr = array();
                    $select_all_user_id->bind_result($all_user_id);
                    while ($select_all_user_id->fetch()) {
                        $user_id_notification = 1;
                        $other_user_id_notification = $all_user_id;
                        $action_id = $all_user_id;

                        $action = 'broadcast';
                        //$action_id = $product_name;
                        $title   =  $title;
                        $title_2 =  $title;
                        $title_3 =  $title;
                        $title_4 =  $title;
                        $message   = $message;
                        $message_2 = $message;
                        $message_3 = $message;
                        $message_4 = $message;
                        $action_data = array('user_id' => $user_id_notification, 'other_user_id' => $other_user_id_notification, 'action_id' => $action_id, 'action' => $action);
                        // $action_json=json_encode($action_data);
                        //set aray
                        // print_r($action_data);die;
                        $notification_arr_check = getNotificationArrSingle($user_id_notification, $other_user_id_notification, $action, $action_id, $title, $title_2, $title_3, $title_4, $message, $message_2, $message_3, $message_4, $action_data);
                        if ($notification_arr_check != 'NA') {
                            $notification_arr[] = $notification_arr_check;
                        }
                        // print_r($notification_arr);
                        //-------------------------- Notification array end -----------------------
                    }
                    $record = array('success' => true, 'msg' => $msgBroadcastAddedSuccess, 'data' => array('notification_arr' => $notification_arr));
                    jsonSendEncode($record);
                    if (empty($notification_arr)) {
                        $notification_arr = 'NA';
                    }
                }
            } else
         if ($select_broadcast == 'selected_user') {
                $user_arr = $data['select_user'];
                $user_playerIdArr = array();
                $notification_arr = array();
                if (count($user_arr) > 0) {
                    $admin_send = 1; // sender 
                    $action     =   'broadcast'; // 
                    $title      =   $title; //English
                    $title_2    =   $title;
                    $title_3    =   $title;
                    $title_4    =   $title;
                    $message    =   $message;
                    $message_2  =   $message;
                    $message_3  =   $message;
                    $message_4  =   $message;

                    for ($i = 0; $i < count($user_arr); $i++) {
                        $other_user_id = $user_arr[$i];
                        $action_id = $user_arr[$i];
                        $action_data = array('user_id' => $admin_send, 'other_user_id' => $other_user_id, 'action_id' => $action_id, 'action' => $action);
                        $notification_arr_get = getNotificationArrSingle($admin_send, $other_user_id, $action, $action_id, $title, $title_2, $title_3, $title_4, $message, $message_2, $message_3, $message_4, $action_data);
                        if ($notification_arr_get != 'NA') {
                            $notification_arr[] = $notification_arr_get;
                        }
                    }
                    if (empty($notification_arr)) {
                        $notification_arr = 'NA';
                    }
                    $record = array('success' => true, 'msg' => $msgBroadcastAddedSuccess, 'data' => array('notification_arr' => $notification_arr));
                    jsonSendEncode($record);
                }
            } else
    
      if ($select_broadcast == 'selected_store') {
                $parking_arr = $data['select_store'];
                // print_r($parking_arr);die;
                $user_playerIdArr = array();
                $notification_arr = array();
                if (count($parking_arr) > 0) {
                    for ($i = 0; $i < count($parking_arr); $i++) {
                        $user_id = 1;
                        $other_user_id = $parking_arr[$i];
                        $action = 'broadcast';
                        $action_id = $parking_arr[$i];
                        $title   =  $title;
                        $title_2 =  $title;
                        $title_3 =  $title;
                        $title_4 =  $title;
                        $message   = $message;
                        $message_2 = $message;
                        $message_3 = $message;
                        $message_4 = $message;
                        $action_data = array('user_id' => $user_id, 'other_user_id' => $other_user_id, 'action_id' => $action_id, 'action' => $action);
                        // $action_json=json_encode($action_data);
                        //set aray
                        //print_r($action_data);
                        $notification_arr_check = getNotificationArrSingle($user_id, $other_user_id, $action, $action_id, $title, $title_2, $title_3, $title_4, $message, $message_2, $message_3, $message_4, $action_data);
                        if ($notification_arr_check != 'NA') {
                            $notification_arr[] = $notification_arr_check;
                        }
                        //-------------------------- Notification array end -----------------------
                    }
                    if (empty($notification_arr)) {
                        $notification_arr = 'NA';
                    }
                    $record = array('success' => true, 'msg' => $msgBroadcastAddedSuccess, 'data' => array('notification_arr' => $notification_arr));
                    jsonSendEncode($record);
                }
            } else  if ($select_broadcast == 'all_driver') {
                $select_all_user_id = $mysqli->prepare("SELECT user_id FROM user_master  WHERE user_type =2 AND delete_flag='0' and profile_complete=1");
                $select_all_user_id->execute();
                $select_all_user_id->store_result();
                $select_all_user_id_num_row = $select_all_user_id->num_rows;
                if ($select_all_user_id_num_row <= 0) {
                    $record = array('success' => false, 'msg' => $msgUserNotExist, 'data' => array());
                    jsonSendEncode($record);
                } else {
                    $notification_arr = array();
                    $user_playerIdArr = array();
                    $select_all_user_id->bind_result($all_user_id);
                    while ($select_all_user_id->fetch()) {
                        $user_id_notification = 1;
                        $other_user_id_notification = $all_user_id;
                        $action_id = $all_user_id;

                        $action = 'broadcast';
                        //$action_id = $product_name;
                        $title   =  $title;
                        $title_2 =  $title;
                        $title_3 =  $title;
                        $title_4 =  $title;
                        $message   = $message;
                        $message_2 = $message;
                        $message_3 = $message;
                        $message_4 = $message;
                        $action_data = array('user_id' => $user_id_notification, 'other_user_id' => $other_user_id_notification, 'action_id' => $action_id, 'action' => $action);
                        // $action_json=json_encode($action_data);
                        //set aray
                        // print_r($action_data);die;
                        $notification_arr_check = getNotificationArrSingle($user_id_notification, $other_user_id_notification, $action, $action_id, $title, $title_2, $title_3, $title_4, $message, $message_2, $message_3, $message_4, $action_data);
                        if ($notification_arr_check != 'NA') {
                            $notification_arr[] = $notification_arr_check;
                        }
                        // print_r($notification_arr);
                        //-------------------------- Notification array end -----------------------
                    }
                    $record = array('success' => true, 'msg' => $msgBroadcastAddedSuccess, 'data' => array('notification_arr' => $notification_arr));
                    jsonSendEncode($record);
                    if (empty($notification_arr)) {
                        $notification_arr = 'NA';
                    }
                }
            } else
    
      if ($select_broadcast == 'selected_driver') {
                $parking_arr = $data['select_driver'];
                // print_r($parking_arr);die;
                $user_playerIdArr = array();
                $notification_arr = array();
                if (count($parking_arr) > 0) {
                    for ($i = 0; $i < count($parking_arr); $i++) {
                        $user_id = 1;
                        $other_user_id = $parking_arr[$i];
                        $action = 'broadcast';
                        $action_id = $parking_arr[$i];
                        $title   =  $title;
                        $title_2 =  $title;
                        $title_3 =  $title;
                        $title_4 =  $title;
                        $message   = $message;
                        $message_2 = $message;
                        $message_3 = $message;
                        $message_4 = $message;
                        $action_data = array('user_id' => $user_id, 'other_user_id' => $other_user_id, 'action_id' => $action_id, 'action' => $action);
                        // $action_json=json_encode($action_data);
                        //set aray
                        //print_r($action_data);
                        $notification_arr_check = getNotificationArrSingle($user_id, $other_user_id, $action, $action_id, $title, $title_2, $title_3, $title_4, $message, $message_2, $message_3, $message_4, $action_data);
                        if ($notification_arr_check != 'NA') {
                            $notification_arr[] = $notification_arr_check;
                        }
                        //-------------------------- Notification array end -----------------------
                    }
                    if (empty($notification_arr)) {
                        $notification_arr = 'NA';
                    }
                    $record = array('success' => true, 'msg' => $msgBroadcastAddedSuccess, 'data' => array('notification_arr' => $notification_arr));
                    jsonSendEncode($record);
                }
            } else {
                $record = array('success' => false, 'msg' => $msgBroadcastAddederror, 'data' => array());
                jsonSendEncode($record);
            }
        }
    }
}


function sendReply($data){
    //error_reporting(0);
    global $mysqli;
    //print_r($data);exit;
    include_once('language_message.php');
    include_once('mailFunctions.php');
    include_once('site_config.php');
    if( empty($data['subject']) || empty($data['message']) || empty($data['contact_id'])) {
        $record=array('success'=>false, 'msg'=>$msgEmptyAll, 'data'=>array()); 
        jsonSendEncode($record);  
    } else {
        $subject1       = $data['subject'];
        $message        = $data['message'];
        $contact_id     = decode($data['contact_id']);
        $name       = $data['name'];
        $check_data=$mysqli->prepare("SELECT contact_id,user_id,name,createtime,email from contact_us_master where delete_flag='0' and contact_id=?");
        $check_data->bind_param('i',$contact_id);
        $check_data->execute();
        $check_data->store_result();
        $check_data_count=$check_data->num_rows;

        if($check_data_count >0) {
            $check_data->bind_result($contact_id,$user_id_get,$name,$createtimem,$email);
            $check_data->fetch();
        }
        
        $postData['name']               =   $name;
        $postData['email']              =   $email;
        //$postData['subject']          =   $subject;
        $postData['message']            =   $message;
        $postData['fromName']           =   $appName;
        $postData['app_name']           =   $appName;
        $postData['mail_title']         ='Contact Us Reply';
        $postData['app_logo']           =   $appLogo;
        //$postData['app_bg']           =   app_bg; 
        $subject                        =$subject1;
        $mailbody                       =   mailBodyContactUs($postData);
        $mailResponse                   =   mailSend($postData['email'], $postData['fromName'], $subject, $mailbody);

                
        if($mailResponse == 'yes'){

            $updatetime = date('Y-m-d H:i:s');

            $query_update_status=$mysqli->prepare("UPDATE `contact_us_master` SET status=1, replied_date_time=? WHERE contact_id=?");
            $query_update_status->bind_param("si",$updatetime,$contact_id);
            $query_update_status->execute();
            $query_update_status->store_result();
            $reply_status_count=$query_update_status->affected_rows;  //0 
            $record=array('success'=>true, 'msg'=>$ReplySendSuccess, 'data'=>array()); 
            jsonSendEncode($record);  
        }else{
            $record=array('success'=>false, 'msg'=>$ReplySendError, 'data'=>array()); 
            jsonSendEncode($record);  
        }
    }
}

//-----------------------------show tabular post reports-------------------------------//
function ShowTabularPostReport($data)
{
    global $mysqli;
    global $onerror;
    include_once('language_message.php');

    // print_r($data);
    if (empty($data['s_date']) || empty($data['e_date'])) {
        $record = array('success' => false, 'msg' => $msgEmptyAll, 'data' => array());
        jsonSendEncode($record);
    } else {
        $srno = 1;
        $s_date = $data['s_date'];
        $e_date = $data['e_date'] . ' 23:59:59';

        $start_date = date('Y-m-d 01:00:00', strtotime($s_date));
        $end_date = date('Y-m-d 23:59:59', strtotime($e_date));

        // echo $end_date;
        // echo $start_date;
        // exit(); 

        $check_user_all = $mysqli->prepare("SELECT post_id,user_id,image,address,category_id,discription,createtime FROM post_master WHERE delete_flag=0   AND createtime BETWEEN ? AND ? order by post_id desc");
        $check_user_all->bind_param('ss', $start_date, $end_date);
        $check_user_all->execute();
        $check_user_all->store_result();
        $check_user = $check_user_all->num_rows;
        if ($check_user > 0) {

            $check_user_all->bind_result($post_id, $user_id, $image, $address, $category_id, $description, $createtime);
            while ($check_user_all->fetch()) {
                $image_html = '<img  width="50px" src="../webservice/images/' . $image . '" class="rounded-circle circle-border m-b-md img-fix img-responsive" name="profile_image" id="previewholder" style="width: 70px; height: 70px;" alt="" onerror="this.src=' . $onerror . ';">';


                $createtime = date('Y-m-d h:i A', strtotime($createtime));



                $post_arr[] = array(
                    'srno' => $srno,
                    'post_id' => $post_id,
                    'username' => getUserName($user_id),
                    'image' => $image_html,
                    'address' => $address,
                    'categoryname' => getCatgoryName($category_id),
                    'description' => $description,

                    'createtime' => $createtime
                );
                $srno++;
            }
        }
        $record = array('success' => true, 'msg' => $msgDataFound, 'data' => array('post_arr' => $post_arr));
        jsonSendEncode($record);
    }
}


//---------------------send mail to vendor----------------------------------//


function sendMail($data)
{
    error_reporting(0);
    global $mysqli;
    include("1_signal_final.php");
    include_once('language_message.php');
    include_once('mailFunctions.php');
    include_once('site_config.php');
    include_once('common_functions.php');
    include_once('common_functions_app.php');
    if (empty($data['title']) || empty($data['message'])) {
        $record = array('success' => false, 'msg' => $msgEmptyAll, 'data' => array());
        jsonSendEncode($record);
    } else {
        $select_sendmail = $data['sendmail'];
        $subject1      = $data['title'];
        $message        = $data['message'];
        if ($select_sendmail != "") {
            if ($select_sendmail == 'all') {
                $select_all_user_id = $mysqli->prepare("SELECT user_id,name,email FROM user_master  WHERE user_type=2 AND delete_flag='0' and profile_complete=1");
                $select_all_user_id->execute();
                $select_all_user_id->store_result();
                $select_all_user_id_num_row = $select_all_user_id->num_rows;
                if ($select_all_user_id_num_row <= 0) {
                    $record = array('success' => false, 'msg' => $msgUserNotExist, 'data' => array());
                    jsonSendEncode($record);
                } else {

                    $select_all_user_id->bind_result($user_id, $name, $all_email);
                    while ($select_all_user_id->fetch()) {

                        $postData['name']               =   $name;
                        $postData['email']              =   $all_email;
                        $postData['subject']            =   $subject1;
                        $postData['message']            =   $message;
                        $postData['fromName']           =   $appName;
                        $postData['app_name']           =   $appName;
                        $postData['mail_title']         =  'Admin Message';
                        $postData['app_logo']           =   $appLogo;
                        //$postData['app_bg']           =   app_bg; 
                        $subject                        =   "Admin Reply";
                        $mailbody                       =   mailBodyContactUs($postData);
                        $mailResponse                   =   mailSend($postData['email'], $postData['fromName'], $subject, $mailbody);
                    }


                    if ($mailResponse == 'yes') {
                        $record = array('success' => true, 'msg' => $ReplySendSuccess, 'data' => array());
                        jsonSendEncode($record);
                    } else {
                        $record = array('success' => false, 'msg' => $ReplySendError, 'data' => array());
                        jsonSendEncode($record);
                    }
                }
            } else if ($select_sendmail == 'selectvendor') {
                $email = $data['select_vendor'];
                //print_r($user_arr);

                if (count($email) > 0) {
                    for ($i = 0; $i < count($email); $i++) {


                        //$postData['name']               =   $name;
                        $postData['email']              =   $email[$i];
                        $postData['subject']            =   $subject1;
                        $postData['message']            =   $message;
                        $postData['fromName']           =   $appName;
                        $postData['app_name']           =   $appName;
                        $postData['mail_title']         =  'Admin Message';
                        $postData['app_logo']           =   $appLogo;
                        //$postData['app_bg']           =   app_bg; 
                        $subject                        =   "Admin Reply";
                        $mailbody                       =   mailBodyContactUs($postData);
                        $mailResponse                   =   mailSend($postData['email'], $postData['fromName'], $subject, $mailbody);
                    }

                    if ($mailResponse == 'yes') {
                        $record = array('success' => true, 'msg' => $ReplySendSuccess, 'data' => array());
                        jsonSendEncode($record);
                    } else {
                        $record = array('success' => false, 'msg' => $ReplySendError, 'data' => array());
                        jsonSendEncode($record);
                    }
                }
            }
        }
    }
}

function getUserContact($data)
{
    global $mysqli;
    global $onerror;
    include_once('language_message.php');
    //$user_id      = $data['user_id'];
    $srno = 1;
    // echo "SELECT `contact_id`, `user_id`,`name`, `message`,`email`,`status`,`createtime`,`reply_datetime` FROM `contact_us_master` WHERE user_id>0 AND delete_flag=0 order by contact_id desc";die;
    $check_data = $mysqli->prepare("SELECT `contact_id`, `user_id`,`name`, `message`,`email`,`status`,`createtime`,`replied_date_time` FROM `contact_us_master` WHERE user_id>0 AND delete_flag=0 order by contact_id desc");
    $check_data->execute();
    $check_data->store_result();
    $check_data_count = $check_data->num_rows;

    if ($check_data_count > 0) {
        $check_data->bind_result($contact_id, $user_id, $name, $message, $email, $status, $createtime, $reply_datetime);
        while ($check_data->fetch()) {
            $getuser_detail = getUserDetails($user_id);
            if ($getuser_detail != 'NA') {
                $user_name = $getuser_detail['name'];
                 $user_type =$getuser_detail['user_type'];

                    if($user_type ==1){
                        $user_type_text ='<label class="badge badge-info">User</lable>';
                    }else{
                        $user_type_text ='<label class="badge badge-primary">Charm</lable>';
                    }
            }
            if (is_null($user_name)) {
                $user_name = "--";
            }
            // if(is_null($subject)){
            //     $subject = "-";
            // } 

            if (empty($reply_datetime) || $reply_datetime == 'null') {
                $reply_datetime1 = 'NA';
            } else {
                $reply_datetime1 = date("d-m-Y h:i A", strtotime($reply_datetime));;
            }
            if ($status == '0') {
                $status_html = '<label class="badge badge-warning" style="color:white;">Pending</label>';
            } elseif ($status == '1') {
                $status_html = '<label class="badge badge-info">Replied</label>';
            }

            $createtime = date("d-m-Y h:i A", strtotime($createtime));

            $reply_datetime = date("d-m-Y h:i A", strtotime($reply_datetime));


            $action_reply = '<a href="contact_us_reply.php?id=' . encode($contact_id) . '"><button type="button" class="btn btn-primary ">Reply</button></a>';

            $contact_arr[] = array(
                'srno' => $srno,
                'contact_id' => $contact_id,
                'user_id' => $user_id,
                'name' => $name,
                'email' => $email,
                'message' => $message,
                'status' => $status_html,
                'createtime' => $createtime,
                'action' => $action_reply,
                'reply_datetime' => $reply_datetime1,
                'user_type_text'=>$user_type_text,
            );
            $srno++;
        }
    }
    if (empty($contact_arr)) {
        $contact_arr = array();
    }
    $record = array('success' => true, 'msg' => $msgDataFound, 'data' => array('contact_arr' => $contact_arr));
    jsonSendEncode($record);
}
