<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Staff extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function login($type)
	{
		$data = array();
		$data['page_title'] = "Login";
		$data['page'] = "Login";
		$data['type'] = $type;
		$data['page_type'] = 'staff';
		$data['main_content'] = $this->load->view('frontend/login/staff_login', $data, TRUE);
		$this->load->view('frontend/index', $data);
	}


	public function user_login($type)
	{
		$this->form_validation->set_rules('phone', __('username'), 'trim|xss_clean');
		$this->form_validation->set_rules('password', __('password'), 'trim|required|xss_clean');

		if ($this->form_validation->run() == FALSE) {
			return __request(validation_errors(), 0, '');
		}

		// reCAPTCHA check
		$recaptcha = customer_recaptcha();
		if (!empty($recaptcha->is_google_recaptcha) && $recaptcha->is_google_recaptcha == 1 && !$this->security_m->recaptcha($recaptcha)) {
			return __request(__('robot_verification_failed'), 0, '');
		}

		$phone    = $this->input->post('phone', TRUE);
		$email    = $this->input->post('email', TRUE);
		$password = $this->input->post('password', TRUE);
		$is_staff = [];

		// Determine login type
		switch ($type) {
			case 'customer':
				if (preg_match('/^[0-9]+$/', $phone)) {
					$dial_code         = $this->input->post('dial_code') ?? '';
					$phone_with_code   = $dial_code . $phone;
					$is_staff = $this->admin_m->check_customer_login_status($phone_with_code, $phone, $password);
				} else {
					$is_staff = $this->admin_m->check_customer_email_login($email ?? $phone, $password);
				}
				break;

			case 'staff':
				if (filter_var($phone, FILTER_VALIDATE_EMAIL)) {
					$is_staff = $this->admin_m->check_staff_login_info($phone, $password);
				}
				break;

			case 'delivery':
				if (preg_match('/^[0-9]+$/', $phone)) {
					$dial_code         = $this->input->post('dial_code') ?? '';
					$phone_with_code   = $dial_code . $phone;
					$is_staff = $this->admin_m->delivery_login_with_phone($phone_with_code, $phone, $password);
				} else {
					$is_staff = $this->admin_m->delivery_login_with_email($phone, $password);
				}
				break;

			default:
				return __request(lang('invalid_login'), 0, '');
		}


		if (empty($is_staff['result'])) {
			return __request(lang('not_found'), 0, '');
		}

		// Process authenticated user
		foreach ($is_staff['result'] as $row) {
			$role = $row->role;
			$status = $row->status ?? 1;

			if ($role === 'admin_staff'  && $type == "staff") {
				$sessionData = $this->custom_m->staff_login_session_data('admin_staff', $row, true);
			} elseif ($role === 'staff' && $type == "staff") {
				$sessionData = $this->custom_m->staff_login_session_data('staff', $row, false);
			} elseif ($role === 'waiter' && $type == "staff") {
				$sessionData = $this->custom_m->staff_login_session_data('waiter', $row, false);
			} elseif ($role === 'customer' && $type === 'customer') {
				$sessionData = $this->custom_m->staff_login_session_data('customer', $row, false);
			} elseif ($role === 'delivery') {
				if ($status != 1) {
					return __request(lang('permission_denied'), 0, '');
				}
				$sessionData = $this->custom_m->staff_login_session_data('delivery', $row, false);
			} else {
				return __request(lang('invalid_login'), 0, '');
			}

			$this->session->set_userdata($sessionData);
			update_session_type('authenticated');
			$redirectUrl = $this->custom_m->redirectUrl($role);
			return __request(lang('login_success'), 1, $redirectUrl);
		}

		return __request(lang('invalid_login'), 0, '');
	}



	public function customer()
	{
		$data = array();
		$data['page_title'] = "Customer Panel";
		$data['page'] = "Customer";
		if (auth('is_customer') == FALSE) {
			redirect('staff-login/customer');
		}
		$data['info'] = $this->admin_m->single_select_by_id(auth('customer_id'), 'customer_list');
		$data['countries'] = $this->admin_m->select('country');
		$data['question_list'] = $this->admin_m->select('question_list');
		$data['main_content'] = $this->load->view('frontend/customer/customer_profile', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}


	public function password()
	{
		if (empty(auth('is_customer'))) {
			redirect(base_url());
		}
		$data = [];
		$data['page_title'] = "Customer Password";
		$data['info'] = $this->admin_m->single_select_by_id(auth('customer_id'), 'customer_list');
		$data['main_content'] = $this->load->view('frontend/customer/change_password', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}


	public function update_profile()
	{

		$sorry = !empty(lang("sorry")) ? lang("sorry") : "sorry";
		$success = !empty(lang("success")) ? lang("success") : "success";
		$reg_success = !empty(lang("registration_successfull")) ? lang("registration_successfull") : "Registration successfull";
		$invalid = !empty(lang('error_text')) ? lang('error_text') : 'Somethings Were Wrong!!';


		$phone = $this->input->post('phone', true);
		$active_phone = $this->input->post('active_phone', true);

		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('address', 'Address', 'trim|xss_clean');
		$this->form_validation->set_rules('question', 'Question', 'trim|xss_clean');
		$this->form_validation->set_rules('answer', 'Answer', 'trim|xss_clean');
		if ($phone == $active_phone) :
			$this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean');
		else :
			$this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean|is_unique[customer_list.phone]', array('is_unique' => 'The phone is already Exists'));
		endif;
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('staff/customer'));
		} else {

			$name = $this->input->post('name', true);
			$country_id = $this->input->post('country_id', true);
			$question = [
				'id' => $this->input->post('question', true),
				'answer' => $this->input->post('answer', true),
			];

			$data = array(
				'customer_name' => $name,
				'phone' => $phone,
				'email' => $this->input->post('email', true),
				'address' => $this->input->post('address', true),
				'gmap_link' => $this->input->post('gmap_link', true),
				'question' => json_encode($question),
				'country_id' => $country_id,
			);
			$insert = $this->common_m->update($data, auth('customer_id'), 'customer_list');


			if ($insert) :
				$this->upload_profile('customer');
				$this->session->set_flashdata('success', !empty(lang('success_text')) ? lang('success_text') : 'Save Change Successful');
				redirect(base_url('staff/customer'));
			else :
				$this->session->set_flashdata('error', !empty(lang('error_text')) ? lang('error_text') : 'Somethings Were Wrong!!');
				redirect(base_url('staff/customer'));
			endif;
		}
	}


	public function change_password()
	{
		is_test();

		$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|min_length[3]');
		$this->form_validation->set_rules('cpassword', 'Confirm password', 'trim|required|xss_clean|matches[password]');

		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('staff/password'));
		} else {

			$data = array(
				'password' => md5($this->input->post('password', true)),
			);
			$insert = $this->common_m->update($data, auth('customer_id'), 'customer_list');
			if ($insert) :
				$this->session->set_flashdata('success', !empty(lang('success_text')) ? lang('success_text') : 'Save Change Successful');
				redirect(base_url('staff/customer'));
			else :
				$this->session->set_flashdata('error', !empty(lang('error_text')) ? lang('error_text') : 'Somethings Were Wrong!!');
				redirect(base_url('staff/customer'));
			endif;
		}
	}




	public function logout()
	{

		$this->session->unset_userdata('is_customer');
		$this->session->sess_destroy();
		$this->session->set_flashdata('success', 'Successfully logout');
		if (isset($_GET['type']) && $_GET['type'] == "customer") :
			redirect(base_url('staff-login/customer'));
		elseif (isset($_GET['type']) && $_GET['type'] == "delivery") :
			redirect(base_url('staff-login/delivery'));
		endif;
	}

	public function order_list()
	{
		if (empty(auth('is_customer'))) {
			redirect(base_url());
		}
		$data = [];
		$data['page_title'] = "Customer OrderList";
		$data['page'] = "Customer";

		$config = [];
		$this->load->library('pagination');
		$per_page = 12;

		$page = $this->input->get('page');
		if (empty($page)) {
			$page = 0;
		}

		if ($page != 0) {
			$page = $page - 1;
		}
		$offset = ceil($page * $per_page);


		$total = $this->admin_m->get_customer_order_list(auth('customer_id'), 0, 0, $is_total = 1);
		$config['base_url'] = base_url('staff/order_list');
		$config['total_rows'] = $total;
		$config['per_page'] =  $per_page;
		$this->pagination->initialize($config);

		$data['info'] = $this->admin_m->single_select_by_id(auth('customer_id'), 'customer_list');
		$data['order_list'] = $this->admin_m->get_customer_order_list(auth('customer_id'), $per_page, $offset, 0);
		$data['main_content'] = $this->load->view('frontend/customer/order_list', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}

	public function my_invoice($slug = null, $uid = null)
	{
		$data = [];
		$id = get_id_by_slug($slug);
		$data['id'] = $id;
		if (empty($id) || empty($uid)) {
			redirect(base_url('error-404'));
		}
		$data['shop'] = restaurant($id);
		$data['user'] = $this->admin_m->get_profile_info($id);
		$data['shop_id'] = $data['shop']->id;
		$data['slug'] = $slug;
		$data['page_title'] = "Customer OrderList";
		$data['info'] = $this->admin_m->single_select_by_id(auth('customer_id'), 'customer_list');
		$data['order_info'] = $this->admin_m->single_select_by_uid($uid, 'order_user_list');
		$data['item_list'] = $this->admin_m->get_customer_order_item_list($data['shop_id'], $uid);
		$data['main_content'] = $this->load->view('frontend/invoice/my_invoice', $data, TRUE);
		$this->load->view('frontend/invoice/index', $data);
	}
	public function print_invoice($slug = null, $uid = null)
	{
		$data = [];
		$id = get_id_by_slug($slug);
		$data['id'] = $id;
		if (empty($id) || empty($uid)) {
			redirect(base_url('error-404'));
		}
		$data['shop'] = restaurant($id);
		$data['user'] = $this->admin_m->get_profile_info($id);
		$data['shop_id'] = $data['shop']->id;
		$data['slug'] = $slug;
		$data['page_title'] = "Customer OrderList";
		$data['info'] = $this->admin_m->single_select_by_id(auth('customer_id'), 'customer_list');
		$data['order_info'] = $this->admin_m->single_select_by_uid($uid, 'order_user_list');
		$data['item_list'] = $this->admin_m->get_customer_order_item_list($data['shop_id'], $uid);
		$data['main_content'] = $this->load->view('frontend/invoice/my_invoice', $data, TRUE);
		$this->load->view('frontend/invoice/index', $data);
	}


	public function pos_invoice($slug = null, $uid = null)
	{
		$data = [];
		$id = get_id_by_slug($slug);
		$data['id'] = $id;
		if (empty($id) || empty($uid)) {
			redirect(base_url('error-404'));
		}
		$data['shop'] = restaurant($id);
		$data['user'] = $this->admin_m->get_profile_info($id);
		$data['shop_id'] = $data['shop']->id;
		$data['slug'] = $slug;
		$data['uid'] = $uid;
		$data['page_title'] = "Customer OrderList";
		$data['info'] = $this->admin_m->single_select_by_id(auth('customer_id'), 'customer_list');
		$data['order_info'] = $this->admin_m->single_select_by_uid($uid, 'order_user_list');
		$data['item_list'] = $this->admin_m->get_customer_order_item_list($data['shop_id'], $uid);
		$load = $this->load->view('frontend/invoice/pos_invoice', $data, true);
		echo json_encode(['st' => 1, 'result' => $load]);
	}




	public function change_status($status, $id)
	{
		$data = [
			'status' => $status
		];

		$this->admin_m->update($data, $id, 'service_order');
		redirect($_SERVER['HTTP_REFERER']);
	}




	/*----------------------------------------------
     DELIVERY STAFF AREA
     ----------------------------------------------*/
	public function delivery()
	{

		$data = array();
		$data['page_title'] = "Delivery Panel";
		$data['page'] = "Delivery";
		if (auth('is_delivery') == FALSE) {
			redirect(base_url('staff-login/delivery'));
		}
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		$data['countries'] = $this->admin_m->select('country');
		$data['cityList'] = $this->admin_m->select('restaurant_city_list');
		$data['main_content'] = $this->load->view('frontend/delivery/profile', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}

	public function account()
	{

		$data = array();
		$data['page_title'] = "Delivery Account";
		$data['page'] = "Delivery";
		if (auth('is_delivery') == FALSE) {
			redirect(base_url('staff-login/delivery'));
		}
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		$data['order_list'] = $this->admin_m->get_all_accepted_delivery_order_list(auth('staff_id'));
		$data['main_content'] = $this->load->view('frontend/delivery/account', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}


	public function report()
	{

		$data = array();
		$data['page_title'] = "Delivery Report";
		$data['page'] = "Delivery";
		if (auth('is_delivery') == FALSE) {
			redirect(base_url('staff-login/delivery'));
		}
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		$data['order_list'] = $this->statistics_m->get_dboy_statistics(auth('staff_id'));
		$data['dboy_date'] = $this->statistics_m->get_dboy_date(auth('staff_id'));
		$data['main_content'] = $this->load->view('frontend/delivery/statistics', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}


	public function update_delivery_profile()
	{

		is_test();
		$sorry = !empty(lang("sorry")) ? lang("sorry") : "sorry";
		$success = !empty(lang("success")) ? lang("success") : "success";
		$reg_success = !empty(lang("registration_successfull")) ? lang("registration_successfull") : "Registration successfull";
		$invalid = !empty(lang('error_text')) ? lang('error_text') : 'Somethings Were Wrong!!';


		$phone = $this->input->post('phone', true);
		$active_phone = $this->input->post('active_phone', true);

		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		if (isset(st()->is_city) && st()->is_city == 1):
			$this->form_validation->set_rules('city_id', lang('city_name'), 'trim|required|xss_clean');
		endif;
		if ($phone == $active_phone) :
			$this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean');
		else :
			$this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean|is_unique[staff_list.phone]', array('is_unique' => 'The phone is already Exists'));
		endif;
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('staff/delivery'));
		} else {

			$name = $this->input->post('name', true);
			$country_id = $this->input->post('country_id', true);
			$data = array(
				'name' => $name,
				'phone' => $phone,
				'email' => $this->input->post('email', true),
				'city_id' => $this->input->post('city_id', true) ?? 0,
				'country_id' => $country_id,
			);
			$insert = $this->common_m->update($data, auth('staff_id'), 'staff_list');
			if ($insert) :
				$this->upload_profile('dboy');
				$this->session->set_flashdata('success', !empty(lang('success_text')) ? lang('success_text') : 'Save Change Successful');
				redirect(base_url('staff/delivery'));
			else :
				$this->session->set_flashdata('error', !empty(lang('error_text')) ? lang('error_text') : 'Somethings Were Wrong!!');
				redirect(base_url('staff/delivery'));
			endif;
		}
	}


	public function change_delivery_password()
	{
		is_test();

		$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|min_length[3]');
		$this->form_validation->set_rules('cpassword', 'Confirm password', 'trim|required|xss_clean|matches[password]');

		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('staff/password'));
		} else {

			$data = array(
				'password' => md5($this->input->post('password', true)),
			);
			$insert = $this->common_m->update($data, auth('staff_id'), 'staff_list');
			if ($insert) :
				$this->session->set_flashdata('success', !empty(lang('success_text')) ? lang('success_text') : 'Save Change Successful');
				redirect(base_url('staff/delivery'));
			else :
				$this->session->set_flashdata('error', !empty(lang('error_text')) ? lang('error_text') : 'Somethings Were Wrong!!');
				redirect(base_url('staff/delivery'));
			endif;
		}
	}

	public function delivery_password()
	{
		if (empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$data = [];
		$data['page_title'] = "Delivery Password";
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['main_content'] = $this->load->view('frontend/delivery/change_password', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}

	public function new_order_list($type = "new")
	{
		if (empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$data = [];
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		if ($type == "new") {
			$data['page_title'] = "New OrderList";
			$status = 0;
			$is_picked = 0;
			$data['order_list'] = $this->admin_m->get_all_delivery_order_list($data['info']['user_id'], $status, $is_picked);
		} elseif ($type == "accepted") {
			$data['page_title'] = "Accepted OrderList";
			$status = 1;
			$is_picked = 0;
			$data['order_list'] = $this->admin_m->get_all_active_delivery_order_list(auth('staff_id'), $status, $is_picked);
		} elseif ($type == "picked") {
			$data['page_title'] = "Picked OrderList";
			$status = 2;
			$is_picked = 1;
			$data['order_list'] = $this->admin_m->get_all_active_delivery_order_list(auth('staff_id'), $status, $is_picked);
		}

		$data['page'] = "Delivery OrderList";

		$data['main_content'] = $this->load->view('frontend/delivery/order_list', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}

	public function get_delivery_order_notification()
	{
		if (empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$data = [];
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		$order = $this->admin_m->get_all_delivery_order_list($data['info']['user_id'], $status = 0, $is_picked = 0);
		if (sizeof($order) > 0) {
			echo json_encode(['st' => 1]);
		} else {
			echo json_encode(['st' => 0]);
		}
	}

	public function get_order_list($type = "new")
	{
		if (empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$data = [];
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		$is_notify = 0;
		if ($type == "new") {
			$data['page_title'] = "New OrderList";
			$status = 0;
			$is_picked = 0;
			$data['order_list'] = $this->admin_m->get_all_delivery_order_list($data['info']['user_id'], $status, $is_picked);
			if (!empty($data['order_list']) && sizeof($data['order_list']) > 0) {
				$is_notify = 1;
			}
		} elseif ($type == "accepted") {
			$data['page_title'] = "Accepted OrderList";
			$status = 1;
			$is_picked = 0;
			$data['order_list'] = $this->admin_m->get_all_active_delivery_order_list(auth('staff_id'), $status, $is_picked);
		} elseif ($type == "picked") {
			$data['page_title'] = "Picked OrderList";
			$status = 2;
			$is_picked = 1;
			$data['order_list'] = $this->admin_m->get_all_active_delivery_order_list(auth('staff_id'), $status, $is_picked);
		}

		$data['page'] = "Delivery OrderList";
		$load_data = $this->load->view('frontend/delivery/order_thumb', $data, TRUE);
		echo json_encode(['st' => 1, 'load_data' => $load_data, 'is_notify' => $is_notify]);
	}

	public function order_details($slug = null, $uid = null)
	{
		$data = [];
		$id = get_id_by_slug($slug);
		$data['id'] = $id;
		if (empty($id) || empty($uid) || empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$data['shop'] = restaurant($id);
		$data['shop_id'] = $data['shop']->id;
		$data['slug'] = $slug;
		$data['page_title'] = "Customer OrderList";
		$data['is_reject'] = 1;
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['order_info'] = $this->admin_m->single_select_by_uid($uid, 'order_user_list');
		$data['item_list'] = $this->admin_m->get_customer_order_item_list($data['shop_id'], $uid);
		$data['main_content'] = $this->load->view('frontend/delivery/order_details', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}

	public function accept_order($uid = null, $dboy_id = null)
	{
		$data = [];
		if (empty($dboy_id) || empty($uid) || empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$check = $this->common_m->check_order_status($uid, 'is_db_accept');
		$data = array(
			'dboy_id' => $dboy_id,
			'dboy_accept_time' => d_time(),
			'dboy_status' => 1,
			'is_db_accept' => 1,
			'action_by' => 'dboy',
		);
		if ((isset($check) && $check->dboy_id == 0) || (isset($check) && $check->dboy_id == auth('staff_id'))) :
			$this->admin_m->update_by_uid($data, $uid);
			__staffAction($uid, $dboy_id, 'accept');
			$this->session->set_flashdata('success', !empty(lang('success_text')) ? lang('success_text') : 'Save Change Successful');
		else :
			$this->session->set_flashdata('error', lang('this_job_already_handle_by_someone'));
		endif;
		redirect($_SERVER['HTTP_REFERER']);
	}

	public function picked_order($uid = null, $dboy_id = null)
	{
		$data = [];
		if (empty($dboy_id) || empty($uid) || empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}
		$check = $this->common_m->check_order_status($uid);

		$data = array(
			'dboy_picked_time' => d_time(),
			'dboy_status' => 2,
			'is_picked' => 1,
			'dboy_id' => $dboy_id,
			'action_by' => 'dboy',
		);


		if ((isset($check) && $check->dboy_id == 0) || (isset($check->dboy_id) && $check->dboy_id == auth('staff_id'))) :
			$update = $this->admin_m->update_by_uid_dboy_id($data, $uid, $dboy_id);
			__staffAction($uid, $dboy_id, 'picked');
			$this->session->set_flashdata('success', lang('success_text'));
		else :
			$this->session->set_flashdata('error', lang('this_job_already_handle_by_someone'));
		endif;

		redirect($_SERVER['HTTP_REFERER']);
	}

	public function completed_order($uid = null, $dboy_id = null)
	{
		$data = [];
		if (empty($dboy_id) || empty($uid) || empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}

		$check = $this->common_m->check_order_status($uid);
		$data = array(
			'dboy_completed_time' => d_time(),
			'dboy_status' => 3,
			'is_db_completed' => 1,
			'dboy_id' => $dboy_id,
			'action_by' => 'dboy',
		);

		if ((isset($check) && $check->dboy_id == 0) || (isset($check) && $check->dboy_id == auth('staff_id'))) :
			$this->admin_m->update_by_uid_dboy_id($data, $uid, $dboy_id);
			__staffAction($uid, $dboy_id, 'complete');
			$this->session->set_flashdata('success', lang('success_text'));
			redirect(base_url("staff/new_order_list"));
		else :
			$this->session->set_flashdata('error', lang('this_job_already_handle_by_someone'));
			redirect($_SERVER['HTTP_REFERER']);
		endif;
	}

	public function cancel_order($uid = null, $dboy_id = null)
	{
		$data = [];
		if (empty($dboy_id) || empty($uid) || empty(auth('is_delivery'))) {
			redirect(base_url('staff-login/delivery'));
		}

		$check = $this->common_m->check_order_status($uid);

		$reject_id = $_POST['reason_id'] ?? '';
		$reject_msg = $_POST['reason_msg'] ?? '';
		$reject_reason = ['reason_ids' => $reject_id, 'reject_msg' => $reject_msg];

		$data = array(
			'cancel_time' => d_time(),
			'dboy_status' => 4,
			'status' => 3,
			'dboy_id' => $dboy_id,
			'is_db_completed' => 0,
			'reject_reason' => isset($reject_reason) ? json_encode($reject_reason) : '',
			'action_by' => 'dboy',
		);
		if ((isset($check) && $check->dboy_id == 0) || (isset($check) && $check->dboy_id == auth('staff_id'))) :
			$this->admin_m->update_by_uid_dboy_id($data, $uid, $dboy_id);
			__staffAction($uid, $dboy_id, 'reject');
			$this->session->set_flashdata('success', lang('success_text'));
		else :
			$this->session->set_flashdata('error', lang('this_job_already_handle_by_someone'));
		endif;

		redirect($_SERVER['HTTP_REFERER']);
	}

	public function upload_profile($type)
	{
		is_test();
		$data = array();
		if (!empty($_FILES['file']['name'])) {
			$up_load = $this->upload_m->upload(250);
			if ($up_load['st'] == 1) :
				foreach ($up_load['data'] as $key => $value) {
					$data = array(
						'images' => $value['image'],
						'thumb' => $value['thumb'],
					);
					if ($type == 'customer') {
						$this->admin_m->update(array('thumb' => $value['thumb'], 'images' => $value['image']), auth('customer_id'), 'customer_list');
					} else {
						$this->admin_m->update(array('thumb' => $value['thumb'], 'images' => $value['image']), auth('staff_id'), 'staff_list');
					}
				}
				return true;
			else :
				// $this->session->set_flashdata('error', $up_load['data']);
				return false;
			endif;
		} else {
			$this->session->set_flashdata('error', 'Please select an image');
		}
	}

	public function track_order($slug = null, $uid = null)
	{
		$data = [];
		$id = get_id_by_slug($slug);
		$data['id'] = $id;
		if (empty($id) || empty($uid)) {
			redirect(base_url('error-404'));
		}
		$data['shop'] = restaurant($id);
		$data['shop_id'] = $data['shop']->id;
		$data['slug'] = $slug;
		$data['page_title'] = "Customer OrderList";
		$data['info'] = $this->admin_m->single_select_by_id(auth('staff_id'), 'staff_list');
		$data['shop_id'] = $this->admin_m->get_shop_id($data['info']['user_id'] ?? 0);
		$data['order_info'] = $this->admin_m->single_select_by_uid($uid, 'order_user_list');
		$data['main_content'] = $this->load->view('frontend/customer/track_status', $data, TRUE);
		$this->load->view('frontend/customer/index', $data);
	}
	public function order_item_list($uid, $shop_id)
	{
		$data = [];
		$data['shop_id'] = $shop_id;
		$data['order_list'] = $this->common_m->order_item_details_by_order_id($uid, $shop_id);
		$load_data = $this->load->view('frontend/customer/customer_item_list', $data, true);
		echo json_encode(['st' => 1, 'load_data' => $load_data]);
	}
	public function recaptcha()
	{
		$settings =  settings();
		$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
		$userIp = $this->input->ip_address();
		$secret = $this->settings['recaptcha']->secret_key;

		$url = "https://www.google.com/recaptcha/api/siteverify";
		$client = new \GuzzleHttp\Client();
		$response = $client->post($url, [
			'form_params' => [
				'secret'   => $secret,
				'response' => $recaptchaResponse,
				'remoteip' => $userIp,
			]
		]);
		$output = $response->getBody();

		$status = json_decode($output, true);
		if ($status['success']) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
