<?php

define('ZAPIER', 'zapier');
require(APP_MODULES_PATH . 'zapier/libraries/REST_Controller.php');

class Estimate extends REST_Controller
{

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper(ZAPIER . '/zapier');

        if (!empty($this->input->get_request_header('client-secret-key')) && !empty($this->input->get_request_header('base-url'))) {
            $client_secret_key = $this->input->get_request_header('client-secret-key');
            $client_url = $this->input->get_request_header('base-url');
        } else {
            print_r(json_encode(['status' => false, 'message' => 'client_secret_key and base_url is missing']));
            die;
        }
        if (!zapier_check_api_authentication($client_secret_key) &&  $client_url != base_url()) {
            print_r(json_encode(['status' => false, 'message' => 'client_secret_key and base_url is incorrect']));
            die;
        }

        // if (!option_exists(ZAPIER . '_verification_id') || !option_exists(ZAPIER . '_verified') || 1 != get_option(ZAPIER . '_verified') || !option_exists(ZAPIER . '_last_verification')) {
        //     print_r(['Your perfex module is deactivated']);
        //     print_r(json_encode(['status' => false, 'message' => 'module of perfex is deactivated']));
        //     die;
        // }
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function index_get()
    {
        print_r(json_encode(['status' => true]));
        die;
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function create_post()
    {
        $posted_data = json_decode(file_get_contents('php://input'));

        $data = [];

        if ($posted_data) {
            if(isset($posted_data->clientid)){
                $data["clientid"] = get_company($posted_data->clientid);
            } else{
                print_r(json_encode(['message'=> 'Company Email is missing']));exit;
            }
            if(isset($posted_data->number)){
                $data['number'] = $posted_data->number;
            } else{
                print_r(json_encode(['message'=> 'Estimate Number is missing']));exit;
            }
            if(isset($posted_data->date)){
                $data['date'] = $posted_data->date;
            } else{
                print_r(json_encode(['message'=> 'Date is missing']));exit;
            }
            if(isset($posted_data->expirydate)){
                $data['expirydate'] = $posted_data->expirydate;
            }

            $data['prefix'] = get_option('estimate_prefix');
            $data['number_format'] = get_option('estimate_number_format');
            $data['currency'] = $this->clients_model->get_customer_default_currency($data['clientid']);


                $this->db->insert(db_prefix() . 'estimates', $data);
                $id = $this->db->insert_id();
                $posted_data->id = $id;
              
                print_r(json_encode($posted_data));
        } else {
            print_r(json_encode(['message'=> 'Something went wrong']));exit;
        }
    }

    // public function update_post()
    // {
    //     $posted_data = json_decode(file_get_contents('php://input'));
    //     $data = [];

    //     if ($posted_data) {
    //         if(isset($posted_data->clientid)){
    //             $data["clientid"] = get_company($posted_data->clientid);
    //         } else{
    //             print_r(json_encode(['message'=> 'Company Email is missing']));exit;
    //         }
    //         if(isset($posted_data->number)){
    //             $data['number'] = $posted_data->number;
    //         } else{
    //             print_r(json_encode(['message'=> 'Estimate Number is missing']));exit;
    //         } 
    //         if(isset($posted_data->date)){
    //             $data['date'] = $posted_data->date;
    //         } else{
    //             print_r(json_encode(['message'=> 'Date is missing']));exit;
    //         }
    //         if(isset($posted_data->expirydate)){
    //             $data['expirydate'] = $posted_data->expirydate;
    //         }   
            
    //         $clientId = $this->db->where('number', $posted_data->number);
    //         $this->db->limit(1);            
    //         $clientId = $this->db->get('estimates')->row('id');
            
    //         $this->load->model('estimates_model');
    //         $id = $this->estimates_model->update($data, $clientId);
            
    //             $posted_data->id = $id;
              
    //             print_r(json_encode($posted_data));
    //     } else {
    //         print_r(json_encode(['message'=> 'Something went wrong']));exit;
    //     }
    // }
    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function createHook_post($hooktype)
    {
        zapier_add_hook($hooktype, $_GET['url']);
        print_r(json_encode(['status' => true, 'message' => 'subscribed successfully']));
        die;
    }

    /**
     * Get All Data from this method
     *
     * @return Response
     */
    public function removeHook_delete()
    {
        zapier_delete_hook($_GET['url']);
        print_r(json_encode(['status' => true, 'message' => 'unsubscribed successfully']));
        die;
    }


}
