<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    $password = mysqli_real_escape_string($con, trim($request->form_value->newpassword));
    $oldpassword = mysqli_real_escape_string($con, trim($request->form_value->oldpassword));
    $shop_id = mysqli_real_escape_string($con, trim($request->shop_id));
    $shop_code = mysqli_real_escape_string($con, trim($request->shop_code));
    
    include 'token_check.php';

    if($token_check)
    {
        $sel = $con->query("select password,hash_password from shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
        if($row = $sel->fetch_assoc())
        {
            $dpassword = $row['password'];
            $hpassword = $row['hash_password'];
        }
        if($dpassword == $oldpassword && $hpassword == md5($oldpassword))
        {
            $hashpassword = md5($password);
            $sql = "UPDATE shop_settings set password = '$password',hash_password = '$hashpassword' where shop_id='$shop_id' and shop_code='$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Password Updated');
                echo json_encode($rows);
            }
        }
        else
        {
            $rows = array('status'=>false,'message' => 'Password Error');
            echo json_encode($rows);
        }
    }
    else
    {
        $rows = array('status'=>"",'message' => 'Data Error');
        echo json_encode($rows);
    }

}

?>
