<?php
/**
 * Fired during plugin activation
 *
 * @link  http://www.powerfulwp.com
 * @since 1.0.0
 *
 * @package    DDFWM
 * @subpackage DDFWM/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    DDFWM
 * @subpackage DDFWM/includes
 * @author     powerfulwp <cs@powerfulwp.com>
 */
class DDFWM_Driver {
	/**
	 * Drivers query.
	 *
	 * @param  mixed $vendor_id .
	 * @param  mixed $type .
	 * @since 1.0.0
	 * @return array
	 */
	public static function ddfwm_get_drivers( $vendor_id, $type = '' ) {
		global $ddfwm_page, $ddfwm_vendor_drivers;
		$number_of_users = 20;

		if ( 'all' === $type ) {
			$array = array();
		} else {
			$array = array(
				'number' => $number_of_users,
				'paged'  => $ddfwm_page,
			);
		}

		if ( '1' === $ddfwm_vendor_drivers ) {
			$meta_array = array(
				'relation' => 'or',
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => $vendor_id,
					'compare' => '=',
				),
				array(
					'key'     => 'ddfwm_vendor',
					'compare' => 'NOT EXISTS',
				),
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => '',
					'compare' => '=',
				),
			);
		} elseif ( '2' === $ddfwm_vendor_drivers ) {
			$meta_array = array(
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => $vendor_id,
					'compare' => '=',
				),
			);
		} else {
			$meta_array = array(
				'relation' => 'or',
				array(
					'key'     => 'ddfwm_vendor',
					'compare' => 'NOT EXISTS',
				),
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => '',
					'compare' => '=',
				),
			);
		}

		$args = array(
			'role'       => 'driver',
			'meta_query' => $meta_array,
			'orderby'    => 'meta_value ASC,display_name ASC',
			$array,
		);
		return get_users( $args );
	}

	/**
	 * All Drivers
	 *
	 * @param  mixed $vendor_id .
	 * @since 1.0.0
	 * @return array
	 */
	public function ddfwm_drivers( $vendor_id ) {
		global $ddfwm_page, $ddfwm_vendor_drivers;

		$vendor = new DDFWM_Vendor();

		if ( '1' === $ddfwm_vendor_drivers ) {
			$array = array(
				'relation' => 'or',
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => $vendor_id,
					'compare' => '=',
				),
				array(
					'key'     => 'ddfwm_vendor',
					'compare' => 'NOT EXISTS',
				),
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => '',
					'compare' => '=',
				),
			);
		} elseif ( '2' === $ddfwm_vendor_drivers ) {
			$array = array(
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => $vendor_id,
					'compare' => '=',
				),
			);
		} else {
			$array = array(
				'relation' => 'or',
				array(
					'key'     => 'ddfwm_vendor',
					'compare' => 'NOT EXISTS',
				),
				array(
					'key'     => 'ddfwm_vendor',
					'value'   => '',
					'compare' => '=',
				),
			);
		}

		// Drivers query.
		$drivers_per_page = 20;
		$args             = array(
			'role'       => 'driver',
			'meta_query' => $array,
			'orderby'    => 'display_name ASC',
			'number'     => $drivers_per_page,
			'paged'      => $ddfwm_page,
		);

		$user_query    = new WP_User_Query( $args );
		$drivers       = $user_query->get_results();
		$total_drivers = $user_query->get_total();
		$num_pages     = ceil( $total_drivers / $drivers_per_page );
		// Pagination.
		$base       = ddfwm_vendors_page_url( 'ddfwm_screen=drivers' ) . '&ddfwm_page=%#%';
		$pagination = paginate_links(
			array(
				'base'         => $base,
				'total'        => $num_pages,
				'current'      => $ddfwm_page,
				'format'       => '&ddfwm_page=%#%',
				'show_all'     => false,
				'type'         => 'array',
				'end_size'     => 2,
				'mid_size'     => 0,
				'prev_next'    => true,
				'prev_text'    => sprintf( '<i></i> %1$s', __( '<<', 'ddfwm' ) ),
				'next_text'    => sprintf( '%1$s <i></i>', __( '>>', 'ddfwm' ) ),
				'add_args'     => false,
				'add_fragment' => '',
			)
		);

		$html = '
	<div class="container">
	<div class="row">
	<div class="col-12">';
		if ( ! empty( $drivers ) ) {
			$html   .= '	<div class="table-responsive"><table class="table table-striped table-hover">
		<thead class="table-dark">
			<tr>
				<th scope="col">#</th>
				<th scope="col">' . esc_html( __( 'Name', 'ddfwm' ) ) . '</th>
				<th scope="col">' . esc_html( __( 'Phone', 'ddfwm' ) ) . '</th>
				<th scope="col">' . esc_html( __( 'Email', 'ddfwm' ) ) . '</th>
				<th scope="col" class="text-center">' . esc_html( __( 'Account', 'ddfwm' ) ) . '</th>
				<th scope="col" class="text-center">' . esc_html( __( 'Claim', 'ddfwm' ) ) . '</th>
				<th scope="col" class="text-center">' . esc_html( __( 'Availability', 'ddfwm' ) ) . '</th>
				<th scope="col" class="text-center">' . esc_html( __( 'Vendor', 'ddfwm' ) ) . '</th>
			</tr>
		</thead>
		<tbody>';
			$counter = ( 1 < $ddfwm_page ) ? $ddfwm_page * $drivers_per_page : 1;

			foreach ( $drivers as $user ) {

				$driver_account      = get_user_meta( $user->ID, 'lddfw_driver_account', true );
				$driver_availability = get_user_meta( $user->ID, 'lddfw_driver_availability', true );
				$driver_claim        = get_user_meta( $user->ID, 'lddfw_driver_claim', true );
				$seller_id           = get_user_meta( $user->ID, 'ddfwm_vendor', true );
				$store_name          = $vendor->ddfwm_store_name( '', $seller_id );
				// Status icons.
				if ( '1' === $driver_availability ) {
					$driver_availability_icon = '<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="circle" class=" text-success svg-inline--fa fa-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>';
				} else {
					$driver_availability_icon = '<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="circle" class=" text-danger svg-inline--fa fa-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>';
				}

				if ( '1' === $driver_account ) {
					$driver_account_icon = '<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="circle" class=" text-success svg-inline--fa fa-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>';
				} else {
					$driver_account_icon = '<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="circle" class=" text-danger svg-inline--fa fa-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>';
				}

				if ( '1' === $driver_claim ) {
					$driver_claim_icon = '<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="circle" class=" text-success svg-inline--fa fa-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>';
				} else {
					$driver_claim_icon = '<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="circle" class=" text-danger svg-inline--fa fa-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>';
				}

				$icon_class = ( strval( $seller_id ) === strval( $vendor_id ) ) ? 'ddfwm_user_icon' : 'ddfwm_user_icon_disable';

				$html .= '
			<tr>
				<th scope="row">' . $counter . '</th>
				<td><a href="' . ddfwm_vendors_page_url( 'ddfwm_screen=driver&ddfwm_driverid=' . $user->ID ) . '" >' . esc_html( $user->first_name . ' ' . $user->last_name ) . '</a></td>
				<td>' . esc_html( $user->billing_phone ) . '</td>
				<td>' . esc_html( $user->user_email ) . '</td>
				<td class="text-center">' . ddfwm_premium_feature( '<a href="#" class="' . $icon_class . '" service="ddfwm_driver_account_status" driver_id="' . esc_attr( $user->ID ) . '">' . $driver_account_icon . '</a>' ) . '</td>
				<td class="text-center">' . ddfwm_premium_feature( '<a href="#" class="' . $icon_class . '" service="ddfwm_claim_permission" driver_id="' . esc_attr( $user->ID ) . '">' . $driver_claim_icon . '</a>' ) . '</td>
				<td class="text-center">' . ddfwm_premium_feature( '<a href="#" class="' . $icon_class . '" service="ddfwm_availability" driver_id="' . esc_attr( $user->ID ) . '">' . $driver_availability_icon . '</a>' ) . '</td>
				<td class="text-center">' . $store_name . '</td>
			</tr>';
				$counter ++;
			}

			$html .= '</tbody>
		</table></div>';
		} else {
			$html .= '<div class="alert alert-info">' . esc_html( __( 'Drivers not found.', 'ddfwm' ) ) . '</div>';
		}
		if ( ! empty( $pagination ) ) {
			$html .= '<div class="pagination text-sm-center"><nav aria-label="Page navigation" style="width:100%"><ul class="pagination justify-content-center">';
			foreach ( $pagination as $page ) {
				$html .= '<li class="page-item ';
				if ( strpos( $page, 'current' ) !== false ) {
					$html .= ' active';
				}
				$html .= '"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
			}
			$html .= '</nav></div>';
		}
		$html .= '</div>
		</div>
		</div>';
		return $html;
	}

	/**
	 * Edit driver form
	 *
	 * @param  mixed $user_meta .
	 * @since 1.0.0
	 * @return array
	 */
	public function ddfwm_edit_driver_form( $user_meta ) {

		$first_name      = $user_meta->first_name;
		$last_name       = $user_meta->last_name;
		$email           = $user_meta->user_email;
		$billing_country = $user_meta->billing_country;
		$phone           = $user_meta->billing_phone;
		$city            = $user_meta->billing_city;
		$company         = $user_meta->billing_company;
		$address_1       = $user_meta->billing_address_1;
		$address_2       = $user_meta->billing_address_2;
		$postcode        = $user_meta->billing_postcode;
		$billing_state   = $user_meta->billing_state;

		$html = '<div class="container">
			<div class="row">
				<div class="col-12">';

				// Check if vendor driver.
				$driver_id     = $user_meta->ID;
				$vendor        = wp_get_current_user();
				$vendor_id     = $vendor->ID;
				$driver_vendor = get_user_meta( $driver_id, 'ddfwm_vendor', true );

		if ( strval( $driver_vendor ) === strval( $vendor_id ) ) {

			$html .= '<form service="ddfwm_edit_driver" class="ddfwm_form ddfwm_edit_driver_form">
						<input type="hidden" name="ddfwm_driverid" value="' . $driver_id . '">
						<div class="ddfwm_alert_wrap"></div>
						<div class="ddfwm_wrap">
						';

				$html .= '<div class="form-group row upload_image_form">
						<label class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Photo', 'ddfwm' ) ) . '</label>
						<div class="col-sm-10">
						<div class="upload_image_wrap"><span class="ddfwm_helper"></span>';
				/* driver photo */
				$image    = '';
				$image_id = get_user_meta( $driver_id, 'lddfw_driver_image', true );
			if ( intval( $image_id ) > 0 ) {
					$image = wp_get_attachment_image_src( $image_id, 'medium' )[0];
				if ( '' != $image ) {
					$html .= '<img src="' . esc_url( $image ) . '">';
				}
			}
			if ( '' === $image ) {
						$html .= '<img src="' . esc_url( plugins_url() . '/' . DDFWM_FOLDER . '/public/images/user.png?ver=' . DDFWM_VERSION ) . '">';
			}
							$html .= '

						</div>
						<div class="custom-file photo_upload" >
							<input type="hidden" class="ddfwm_image_input" name="ddfwm_image_input" value="" >
							<input type="file" class="custom-file-input ddfwm_upload_image"  >
							<label class="custom-file-label" for="upload_image">
							<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="camera" class="svg-inline--fa fa-camera fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M512 144v288c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V144c0-26.5 21.5-48 48-48h88l12.3-32.9c7-18.7 24.9-31.1 44.9-31.1h125.5c20 0 37.9 12.4 44.9 31.1L376 96h88c26.5 0 48 21.5 48 48zM376 288c0-66.2-53.8-120-120-120s-120 53.8-120 120 53.8 120 120 120 120-53.8 120-120zm-32 0c0 48.5-39.5 88-88 88s-88-39.5-88-88 39.5-88 88-88 88 39.5 88 88z"></path></svg>
							</label>
						</div>
						</div></div>';

							$html .= '<div class="form-group row">
							<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'First name', 'ddfwm' ) ) . '</label>
							<div class="col-sm-10">
							<input type="text" name="ddfwm_first_name" value="' . $first_name . '" class="form-control reqi" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'First name', 'ddfwm' ) ) . '">
							</div>
						</div>
						<div class="form-group row">
							<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Last name', 'ddfwm' ) ) . '</label>
							<div class="col-sm-10">
							<input type="text" name="ddfwm_last_name" value="' . $last_name . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Last name', 'ddfwm' ) ) . '">
							</div>
						</div>
						<div class="form-group row">
							<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Company', 'ddfwm' ) ) . '</label>
							<div class="col-sm-10">
							<input type="text" name="ddfwm_company" value="' . $company . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Company', 'ddfwm' ) ) . '">
							</div>
						</div>
						<div class="form-group row">
							<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Address line 1', 'ddfwm' ) ) . '</label>
							<div class="col-sm-10">
							<input type="text" name="ddfwm_address_1" value="' . $address_1 . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Address line 1', 'ddfwm' ) ) . '">
							</div>
						</div>
						<div class="form-group row">
							<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Address line 2', 'ddfwm' ) ) . '</label>
							<div class="col-sm-10">
							<input type="text" name="ddfwm_address_2" value="' . $address_2 . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Address line 2', 'ddfwm' ) ) . '">
							</div>
						</div>
						<div class="form-group row">
						<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'City', 'ddfwm' ) ) . '</label>
						<div class="col-sm-10">
						<input type="text" name="ddfwm_city" value="' . $city . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'City', 'ddfwm' ) ) . '">
						</div>
					</div>
					<div class="form-group row">
						<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Postcode / ZIP', 'ddfwm' ) ) . '</label>
						<div class="col-sm-10">
						<input type="text" name="ddfwm_postcode" value="' . $postcode . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Postcode / ZIP', 'ddfwm' ) ) . '">
						</div>
					</div>

						';

							global $woocommerce;
							$countries_obj = new WC_Countries();
							$countries     = $countries_obj->__get( 'countries' );

							$default_country       = $countries_obj->get_base_country();
							$default_county_states = $countries_obj->get_states( 'US' );

							$html .= '<div class="form-group row">
						<label  class="col-sm-2 col-form-label" for="exampleInputPassword1">' . esc_html( __( 'Country / Region', 'ddfwm' ) ) . '</label>';
							$html .= '<div class="col-sm-10"><select id="billing_country" name="ddfwm_country" class="form-control">
							<option value="">' . esc_html( __( 'Select Country / Region', 'ddfwm' ) ) . '</option>';
			foreach ( $countries as $key => $country ) {
				 $html .= '<option value="' . $key . '" ' . selected( $billing_country, $key, false ) . ' >' . $country . '</option>';
			}
							$html .= '</select>';
							$html .= '</div></div>';

							$html .= '<div class="form-group row">
								  <label class="col-sm-2 col-form-label" for="exampleInputPassword1">' . esc_html( __( 'State / County', 'ddfwm' ) ) . '</label>';
							$html .= '<div class="col-sm-10"><select style="display:none" id="billing_state_select" name="billing_state_select" class="form-control">
									<option value="">' . esc_html( __( 'Select State / County', 'ddfwm' ) ) . '</option>';
			foreach ( $default_county_states as $key => $state ) {
				$html .= '<option value="' . $key . '" ' . selected( $billing_state, $key, false ) . ' >' . $state . '</option>';
			}
							$html .= '</select>
								  <input type="text" style="display:none" class="form-control" id="billing_state_input"  placeholder="' . esc_html( __( 'State / County', 'ddfwm' ) ) . '" value="' . esc_attr( $billing_state ) . '" name="billing_state">';
							$html .= '</div></div>';

							$html .= '<div class="form-group row">
						<label  class="col-sm-2 col-form-label" for="exampleInputEmail1">' . esc_html( __( 'Phone number', 'ddfwm' ) ) . '</label>
						<div class="col-sm-10">
						<input type="text" name="ddfwm_phone" value="' . $phone . '" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Phone number', 'ddfwm' ) ) . '">
						</div>
					</div>
					<div class="form-group row">
						<label class="col-sm-2 col-form-label"  for="exampleInputEmail1">' . esc_html( __( 'Email address', 'ddfwm' ) ) . '</label>
						<div class="col-sm-10">
						<input type="email" name="ddfwm_email"  value="' . $email . '"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Enter email', 'ddfwm' ) ) . '">
						</div>
					</div>
					
					<div class="form-group row">
							<label class="col-sm-2 col-form-label"  for="exampleInputEmail1">' . esc_html( __( 'Password', 'ddfwm' ) ) . '</label>
							<div class="col-sm-10">
								<button type="button" id="new_password_button" class="btn btn-secondary">' . esc_html( __( 'Set New Password', 'ddfwm' ) ) . '</button>
								<div class = "row" id = "ddfwm_password_holder" style = "display:none" >
									<div class="col-6">
										<input type="text" name="ddfwm_password" id="ddfwm_password"  value="" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Enter password', 'ddfwm' ) ) . '">
									</div>
									<div class="col-6">
										<button  type="button" id="cancel_password_button" class="btn btn-secondary">' . esc_html( __( 'Cancel', 'ddfwm' ) ) . '</button>
									</div>
								</div>
							</div>
						</div>
					';

							$html    .= '<div class="form-group row"><label class="col-sm-2 col-form-label" for="lddfw_driver_account">' . esc_html( __( 'Driver account status', 'lddfw' ) ) . '</label>
						<div class="col-sm-10">
							<select name="lddfw_driver_account"  class="form-control small-select"  id="lddfw_driver_account">
								<option value="0">' . esc_html( __( 'Not active', 'lddfw' ) ) . '</option>';
						$selected     = get_user_meta( $driver_id, 'lddfw_driver_account', true ) === '1' ? 'selected' : '';
						$html        .= '<option ' . esc_attr( $selected ) . ' value="1">' . esc_html( __( 'Active', 'lddfw' ) ) . '</option>
							</select>
							<small id="emailHelp" class="form-text text-muted">' . esc_html( __( 'Only drivers with active accounts can access the drivers\' panel.', 'lddfw' ) ) . '</small>
						</div>
						</div>

						<div class="form-group row">
						<label class="col-sm-2 col-form-label" for="lddfw_driver_availability"> ' . esc_html( __( 'Driver availability', 'lddfw' ) ) . '</label>
						<div class="col-sm-10">
							<select class="form-control small-select" name="lddfw_driver_availability" id="lddfw_driver_availability">
								<option value="0"> ' . esc_html( __( 'Unavailable', 'lddfw' ) ) . '</option>';
						$selected     = get_user_meta( $driver_id, 'lddfw_driver_availability', true ) === '1' ? 'selected' : '';
						$html        .= '<option  ' . esc_attr( $selected ) . ' value="1"> ' . esc_html( __( 'Available', 'lddfw' ) ) . ' </option>
							</select>
							<small id="emailHelp" class="form-text text-muted">' . esc_html( __( 'The delivery driver availability for work today.', 'lddfw' ) ) . ' </small>
						</div>
						</div>

						<div class="form-group row">
							<label for="lddfw_driver_claim" class="col-sm-2 col-form-label" > ' . esc_html( __( 'Driver can claim orders', 'lddfw' ) ) . ' </label> ';
							$selected = get_user_meta( $driver_id, 'lddfw_driver_claim', true ) === '1' ? 'selected' : '';
							$html    .= '
							<div class="col-sm-10">
								<select name="lddfw_driver_claim"  class="form-control small-select"  id="lddfw_driver_claim">
								<option value="0">' . esc_html( __( 'No', 'lddfw' ) ) . '</option>
								<option ' . esc_attr( $selected ) . ' value = "1" >' . esc_html( __( 'Yes', 'lddfw' ) ) . '</option>
								</select>
								<small id="emailHelp" class="form-text text-muted">' . esc_html( __( 'Give the driver permission to claim orders.', 'lddfw' ) ) . '</small>
							</div>
						</div>';

							// buttons.
							$html .= '
							<div class="d-grid gap-2 col-12 col-md-6 mx-auto">	
							<button class="ddfwm_submit_btn btn btn-lg btn-primary btn-block" type="submit">
								' . esc_html( __( 'Update', 'ddfwm' ) ) . '
								</button>
								<button style="display:none" class="ddfwm_loading_btn btn-lg btn btn-block btn-primary" type="button" disabled>
								<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
								' . esc_html( __( 'Loading', 'ddfwm' ) ) . '
								</button>
								</div>
						</form>';
		} else {
			$html .= '<div class="alert alert-danger" role="alert">'
			. esc_html( __( 'The driver does not belong to you.', 'ddfwm' ) ) . '
							</div>';
		}
						$html .= '</div>
						</div>
			</div>
		</div>';
		return $html;
	}

	/**
	 * New driver form
	 *
	 * @since 1.0.0
	 * @return array
	 */
	public function ddfwm_new_driver_form() {
		$html = '<div class="container">
			<div class="row">
				<div class="col-12">
					<form service="ddfwm_new_driver" class="ddfwm_form">
						<div class="ddfwm_alert_wrap"></div>
						<div class="ddfwm_wrap ddfwm_hide_on_success">
						';

					$html .= '<div class="mb-3">
							<label class="form-label" for="ddfwm_first_name">' . esc_html( __( 'First name', 'ddfwm' ) ) . '</label>
							<input type="text" name="ddfwm_first_name"   class="form-control reqi" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'First name', 'ddfwm' ) ) . '">
						</div>
						<div class="mb-3">
							<label class="form-label" for="ddfwm_last_name">' . esc_html( __( 'Last name', 'ddfwm' ) ) . '</label>
							<input type="text" name="ddfwm_last_name"    class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Last name', 'ddfwm' ) ) . '">
						</div>
						<div class="mb-3">
							<label class="form-label" for="ddfwm_phone">' . esc_html( __( 'Phone number', 'ddfwm' ) ) . '</label>
							<input type="text" name="ddfwm_phone"   class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Phone number', 'ddfwm' ) ) . '">
						</div>
						<div class="mb-3">
							<label class="form-label" for="ddfwm_email">' . esc_html( __( 'Email address', 'ddfwm' ) ) . '</label>
							<input type="email" name="ddfwm_email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="' . esc_html( __( 'Enter email', 'ddfwm' ) ) . '">
						</div>
						<div class="mb-3">
							<label class="form-label" for="ddfwm_password">' . esc_html( __( 'Password', 'ddfwm' ) ) . '</label>
							<input type="password" name="ddfwm_password"   class="form-control" id="exampleInputPassword1" placeholder="' . esc_html( __( 'Password', 'ddfwm' ) ) . '">
						</div>';

						global $woocommerce;
						$countries_obj = new WC_Countries();
						$countries     = $countries_obj->__get( 'countries' );
						$html         .= '<div class="mb-3">
						<label class="form-label" for="ddfwm_country">' . esc_html( __( 'Country', 'ddfwm' ) ) . '</label>';
						$html         .= '<select name="ddfwm_country" class="form-control">
							<option value="">' . esc_html( __( 'Select country', 'ddfwm' ) ) . '</option>';
		foreach ( $countries as $key => $country ) {
			$html .= '<option value="' . $key . '">' . $country . '</option>';
		}
						$html .= '</select>';
						$html .= '</div>';

						// buttons.
						$html .= '
						<div class="d-grid gap-2 col-12 col-md-6 mx-auto">		
						<button class="ddfwm_submit_btn btn btn-lg btn-primary btn-block" type="submit">
								' . esc_html( __( 'Submit', 'ddfwm' ) ) . '
								</button>
								<button style="display:none" class="ddfwm_loading_btn btn-lg btn btn-block btn-primary" type="button" disabled>
								<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
								' . esc_html( __( 'Loading', 'ddfwm' ) ) . '
								</button>
								</div>
						</form>
						</div>
						</div>
			</div>
		</div>';
		return $html;
	}



	/**
	 * Create New driver
	 *
	 * @since 1.0.0
	 */
	public function ddfwm_edit_driver_service() {
		$error  = '';
		$result = '0';
		// Security check.
		if ( isset( $_POST['ddfwm_wpnonce'] ) ) {
			$nonce = sanitize_text_field( wp_unslash( $_POST['ddfwm_wpnonce'] ) );
			if ( ! wp_verify_nonce( $nonce, 'ddfwm-nonce' ) ) {
				$error = __( 'Security Check Failure - This alert may occur when you are logged in as an administrator and as a vendor on the same browser and the same device. If you want to work on both panels please try to work with two different browsers.', 'ddfwm' );
			} else {
				// Check vendor.
				$vendor = wp_get_current_user();
				if ( ! in_array( ddfwm_vendor_role(), (array) $vendor->roles, true ) ) {
					// No vendor.
					$error = __( 'You are not a vendor.', 'ddfwm' );
				} else {
					$vendor_id = $vendor->ID;

					$driver_id           = ( isset( $_POST['ddfwm_driverid'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_driverid'] ) ) : '';
					$email               = ( isset( $_POST['ddfwm_email'] ) ) ? sanitize_email( wp_unslash( $_POST['ddfwm_email'] ) ) : '';
					$first_name          = ( isset( $_POST['ddfwm_first_name'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_first_name'] ) ) : '';
					$last_name           = ( isset( $_POST['ddfwm_last_name'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_last_name'] ) ) : '';
					$phone               = ( isset( $_POST['ddfwm_phone'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_phone'] ) ) : '';
					$country             = ( isset( $_POST['ddfwm_country'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_country'] ) ) : '';
					$company             = ( isset( $_POST['ddfwm_company'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_company'] ) ) : '';
					$address_1           = ( isset( $_POST['ddfwm_address_1'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_address_1'] ) ) : '';
					$address_2           = ( isset( $_POST['ddfwm_address_2'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_address_2'] ) ) : '';
					$city                = ( isset( $_POST['ddfwm_city'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_city'] ) ) : '';
					$postcode            = ( isset( $_POST['ddfwm_postcode'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_postcode'] ) ) : '';
					$password            = ( isset( $_POST['ddfwm_password'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_password'] ) ) : '';
					$driver_account      = ( isset( $_POST['lddfw_driver_account'] ) ) ? sanitize_text_field( wp_unslash( $_POST['lddfw_driver_account'] ) ) : '';
					$driver_availability = ( isset( $_POST['lddfw_driver_availability'] ) ) ? sanitize_text_field( wp_unslash( $_POST['lddfw_driver_availability'] ) ) : '';
					$driver_claim        = ( isset( $_POST['lddfw_driver_claim'] ) ) ? sanitize_text_field( wp_unslash( $_POST['lddfw_driver_claim'] ) ) : '';
					$state               = ( isset( $_POST['billing_state'] ) ) ? sanitize_text_field( wp_unslash( $_POST['billing_state'] ) ) : '';
					$image               = ( isset( $_POST['ddfwm_image_input'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ddfwm_image_input'] ) ) : '';

					if ( '' === $driver_id ) {
						// No driver.
						$error = __( 'Driver number is empty.', 'ddfwm' );
					} else {
						// Check for empty fields.
						if ( '' === $email ) {
							// No email.
							$error = __( 'The email field is empty.', 'ddfwm' );
						} else {
							if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
								// Invalid Email.
								$error = __( 'The email is invalid.', 'ddfwm' );
							} else {
								// Email exist for another user.
								$user    = get_user_by( 'email', $email );
								$user_id = $user->data->ID;
								if ( $user && (string) $user_id !== (string) $driver_id ) {
									$error = __( 'Email exist for another user.', 'ddfwm' );
								} else {

									if ( ! user_can( $driver_id, 'driver' ) ) {
										$error = __( 'The user is not a driver.', 'pwddm' );
									} else {

										if ( '' === $first_name ) {
											$error = __( 'First name is empty.', 'ddfwm' );
										} else {
											if ( '' === $last_name ) {
												$error = __( 'Last name is empty.', 'ddfwm' );
											} else {
												if ( '' === $phone ) {
													$error = __( 'Phone is empty.', 'ddfwm' );
												} else {
													if ( '' === $address_1 ) {
														$error = __( 'Address 1 is empty.', 'ddfwm' );
													} else {
														if ( '' === $city ) {
															$error = __( 'City is empty.', 'ddfwm' );
														} else {
															if ( '' === $country ) {
																$error = __( 'Country is empty.', 'ddfwm' );
															} else {
																// Check vendor driver.
																$driver_vendor = get_user_meta( $driver_id, 'ddfwm_vendor', true );
																if ( strval( $driver_vendor ) !== strval( $vendor_id ) ) {
																	$error = __( 'The driver does not belong to you.', 'ddfwm' );
																} else {
																	wp_update_user(
																		array(
																			'ID' => $driver_id,
																			'first_name' => $first_name,
																			'last_name' => $last_name,
																			'user_email' => $email,
																			'nickname' => $first_name . ' ' . $last_name,
																		)
																	);
																	update_user_meta( $driver_id, 'billing_first_name', $first_name );
																	update_user_meta( $driver_id, 'billing_last_name', $last_name );
																	update_user_meta( $driver_id, 'billing_company', $company );
																	update_user_meta( $driver_id, 'billing_address_1', $address_1 );
																	update_user_meta( $driver_id, 'billing_address_2', $address_2 );
																	update_user_meta( $driver_id, 'billing_postcode', $postcode );
																	update_user_meta( $driver_id, 'billing_city', $city );
																	update_user_meta( $driver_id, 'billing_state', $state );
																	update_user_meta( $driver_id, 'billing_phone', $phone );
																	update_user_meta( $driver_id, 'billing_country', $country );
																	update_user_meta( $driver_id, 'lddfw_driver_account', $driver_account );
																	update_user_meta( $driver_id, 'lddfw_driver_availability', $driver_availability );
																	update_user_meta( $driver_id, 'lddfw_driver_claim', $driver_claim );

																	if ( '' !== $password ) {
																		wp_set_password( $password, $driver_id );
																	}
																	if ( '' !== $image ) {
																		$image_id = ddfwm_add_image_to_media( $image, 'v_driver' );
																		update_user_meta( $driver_id, 'lddfw_driver_image', $image_id );
																	}

																	$result = 1;
																	$error  = __( 'The driver successfully updated.', 'ddfwm' );
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		echo '{"result":"' . esc_attr( $result ) . '", "error":"' . esc_attr( $error ) . '"}';
	}




	/**
	 * Create New driver
	 *
	 * @since 1.0.0
	 */
	public function ddfwm_create_new_driver_service() {
		$error  = '';
		$result = '0';
		// Security check.
		if ( isset( $_POST['ddfwm_wpnonce'] ) ) {
			$nonce = sanitize_text_field( wp_unslash( $_POST['ddfwm_wpnonce'] ) );
			if ( ! wp_verify_nonce( $nonce, 'ddfwm-nonce' ) ) {
				$error = esc_html( __( 'Security Check Failure - This alert may occur when you are logged in as an administrator and as a vendor on the same browser and the same device. If you want to work on both panels please try to work with two different browsers.', 'ddfwm' ) );
			} else {
				$vendor = wp_get_current_user();
				if ( ! in_array( ddfwm_vendor_role(), (array) $vendor->roles, true ) ) {
					// No vendor.
					$error = esc_html( __( 'You are not a vendor.', 'ddfwm' ) );
				} else {
					$vendor_id = $vendor->ID;

					if ( isset( $_POST['ddfwm_email'] ) ) {
						$email = sanitize_email( wp_unslash( $_POST['ddfwm_email'] ) );
					}
					if ( isset( $_POST['ddfwm_password'] ) ) {
						$password = sanitize_text_field( wp_unslash( $_POST['ddfwm_password'] ) );
					}
					if ( isset( $_POST['ddfwm_last_name'] ) ) {
						$last_name = sanitize_text_field( wp_unslash( $_POST['ddfwm_last_name'] ) );
					}
					if ( isset( $_POST['ddfwm_first_name'] ) ) {
						$first_name = sanitize_text_field( wp_unslash( $_POST['ddfwm_first_name'] ) );
					}
					if ( isset( $_POST['ddfwm_phone'] ) ) {
						$phone = sanitize_text_field( wp_unslash( $_POST['ddfwm_phone'] ) );
					}
					if ( isset( $_POST['ddfwm_country'] ) ) {
						$country = sanitize_text_field( wp_unslash( $_POST['ddfwm_country'] ) );
					}

					// Check for empty fields.
					if ( empty( $email ) ) {
						// No email.
						$error = esc_html( __( 'The email field is empty.', 'ddfwm' ) );
					} else {
						if ( empty( $password ) ) {
							// No password.
							$error = esc_html( __( 'The password field is empty.', 'ddfwm' ) );
						} else {
							if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
								// Invalid Email.
								$error = esc_html( __( 'The email is invalid.', 'ddfwm' ) );
							} else {
								// Check if user exists in WordPress database.
								$user = get_user_by( 'email', $email );
								// Bad email.
								if ( $user ) {
									$error = esc_html( __( 'User is already exist in the system.', 'ddfwm' ) );
								} else {
									if ( empty( $first_name ) ) {
										$error = esc_html( __( 'First name is empty.', 'ddfwm' ) );
									} else {
										if ( empty( $last_name ) ) {
											$error = esc_html( __( 'Last name is empty.', 'ddfwm' ) );
										} else {
											if ( empty( $phone ) ) {
												$error = esc_html( __( 'Phone is empty.', 'ddfwm' ) );
											} else {
												if ( empty( $country ) ) {
													$error = esc_html( __( 'Country is empty.', 'ddfwm' ) );
												} else {

													$username = $email;
													$user_id  = username_exists( $username );
													if ( ! $user_id && email_exists( $email ) === false ) {
														$user_id = wp_create_user( $username, $password, $email );
														if ( ! is_wp_error( $user_id ) ) {
															$user = get_user_by( 'id', $user_id );
															$user->set_role( 'driver' );
															wp_update_user(
																array(
																	'ID' => $user_id,
																	'first_name' => $first_name,
																	'last_name' => $last_name,
																	'user_email' => $email,
																	'nickname' => $first_name . ' ' . $last_name,
																	'display_name' => $first_name . ' ' . $last_name,
																)
															);
															update_user_meta( $user_id, 'ddfwm_vendor', $vendor_id );
															update_user_meta( $user_id, 'lddfw_driver_account', '1' );
															update_user_meta( $user_id, 'billing_phone', $phone );
															update_user_meta( $user_id, 'billing_country', $country );
															$result = 1;
															$error  = '<p>' . esc_html( __( 'New driver successfully created ', 'ddfwm' ) ) . ' <br><br> <a href=\'' . ddfwm_vendors_page_url( 'ddfwm_screen=driver&ddfwm_driverid=' . $user_id ) . '\'  class=\'btn btn-primary\'>' . esc_html( __( 'View driver', 'ddfwm' ) ) . '</a></p>';
														} else {
															$error = esc_html( __( 'An error occurred creating new driver.', 'ddfwm' ) );
														}
													} else {
														$error = esc_html( __( 'User is already exist in the system.', 'ddfwm' ) );
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		echo '{"result":"' . esc_attr( $result ) . '", "error":"' . $error . '"}';
	}


}
