<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('User_Model');
    }

    function index()
    {
        $nowDate = $this->NowDate();
        /*
                $data['PCount'] = $this->Anbar_Model->Get_AllProduct_Count();
                $data['ImportationCount'] = $this->Anbar_Model->Get_AllImportationNow_Count($nowDate);
                $data['ExportCount'] = $this->Anbar_Model->Get_AllExportNow_Count($nowDate);
                $data['StoreKeepersCount'] = $this->Anbar_Model->Get_StoreKeepers_Count();*/
        $this->load->view('Dashboard/index');
    }

    function Setting()
    {
        $this->load->view('User/Setting');
    }

    function Create()
    {
        $data = array();
        $data['User'] = $this->User_Model->Get_AllUser();
        $this->load->view('User/CreateUser', $data);
    }

    function Edit_Img()
    {
        $data = array();
        $array = array();
        $userImage = $_FILES['userImage']['name'];
        if (!empty($userImage)) {
            $config['upload_path'] = 'Assets/userImg/';
            $config['allowed_types'] = 'jpg|png|jpeg';
            $config['max_size'] = 10000;
            $config['max_width'] = 2024;
            $config['max_height'] = 1768;
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('userImage')) {
                array_push($array, "image_upload_failed");
            } else {
                $data['user_Img'] = $this->upload->data('file_name');
                $retrieve = $this->User_Model->Edit_UserImg($this->session->userdata('user_id'), $data);
                if ($retrieve) {
                    if ($this->session->userdata('user_Img') != "" && $this->session->userdata('user_Img') != "no_pic.jpg")
                        unlink('Assets/userImg/' . $this->session->userdata('user_Img'));
                    $user_session = array(
                        'user_Img' => $data['user_Img']
                    );
                    $this->session->set_userdata($user_session);
                    array_push($array, "Edited");
                } else {
                    array_push($array, "not_Edited");
                }
            }
        } else {
            array_push($array, "empty");
        }
        sleep(3);
        echo json_encode($array);
    }

    function Edit_Pass()
    {
        $array = array();
        $password = $this->input->post('Pass', true);
        $newPass = $this->input->post('newPass', true);
        $newPassRepeat = $this->input->post('newPassRepeat', true);
        if (!empty($password) && !empty($newPass) && !empty($newPassRepeat)) {
            if ($newPass != $newPassRepeat) {
                array_push($array, "not_match");
            } else {
                if (md5($password) != $this->session->userdata('user_Password')) {
                    array_push($array, "not_CurrentMatch");
                } else {
                    $retrieve = $this->User_Model->Edit_UserPass($this->session->userdata('user_id'), $newPass);
                    if ($retrieve) {
                        $user_session = array(
                            'user_Password' => md5($newPass)
                        );
                        $this->session->set_userdata($user_session);
                        array_push($array, "Edited");
                    } else {
                        array_push($array, "not_Edited");
                    }
                }
            }
        } else {
            array_push($array, "empty");
        }
        sleep(3);
        echo json_encode($array);
    }

    function Edit_Info()
    {
        $Fields = array(
            'user_firstName' => $this->input->post('firstName', true),
            'user_lastName' => $this->input->post('lastName', true),
            'user_Mobile' => $this->input->post('Mobile', true)
        );
        $array = array();
        if (!empty($Fields['user_firstName']) && !empty($Fields['user_lastName']) && !empty($Fields['user_Mobile'])) {
            $retrieve = $this->User_Model->Edit_UserInfo($this->session->userdata('user_id'), $Fields);
            if ($retrieve) {
                $user_session = array(
                    'user_firstName' => $Fields['user_firstName'],
                    'user_lastName' => $Fields['user_lastName'],
                    'user_Mobile' => $Fields['user_Mobile'],
                );
                $this->session->set_userdata($user_session);
                array_push($array, "Edited");
            } else {
                array_push($array, "not_Edited");
            }

        } else {
            array_push($array, "empty");
        }

        sleep(3);
        echo json_encode($array);
    }

    function CreateUser()
    {
        $array = array();
        $Fields = array(
            'user_firstName' => $this->input->post('firstName', true),
            'user_lastName' => $this->input->post('lastName', true),
            'user_Mobile' => $this->input->post('Mobile', true),
            'user_userName' => $this->input->post('userName', true),
            'user_Password' => $this->input->post('Password', true),
            'user_Access' => $this->input->post('Access', true)
        );
        if (!empty($Fields['user_firstName']) && !empty($Fields['user_lastName']) && !empty($Fields['user_Mobile']) && !empty($Fields['user_userName']) && !empty($Fields['user_Password'])) {
            $Fields['user_Password'] = md5($Fields['user_Password']);
            $check_userName = $this->User_Model->Check_ExistUser_userName($Fields['user_userName']);
            if ($check_userName) {
                array_push($array, "existUser");
            } else {
                $userImage = $_FILES['userImage']['name'];
                if (!empty($userImage)) {
                    $config['upload_path'] = 'Assets/userImg/';
                    $config['allowed_types'] = 'jpg|png|jpeg';
                    $config['max_size'] = 10000;
                    $config['max_width'] = 2024;
                    $config['max_height'] = 1768;
                    $this->load->library('upload', $config);
                    if (!$this->upload->do_upload('userImage')) {
                        array_push($array, "image_upload_failed");
                    } else {
                        $Fields['user_Img'] = $this->upload->data('file_name');
                        $retrieve = $this->User_Model->Create_User($Fields);
                        if ($retrieve) {
                            array_push($array, "Created");
                        } else {
                            array_push($array, "not_Created");
                        }
                    }
                } else {
                    $Fields['user_Img'] = 'no_pic.jpg';
                    $retrieve = $this->User_Model->Create_User($Fields);
                    if ($retrieve) {
                        array_push($array, "Created");
                    } else {
                        array_push($array, "not_Created");
                    }
                }
            }
        } else {
            array_push($array, "empty");
        }
        sleep(3);
        echo json_encode($array);
    }

    function EditUser()
    {
        $array = array();
        $Fields = array(
            'user_firstName' => $this->input->post('firstNameEdit', true),
            'user_lastName' => $this->input->post('lastNameEdit', true),
            'user_Mobile' => $this->input->post('MobileEdit', true),
            'user_userName' => $this->input->post('userNameEdit', true),
            'user_Password' => $this->input->post('PasswordEdit', true),
            'user_Access' => $this->input->post('AccessEdit', true)
        );
        if (!empty($Fields['user_firstName']) && !empty($Fields['user_lastName']) && !empty($Fields['user_Mobile']) && !empty($Fields['user_userName']) && !empty($Fields['user_Password'])) {
            $Fields['user_Password'] = md5($this->input->post('PasswordEdit', true));
            if ($Fields['user_userName'] != $this->input->post('userNameEdit1', true)) {
                $check_userName = $this->User_Model->Check_ExistUser_userName($Fields['user_userName']);
                if ($check_userName) {
                    array_push($array, "existUser");
                } else {
                    $retrieve = $this->User_Model->Edit_User($this->input->post('id', true), $Fields);
                    if ($retrieve) {
                        array_push($array, "Edited");
                    } else {
                        array_push($array, "not_Edited");
                    }
                }
            } else {
                $retrieve = $this->User_Model->Edit_User($this->input->post('id', true), $Fields);
                if ($retrieve) {
                    array_push($array, "Edited");
                } else {
                    array_push($array, "not_Edited");
                }
            }

        } else {
            array_push($array, "empty");
        }
        sleep(3);
        echo json_encode($array);
    }

    function Get_User_ByID()
    {
        $user_Id = $this->input->post('id', true);
        $retrieve = $this->User_Model->Get_User_ByID($user_Id);
        $array = array(
            $retrieve->user_Id,
            $retrieve->user_firstName,
            $retrieve->user_lastName,
            $retrieve->user_Mobile,
            $retrieve->user_userName,
            $retrieve->user_Access,
            base_url('Assets/userImg/') . $retrieve->user_Img
        );
        echo json_encode($array);
    }

    function DeleteUser()
    {
        $id = $this->input->post('id', true);
        $retrieve = $this->User_Model->Delete_User($id);
        if ($retrieve) echo "Deleted";
        else echo "not_Deleted";
    }

    function BanUser()
    {
        $id = $this->input->post('id', true);
        $ban = $this->input->post('ban', true);
        $retrieve = $this->User_Model->Ban_User($id, $ban);
        if ($retrieve) echo "Edited";
        else echo "not_Edited";
    }
    function LogOut()
    {
        $retrieve = $this->User_Model->Set_UserLogout($this->user_id, $this->NowDateTime());
        if ($retrieve) {
            $this->session->sess_destroy();
            redirect(base_url());
        } else {
            redirect(base_url());
        }

    }

}
