<?php
$must_login        = false ;
$require_path      = '../../' ;
$require_sub       = '../' ;
require( $require_path.'extensions/sms.php' ) ;
require( $require_path.'extensions/mailer.php' ) ;
require( $require_sub.'header.php' ) ;

$status            = '300' ;

if ( $array['staff_idno'] != '' && $array['username'] != '' ){
	$status        = '290' ;

	$mysqli_staff  = $mysqli->query("SELECT staff_id, staff_idno, staff_username, staff_email, staff_mobileno FROM staff
                                     WHERE deleted_at IS NULL AND ( staff_date_resigned IS NULL OR staff_date_resigned = '0000-00-00' ) AND staff_idno = '".$array['staff_idno']."' AND staff_username = '".$array['username']."' LIMIT 1") ;

	if ( $mysqli_staff->num_rows > 0 ){
		$status       = '204' ;

		$row_staff    = $mysqli_staff->fetch_assoc() ;

		$password     = $row_staff['staff_id'].strPad( 6, rand(000000, 999999) ) ;
		$enc_password = passwordEncrypt( $password ) ;

		if ( $mysqli->query( "UPDATE staff SET 
							  staff_password = '".$enc_password."' 
							  WHERE staff_id = '".$row_staff['staff_id']."'" ) ){

			$status          = '208' ;

			$mailer          = new Mailer() ;
	        $mailer->from    = EMAILNOREPLY ;
	        $mailer->to      = [ $row_staff['staff_email'] ] ;
	        $mailer->subject = 'Reset password' ;
	        $mailer->body    = 'Your temporary password is ' . $password ;
	        if ( $mailer->send() ){
	        	$status      = '200' ;
	        }

		}

	}

}

require( $require_sub.'footer.php' ) ;
?>