<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$uid = strip_tags(mysqli_real_escape_string($con, $data['uid']));
$type = $data['type'];
$mode = $data['mode'];

if ($type == '' || $uid == '') {
    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} else if ($type == 0) {
    //save
    if ($mode == 0) {
        $name = strip_tags(mysqli_real_escape_string($con, $data['name']));
        $mobile = strip_tags(mysqli_real_escape_string($con, $data['mobile']));
        $password = strip_tags(mysqli_real_escape_string($con, $data['password']));

        $role = strip_tags(mysqli_real_escape_string($con, $data['role']));

        $check_user_exist = $con->query("select * from app_users where user_name='$name' and status='1'  ")->num_rows;

        if ($check_user_exist == 0) {
            $new_app_user_sql = "insert into app_users(
                `user_name`, `mobile`,`password`,`type`)values(
                '$name','$mobile','$password','$role')";

            $result = $con->query($new_app_user_sql);
            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "New app user Added");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        } else {
            $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "User name already exists");

        }




    }
    //edit
    else if ($mode == 1) {
        $userid = strip_tags(mysqli_real_escape_string($con, $data['userid']));
        $name = strip_tags(mysqli_real_escape_string($con, $data['name']));
        $mobile = strip_tags(mysqli_real_escape_string($con, $data['mobile']));
        $password = strip_tags(mysqli_real_escape_string($con, $data['password']));
        $role = strip_tags(mysqli_real_escape_string($con, $data['role']));

        $update_app_user_sql = "update app_users set 
                                    `user_name`='$name',`mobile`='$mobile',`password`='$password',
                                    `type`='$role'
                                        where id = '$userid'";

        $result = $con->query($update_app_user_sql);
        if ($result)
            $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "App user updated");
        else
            $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");

    }
    //block /delete user
    else if ($mode == 2) {
        $userid = strip_tags(mysqli_real_escape_string($con, $data['userid']));

        $block_sql = "update app_users set 
                `status`='2'
            where id = '$userid'";

        $result = $con->query($block_sql);
        if ($result) {
            $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "User block successfully");
        } else
            $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");

    }

}
echo json_encode($returnArr);
mysqli_close($con);
?>