<?php

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

class Users extends MY_Controller {

    var $cats;
    var $thispage;

    public function __construct() {
        parent::__construct();
        $this->thispage = "users";
        $this->load->model('booking');
        $this->operator_id = $this->session->userdata('operator_id');
        if (get_ap('member', $this->operator_id) == 1) {
            
        } else {
            redirect("", "refresh");
        }
        if (!$this->session->userdata('group')) {
            echo "لايمكن الاستخدام ";
            $this->session->set_flashdata('msg', 'لايمكن التعديل عليه');
            ///echo $_SERVER['HTTP_REFERER'];
            redirect($_SERVER['HTTP_REFERER']);
            exit;
        }
    }

    public function index() {
        if (!get_ap('member', $this->operator_id)) {
            redirect("", "refresh");
        }
        $this->all(1);
    }

    public function all($page = 1) {

        $whr = "";


        $this->db->from('user');
        if ($whr)
            $this->db->where($whr);
        $max = 20;
        $data['all_count'] = ceil($this->db->count_all_results() / $max) + 1;




//echo print_r($this->db->last_query());

        $this->db->select('user.*,groups.groups as role_name');
        $this->db->from('user');
        $this->db->order_by('user.id', 'desc');
        if ($whr)
            $this->db->where($whr);
        //$this->db->limit($max, ($max * $page) - $max);
        $this->db->join('groups', 'groups.id = user.operator_id', 'left');
        $data['all_groups'] = $this->db->get()->result_array();
        $gruopname[1] = 'مدير';
        $gruopname[2] = 'مشرف';
        $gruopname[3] = 'عضو';
        $gruopname[4] = 'موقوف';
        $gruopname[7] = 'موقوف'; /*
          $i=0;
          foreach($data['all_groups'] as $row)
          {
          echo $data['all_groups'][$i]['gruop']=$gruopname[$row['gruop']] */
// $i++;

        $data['title'] = $this->lang->line('dashboard');
// $data['view'] = '../controllers/views/'.$this->thispage.'/show';
        $data['cats'] = $this->cats;
        $data['thispage'] = $this->thispage;
        $this->load->view('site/' . $this->thispage . '/show', $data);
    }

    public function rep($id, $page = 1, $noa = '') {
        if (!get_ap('member_rep', $this->operator_id)) {
            redirect("", "refresh");
        }
        $query = $this->db->get_where('user', array('id' => $id));
        $data['edit'] = $query->row_array();

        $whr = "user='" . $data['edit']['name'] . "'";


        /// $whr = " $whr and noa!='2' and noa!='3'";

        $this->db->from('rep');

        $this->db->like('user', $data['edit']['name']);


        if (!$noa)
            $this->db->not_like('comment', 'دخول');
        if (!$noa)
            $this->db->not_like('comment', 'خروج');
        if ($noa)
            $this->db->like('comment', 'دخول');
        if ($noa)
            $this->db->or_like('comment', 'خروج');
        if ($whr)
            $this->db->where($whr);
        $max = 35;
        $data['all_count'] = ceil($this->db->count_all_results() / $max);




///echo print_r($this->db->last_query());

        $this->db->from('rep');
        $this->db->like('user', $data['edit']['name']);
        if (!$noa)
            $this->db->not_like('comment', 'دخول');
        if (!$noa)
            $this->db->not_like('comment', 'خروج');


        if ($noa)
            $this->db->like('comment', 'دخول');
        if ($noa)
            $this->db->or_like('comment', 'خروج');
        $this->db->order_by('id', 'desc');
        if ($whr)
            $this->db->where($whr);
        //$this->db->limit($max, ($max * $page) - $max);
        $data['all_groups'] = $this->db->get()->result_array();
        foreach ($data['all_groups'] as $row) {
            //$data['all_groups']['allcat']=$this->db->from('cat')->where("parentcatid=".$row['catid'])->count_all_results();
        }
        $data['title'] = 'الشقق ';
        $data['id'] = $id;
        $data['noa'] = $noa;
        // $data['view'] = '../controllers/views/users/rep';

        $data['thispage'] = $this->thispage;

        $this->load->view('site/users/rep', $data);
    }

    public function add($cats = 1) {
        if (!get_ap('member_add', $this->operator_id)) {
            redirect("", "refresh");
        }
        $add = FALSE;
        $this->cats = $cats;
        if ($this->input->post('submit')) {
            $this->form_validation->set_rules('name', '', 'trim|required');
            $this->form_validation->set_rules('pass', '', 'trim|required');
            $this->form_validation->set_rules('email', '', 'trim|required|is_unique[user.email]');
            $this->form_validation->set_rules('role', '', 'trim|required');
            //	$this->form_validation->set_rules('comment', '', 'trim|required'); 
            //	$this->form_validation->set_rules('comm', '', 'trim|required');
            // 
//            $query = $this->db->get_where('user', array('email' => $this->input->post('email')));
//            $data['show'] = $query->row_array();
//            if ($data['show'] ['email']) {
//                $add = FALSE;
//                echo "الايميل مكرر ";
//            } else
            if ($this->form_validation->run() === FALSE) {

                $add = FALSE;
            } else {
                $add = TRUE;
                $data = array(
                    'operator_id' => $this->input->post('role'),
                    'name' => $this->input->post('name'),
                    'pass' => md5(md5($this->input->post('pass'))),
                    'email' => $this->input->post('email'),
                    'mosma' => $this->input->post('mosma'),
                    'phone' => $this->input->post('phone'),
                    'userInfo' => $this->input->post('userInfo'),
                    'phone' => $this->input->post('phone'),
                    'regdata' => time(),
                );
                $data = $this->security->xss_clean($data);
                $result = $this->db->insert('user', $data);
                if ($result) {
                    $this->session->set_flashdata('msg', 'تم اضافة القسم بفضل الله!');
                    redirect(base_url('users/index/'));
                }
            }
        }
        if (!$add) {

//$data['cats_select']  = $this->db->get()->result_array(); 

            $query = $this->db->get_where('user', array('id' => '1'));
            $data['edit'] = $query->row_array();
            $data['title'] = $this->lang->line('add-a-member');

            $data['thispage'] = $this->thispage;
            $data['view'] = '../controllers/views/' . $this->thispage . '/add';
            $this->load->view('site/' . $this->thispage . '/add', $data);
        }
    }

    public function del($id) {
        if (!get_ap('member_del', $this->operator_id)) {
            redirect("", "refresh");
        }
        $this->db->where('id', $id);
        $this->db->delete('user');
        //if (count($result) > 0) {
        $this->session->set_flashdata('msg', 'تم حذف  العضو بفضل الله!');
        redirect(base_url('users/'));
        //}
    }

    public function edit($id) {
        if (!get_ap('member_edit', $this->operator_id) && $id != $this->session->userdata('admin_id')) {
            redirect("", "refresh");
        }
        $query = $this->db->get_where('user', array('id' => $id));
        $data['edit'] = $query->row_array();
        $data['edit']['checked'] = array('', '', '', '', '');
        $data['edit']['checked'][$data['edit']['gruop']] = 'checked';

        $add = FALSE;
        if ($this->input->post('submit')) {
            $this->form_validation->set_rules('name', '', 'trim|required');

            $this->form_validation->set_rules('email', '', 'trim|required');
            // $this->form_validation->set_rules('gruop', '', 'trim|required');
            //	$this->form_validation->set_rules('comment', '', 'trim|required'); 
            //	$this->form_validation->set_rules('comm', '', 'trim|required'); 


            if ($this->form_validation->run() === FALSE) {

                $add = FALSE;
            } else {

                $add = TRUE;

                $pass = $data['edit']['pass'];
                if ($this->input->post('new_password') != '') {
                    $pass = md5(md5($this->input->post('new_password')));
                }


                $edit_data = array(
                    'name' => $this->input->post('name'),
                    'pass' => $pass,
                    'email' => $this->input->post('email'),
                    'mosma' => $this->input->post('mosma'),
                    'phone' => $this->input->post('phone'),
                    'userInfo' => $this->input->post('userInfo'),
                );

                if (!$this->session->userdata('teacher_id') && get_ap('member_edit', $this->operator_id)) {
                    $edit_data['operator_id'] = $this->input->post('role');
                }

                $edit_data = $this->security->xss_clean($edit_data);



                $result = $this->db->where('id', $id)->update('user', $edit_data);

                if ($result) {
                    $this->session->set_flashdata('msg', 'تم التعديل العضو بفضل الله!');
                    if (!$this->session->userdata('teacher_id') && get_ap('member_edit', $this->operator_id)) {
                        redirect(base_url('users/'));
                    } else {
                        redirect(base_url('users/edit/' . $id));
                    }
                }
            }
        }


        if (!$add) {
            $data['title'] = $this->lang->line('edit') . ' ' . $this->lang->line('member');

            $data['thispage'] = $this->thispage;
// $data['view'] = '../controllers/views/'.$this->thispage.'/edit';
            $this->load->view('site/' . $this->thispage . '/edit', $data);
        }
    }

    function upload_many_photos($field_name) {

        $config['upload_path'] = 'upload/';

        $config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|pdf|doc|docx|txt';

        $config['remove_spaces'] = TRUE;

        $config['encrypt_name'] = FALSE;

        $config['max_size'] = '200000';
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field_name)) {
            $error = array('error' => $this->upload->display_errors());
            echo"<pre>";
            print_r($config);
            echo"<pre>";
            print_r($error);
        } else {

            $data = $this->upload->data();
            $this->load->library('image_lib');

            //echo $this->upload->upload_path.$this->upload->file_name."<br>";

            $this->image_lib->clear();
        }
        return $data;
    }

    function approve() {
        if (!get_ap('inbox', $this->operator_id)) {
            redirect("", "refresh");
        }
        if ($this->input->post()) {
            if ($this->input->post('start_expiry')) {
                $date_arr = explode(' - ', $this->input->post('start_expiry'));
                $from = date('Y-m-d', strtotime($date_arr[0]));
                $to = date('Y-m-d', strtotime($date_arr[1]));
                $this->db->where(array('create_on >=' => $from, 'create_on <=' => $to));
            }
            if ($this->input->post('status') != -1) {
                $this->db->where('approve_status', $this->input->post('status'));
            }
        }
        $this->db->from('bills_user');
        $this->db->order_by('id', 'asc');
        $data['all_groups'] = $this->db->get()->result_array();
        $data['title'] = $this->lang->line('approve');
        // $data['view'] = '../controllers/views/'. $this->thispage .'/approve';
        $data['thispage'] = $this->thispage;
        $this->load->view('site/' . $this->thispage . '/approve', $data);
    }

    function approve_status($id, $status) {

        if ($status == 1) {
            $data['approve_status'] = 0;
            $this->db->where('user_tb_id', $id)->delete('bills_user');
        } elseif ($status == 0) {
            $data['approve_status'] = 1;
            $datas = $this->db->where('id', $id)->get('user')->row();
            $user['user_tb_id'] = $datas->id;
            $user['username'] = $datas->name;
            $user['email'] = $datas->email;
            $user['cid_user'] = $datas->cid_no;

            $user['sex'] = $datas->sex;
            $user['city'] = $datas->Country;
            $user['age'] = $datas->BirthDate;
            $user['phone'] = $datas->phone;
            $this->db->insert('bills_user', $user);
        } else {
            $data['approve_status'] = 0;
        }
        $this->db->where('id', $id);
        $this->db->update('user', $data);
        $datas = $this->db->where('id', $id)->get('user')->row();
        $user['user_tb_id'] = $datas->id;
        $user['username'] = $datas->name;
        $user['email'] = $datas->email;
        $user['cid_user'] = $datas->cid_no;
        if ($datas->sex == 'male') {
            $user['sex'] = '1';
        } else {
            $user['sex'] = '2';
        }
        $user['city'] = $datas->Country;
        $user['age'] = $datas->BirthDate;
        $user['phone'] = $datas->phone;


        redirect('users/approve', 'refresh');
    }

    function approve_status_student($id, $status) {
        if (!get_ap('student_approve', $this->operator_id)) {
            redirect("", "refresh");
        }
        if ($status == 1) {
            $data['approve_status'] = 0;
        } else {
            $data['approve_status'] = 1;
        }
        $this->db->where('id', $id)->update('bills_user', $data);
        $this->session->set_flashdata('success', 'success');
        redirect('users/approve', 'refresh');
    }

    function del_user($id) {
        if (!get_ap('inbox_delete_user', $this->operator_id)) {
            redirect("", "refresh");
        }
        $this->db->delete('bills_user', array('id' => $id));
        redirect('users/approve', 'refresh');
    }

    function inbox_multi_action() {
        $status = $this->input->post('status');
        $ids = $this->input->post('multi_action');
        foreach ($ids as $key => $value) {
            $this->db->update('bills_user', array('approve_status' => $status), array('id' => $key));
        }
        $this->session->set_flashdata('success', 'success');
        redirect('users/approve');
    }

    function change_member_status($id, $status) {
        $this->db->update('user', array('approve_status' => $status), array('id' => $id));
        $this->session->set_flashdata('msg', 'success');
        redirect('users');
    }

}

?>