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

class Contact extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Contact_Model');
    }
    function Create()
    {
        $data = array();
        $data['Contact'] = $this->Contact_Model->Get_AllContact();
        $this->load->view('Contact/CreateContact', $data);
    }

    function CreateContact()
    {
        $array = array();
        $Fields = array(
            'contact_firstName' => $this->input->post('firstName', true),
            'contact_lastName' => $this->input->post('lastName', true),
            'contact_companyName' => $this->input->post('companyName', true),
            'contact_Tell' => $this->input->post('Tell', true),
            'contact_Mobile' => $this->input->post('Mobile', true),
            'contact_Address' => $this->input->post('Address', true),
            'contact_Email' => $this->input->post('Email', true),
            'contact_Telegram' => $this->input->post('Telegram', true),
            'contact_whatsApp' => $this->input->post('whatsApp', true),
            'contact_Type' => $this->input->post('Type', true),
        );
        if (!empty($Fields['contact_firstName']) && !empty($Fields['contact_lastName']) && !empty($Fields['contact_Tell']) && !empty($Fields['contact_Mobile'])&& !empty($Fields['contact_Type'])) {
            $retrieve = $this->Contact_Model->Create_Contact($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 EditContact()
    {
        $array = array();
        $Fields = array(
            'contact_firstName' => $this->input->post('firstNameEdit', true),
            'contact_lastName' => $this->input->post('lastNameEdit', true),
            'contact_companyName' => $this->input->post('companyNameEdit', true),
            'contact_Tell' => $this->input->post('TellEdit', true),
            'contact_Mobile' => $this->input->post('MobileEdit', true),
            'contact_Address' => $this->input->post('AddressEdit', true),
            'contact_Email' => $this->input->post('EmailEdit', true),
            'contact_Telegram' => $this->input->post('TelegramEdit', true),
            'contact_whatsApp' => $this->input->post('whatsAppEdit', true),
            'contact_Type' => $this->input->post('TypeEdit', true),
        );
        if (!empty($Fields['contact_firstName']) && !empty($Fields['contact_lastName']) && !empty($Fields['contact_Tell']) && !empty($Fields['contact_Mobile'])) {
            $retrieve = $this->Contact_Model->Edit_Contact($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_Contact_ByID()
    {
        $contact_Id = $this->input->post('id', true);
        $retrieve = $this->Contact_Model->Get_Contact_ByID($contact_Id);
        $array = array(
            $retrieve->contact_Id,
            $retrieve->contact_firstName,
            $retrieve->contact_lastName,
            $retrieve->contact_companyName,
            $retrieve->contact_Tell,
            $retrieve->contact_Mobile,
            $retrieve->contact_Address,
            $retrieve->contact_Email,
            $retrieve->contact_Telegram,
            $retrieve->contact_whatsApp,
            $retrieve->contact_Type,
        );
        echo json_encode($array);
    }

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

}
