<?php
include 'dbconnect.php';
error_reporting(0);


if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // View button details
    if (isset($_POST['view_details'])) {

        $id = $_POST['row_id'];
        $view_sql = "SELECT branch.branch_name,branch.branch_id, account_type.name AS 'ac_name',account_type.code AS 'ac_code', account_info.`name`,
`father_name`, `mother_name`, `dob`, `occupation`, `mobile`, `email`, `nationality`, `present_address`,
 `permanent_address`, `gender`, `nominee_name`, `relation`, `share`, `picture`,
 `user_front`, `user_back`, `nominee_front`, `nominee_back`, `status`, `ac_number`, `sign`,`inform` FROM `account_info`  INNER JOIN branch ON account_info.branch = branch.branch_id INNER JOIN account_type ON account_info.ac_type = account_type.code WHERE account_info.id = $id";
        $view_sql_execute = $conn->query($view_sql);
    }

    // inform button details
    if (isset($_POST['inform_details_btn'])) {
        $id = $_POST['row_id'];


        $date_time = date('d/m/y h:m:s a');

        $select_sql = "SELECT account_info.id,branch.branch_name,branch.branch_id, account_type.name as 'ac_name',account_type.code as 'ac_code', account_info.`name`, `father_name`, `mother_name`, `dob`, `occupation`, `mobile`, `email`, `nationality`, `present_address`, `permanent_address`, `gender`, `nominee_name`, `relation`, `share`, `picture`, `user_front`, `user_back`, `nominee_front`, `nominee_back`, `status`, `ac_number`, `sign`,`nid_verified`,`face_verified`,`inform` FROM `account_info` INNER JOIN branch ON account_info.branch = branch.branch_id INNER JOIN account_type on account_info.ac_type = account_type.code WHERE `account_info`.id=$id";
        //  echo $select_sql;
        //  die();

        $single_result = $conn->query($select_sql);
    }

    // Open button details
    if (isset($_POST['open_details_btn'])) {
        $id = $_POST['row_id'];


        $date_time = date('d/m/y h:m:s a');

        $select_sql = "SELECT account_info.id,branch.branch_name,branch.branch_id, account_type.name as 'ac_name',account_type.code as 'ac_code', account_info.`name`, `father_name`, `mother_name`, `dob`, `occupation`, `mobile`, `email`, `nationality`, `present_address`, `permanent_address`, `gender`, `nominee_name`, `relation`, `share`, `picture`, `user_front`, `user_back`, `nominee_front`, `nominee_back`, `status`, `ac_number`, `sign`,`nid_verified`,`face_verified`,`inform` FROM `account_info` INNER JOIN branch ON account_info.branch = branch.branch_id INNER JOIN account_type on account_info.ac_type = account_type.code WHERE `account_info`.id=$id";
        //  echo $select_sql;
        //  die();

        $single_result = $conn->query($select_sql);
    }


    // after clicking Open button, nid,face and account UPDATE button
    if (isset($_POST['acc_update_btn'])) {

        $id = $_POST['id'];
        $nid_verified = $conn->real_escape_string(trim($_POST['nid_verified']));
        $face_verified = $conn->real_escape_string(trim($_POST['face_verified']));
        $acc_no = $conn->real_escape_string(trim($_POST['acc_number']));

        if (!empty($nid_verified) && !empty($face_verified)) {
            if (empty($acc_no)) {
                $update_sql = "UPDATE account_info SET nid_verified='$nid_verified',face_verified='$face_verified' WHERE id=$id";
                echo '<script>alert("Nid and Face Verified!!!");</script>';
            } else {

                $update_sql = "UPDATE account_info SET status = 'Approved', ac_number='$acc_no',nid_verified='$nid_verified',face_verified='$face_verified' WHERE id=$id";
                echo '<script>alert("Account Created!!!");</script>';
            }
        } else if (!empty($nid_verified) && empty($face_verified)) {
            if (!empty($acc_no)) {

                $update_sql = "UPDATE account_info SET nid_verified='$nid_verified' WHERE id=$id";
                echo '<script>alert("Nid Verified!!! \n Account Not Created !!! \n To create account you have verify NID and FACE both")</script>';
            } else {
                $update_sql = "UPDATE account_info SET nid_verified='$nid_verified' WHERE id=$id";
                echo '<script>alert("Nid Verified!!!")</script>';
            }
        } else if (empty($nid_verified) && !empty($face_verified)) {
            if (!empty($acc_no)) {

                $update_sql = "UPDATE account_info SET face_verified = '$face_verified' WHERE id=$id";
                echo '<script>alert("Face Verified!!! \n Account Not Created !!! \n To create account you have verify NID and FACE both")</script>';
            } else {
                $update_sql = "UPDATE account_info SET face_verified = '$face_verified' WHERE id=$id";
                echo '<script>alert("Face Verified!!!")</script>';
            }
        } else {
            echo '<script>alert("Please verify NID and Face verification first to update")</script>';
        }
        $acc_create = $conn->query($update_sql);
    }




    // $cust_informed = $conn->real_escape_string(trim($_POST['cust_informed']));

    // echo 'nid = '.$nid_verified.'face = '.$face_verified.'acc = '.$acc_no.'inform = '.$cust_informed;
    // die();


}

$user_sql = "SELECT * FROM `user_list`";
$user_sql_execute = $conn->query($user_sql);

$zone_sql = "SELECT DISTINCT(zone_id),`branch_div_details`.`zone` FROM `branch` INNER JOIN `branch_div_details` ON branch.branch_id = branch_div_details.branch_code";
$zone_sql_execute = $conn->query($zone_sql);