<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Customers
 *
 *
 * @package		Repairer
 * @category	Controller
 * @author		Usman Sher
*/

// Includes all customers controller

class Customers extends Auth_Controller
{
	// THE CONSTRUCTOR //
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Customers_model');
        $this->upload_path = 'assets/uploads/images';
        $this->image_types = 'gif|jpg|jpeg|png|tif';
        $this->allowed_image_size = 10000;
        $this->load->library('upload');
    }

	// PRINT A CUSTOMERS PAGE //
    public function index()
    {
        $this->mPageTitle = lang('clients');
        $this->repairer->checkPermissions('index');
        $this->render('clients/index');
    }

	// GENERATE THE AJAX TABLE CONTENT //
    public function getAllCustomers()
    {
        $this->repairer->checkPermissions('index');
        $this->load->library('datatables');
        $this->datatables
            ->select('id, name, company, address, email, telephone, image')
            ->from('clients');


        $actions = '<div class="text-center"><div class="btn-group text-left">'
            . '<button type="button" class="btn btn-default btn-xs btn-primary dropdown-toggle" data-toggle="dropdown">'
            . ('actions') . ' <span class="caret"></span></button>
        <ul class="dropdown-menu pull-right" role="menu">';
        if ($this->Admin || $this->GP['customers-view_customer']) {
            $actions .= "<li><a data-dismiss='modal' class='view_client' href='#view_client' data-toggle='modal' data-num='$1'><i class='fas fa-check'></i> ".lang('view_client')."</a></li>";
        }

        if ($this->Admin || $this->GP['customers-edit']) {
            $actions .= "<li><a data-dismiss='modal' id='modify_client' href='#clientmodal' data-toggle='modal' data-num='$1'><i class='fas fa-edit'></i> ".lang('edit_client')."</a></li>";
        }

        if ($this->Admin || $this->GP['customers-delete']) {
            $actions .= "<li><a id='delete_client' data-num='$1'><i class='fas fa-trash'></i> ".lang('delete_client')."</a></li>";
        }
        $actions .= "<li><a id='view_image' data-num='$2'><i class='fas fa-image'></i> ".lang('view_image')."</a></li>";

        $actions .= '</ul></div>';


        $this->datatables->add_column('actions', $actions, 'id, image');
        $this->datatables->unset_column('id');
        $this->datatables->unset_column('image');
        echo $this->datatables->generate();
    }

    // PRINT A CUSTOMERS PAGE //
    public function view_repairs($id=NULL)
    {
        $this->mPageTitle = lang('clients');
        // $this->repairer->checkPermissions('index');
        $data['settings'] = $this->mSettings;
        $data['id'] = $id;
        $this->load->view($this->theme . 'clients/getRepairs', $data);
    }

    // GENERATE THE AJAX TABLE CONTENT //
    public function getClientRepairs($id = NULL)
    {
        $this->repairer->checkPermissions('index');
        $this->load->library('datatables');
        $this->datatables
            ->select('reparation.id as id, code, name, reparation.imei as imei, telephone, defect, model_name, date_opening, a.first_name, (SELECT CONCAT(first_name, " ", last_name) FROM users WHERE reparation.updated_by = users.id) as modified_by, (SELECT COUNT(attachments.id) FROM attachments WHERE reparation_id=reparation.id) as attached, grand_total')
            ->join('users a', 'a.id=reparation.created_by', 'left')
            ->where('client_id', $id)
            ->from('reparation');
        echo $this->datatables->generate();
    }
	
    
	// ADD A CUSTOMER //
    public function add()
    {

        $this->repairer->checkPermissions();

        $name = $this->input->post('name', true);
        $company = $this->input->post('company', true);
        $address = $this->input->post('address', true);
        $city = $this->input->post('city', true);
        $postal_code = $this->input->post('postal_code', true);
        $telephone = $this->input->post('telephone', true);
        $email = $this->input->post('email', true);
        $comment = $this->input->post('comment', true);
        $vat = $this->input->post('vat', true);
        $cf = $this->input->post('cf', true);
		
        $data = array(
            'name' => $name,
            'company' => $company,
            'telephone' => $telephone,
            'address' => $address,
            'city' => $city,
            'postal_code' => $postal_code,
            'email' => $email,
            'date' => date('Y-m-d H:i:s'),
            'comment' => $comment,
            'vat' => $vat,
            'cf' => $cf,
            'image' => null,
        );

        $error = null;
        if (isset($_FILES['image'])) {
            if ($_FILES['image']['size'] > 0) {
                $config['upload_path'] = $this->upload_path;
                $config['allowed_types'] = $this->image_types;
                $config['max_size'] = $this->allowed_image_size;
                $config['max_width'] = $this->mSettings->iwidth;
                $config['max_height'] = $this->mSettings->iheight;
                $config['overwrite'] = FALSE;
                $config['max_filename'] = 25;
                $config['encrypt_name'] = TRUE;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload('image')) {
                    $error = $this->upload->display_errors();
                }else{
                    $upload_file = $this->upload->file_name;
                    $data['image'] = $upload_file;
                    $config = NULL;
                }
            }
        }

        $id = $this->Customers_model->insert_client($data);
        echo $this->repairer->send_json(array('id'=>$id, 'error'=>$error));
    }

	// EDIT CUSTOMER //
    public function edit()
    {

        $this->repairer->checkPermissions();
        $id = $this->input->post('id', true);
        $name = $this->input->post('name', true);
        $company = $this->input->post('company', true);
        $address = $this->input->post('address', true);
        $city = $this->input->post('city', true);
        $postal_code = $this->input->post('postal_code', true);
        $telephone = $this->input->post('telephone', true);
        $email = $this->input->post('email', true);
        $comment = $this->input->post('comment', true);
        $vat = $this->input->post('vat', true);
        $cf = $this->input->post('cf', true);
        $data = array(
            'name' => $name,
            'company' => $company,
            'telephone' => $telephone,
            'address' => $address,
            'city' => $city,
            'postal_code' => $postal_code,
            'email' => $email,
            'comment' => $comment,
            'vat' => $vat,
            'cf' => $cf
        );

        $error = null;
        if (isset($_FILES['image'])) {
            if ($_FILES['image']['size'] > 0) {
                $config['upload_path'] = $this->upload_path;
                $config['allowed_types'] = $this->image_types;
                $config['max_size'] = $this->allowed_image_size;
                $config['max_width'] = $this->mSettings->iwidth;
                $config['max_height'] = $this->mSettings->iheight;
                $config['overwrite'] = FALSE;
                $config['max_filename'] = 25;
                $config['encrypt_name'] = TRUE;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload('image')) {
                    $error = $this->upload->display_errors();
                }else{
                    $upload_file = $this->upload->file_name;
                    $data['image'] = $upload_file;
                    $config = NULL;
                }
            }
        }
        $this->Customers_model->edit_client($id, $data);
        echo $this->repairer->send_json(array('id'=>$id, 'error'=>$error));
    }

	// DELETE CUSTOMER 
    public function delete()
    {
        $this->repairer->checkPermissions();
		$id = $this->security->xss_clean($this->input->post('id', true));
        $data = $this->Customers_model->delete_clients($id);
        echo json_encode($data);
    }

public function delete_image()
    {
        $id = $this->security->xss_clean($this->input->post('id', true));
        $this->db->where('id', $id);
        $this->db->update('clients', array('image'=>null));
        echo $this->repairer->send_json(array('success'=>true));
    }
	// GET CUSTOMER AND SEND TO AJAX FOR SHOW IT //
    public function getCustomerByID()
    {
        $id = $this->security->xss_clean($this->input->post('id', true));
		$data = $this->Customers_model->find_customer($id);
		$token = $this->input->post('token', true);
		// if($_SESSION['token'] != $token) die('CSRF Attempts');
        echo json_encode($data);
    }
}