<?php

require_once "../../../config.php";
require_once "../../../validation.php";
require_once "../../../Helper.php";
require_once "../../../ajax/send_email.php";
require_once "../check_edit_page_ajax.php";


require_once '../../database1.php';
require_once '../check_edit_page_ajax.php';


class convertToInvoice extends database
{
    public function getCustomer($customerID)
    {
        return $this->fetchRow('user', ['id' => $customerID, 'user_type' => 'customer']);
    }

    public function current_count()
    {
        $where['cl_type'] = 'Invoice';
         return $this->fetchRow('tb_auto_incre', $where);
    }

    public function updateCustomer($customerID, $data)
    {
        $where['id'] = $customerID;
        return $this->update('user', $data, $where);
    }
}

if(!isset($_GET['id']) || !is_numeric($_GET['id']))
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'Customer Not Found'));
    exit;
}

$customerID = $_GET['id'];

$obj = new convertToInvoice();
$customer = $obj->getCustomer($customerID);
if(!$customer)
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'Customer Not Found'));
    exit;
}

$password = date("Hismyd") . rand(1000000, 9999999);
$data['password'] = password_hash($password, PASSWORD_DEFAULT);;
$update = $obj->updateCustomer($customerID, $data);


$body = '<table style="width:100%;border:1px solid lightgray"><tr><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px" colspan="2">Eretail - Password Reset</td></tr><tr><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px" colspan="2">Please use the password below to login to your account. </td></tr><tr><td colspan="2" style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">Password: ' . $password . '</td></tr>';


$body.= '</table>';

$total = 0;


$emailRes = send_email($customer['email'], $customer['email'], 'ERETAIL.LV - Reset Password', $body);


http_response_code(200);
echo json_encode(array('message' => 'Password Reset Successful'));
exit;
