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

class Creditcard extends CI_Controller
{
    public function index()
    {
       
        	$settings = apiCall('getSettings');
			
			$settings=$settings['details']['settings'];
			
			$this->session->set_userdata('settings',$settings);
			
			
        $profiledata = apiCall('GetProfile');
        $getloc = json_decode(file_get_contents("http://ipinfo.io/"));
        $merchantinfo = $this->session->userdata('merchantInfo');

        $data = [
            'cc_id' => isset($_GET['id'])?$_GET['id']:'',
            'user_token' => $this->session->userdata('token'),
            'lat' => $merchantinfo['latitude'],
            'lng' => $merchantinfo['lontitude']
        ];
      
        $response = apiCall('getCedittCardInfo',$data,'');
       
        $OrderList = apiCall('OrderList');
        $addressbook = apiCall('AddressBookList');
        $BookingList = apiCall('BookingList',['tab'=>'all']);
       
        renderView($this, 'creditcard/index', ['bookingList'=>$BookingList['details'],'addresslist' => $addressbook['details'],'profileData' => $profiledata,'OrderList' => $OrderList['details'],'creditdata' => $response['details']]);

    }

    public function Createcredit_card()
    {

        $getloc = json_decode(file_get_contents("http://ipinfo.io/"));
        $coordinates = explode(",", $getloc->loc);
        $this->form_validation->set_rules('card_name', 'Card Name', 'required');
        $this->form_validation->set_rules('credit_card_number', 'Credit card number', 'required');
        $this->form_validation->set_rules('billing_address', 'Billing Address', 'required');
        $this->form_validation->set_rules('cvv', 'Cvv', 'required');
        $this->form_validation->set_rules('expiration_month', 'Expiration Month', 'required');
        $this->form_validation->set_rules('expiration_yr', 'Expiration Year', 'required');
        if (isset($_POST) && $_POST) {
            if ($this->form_validation->run() == TRUE) {
            $data = [
                'cc_id' => isset($_POST['id'])?$_POST['id']:'',
                'card_name' => $_POST['card_name'],
                'credit_card_number' => $_POST['credit_card_number'],
                'billing_address' => $_POST['billing_address'],
                'cvv' => $_POST['cvv'],
                'expiration_month' => $_POST['expiration_month'],
                'expiration_yr' => $_POST['expiration_yr'],
                'lat' => $coordinates[0],
                'lng' => $coordinates[1],
                'user_token' => $this->session->userdata('token'),
            ];
            $response = apiCall('saveCreditCard', '', $data);
            if ($response['code'] == 1) {
                if($_POST['id']){
                    $this->session->set_flashdata('success', 'Card Changed successfully');
                }else{
                $this->session->set_flashdata('success', 'Card Added successfully');
                }
                redirect('profile');
            } else {
                $this->session->set_flashdata('error', $response['msg']);
                redirect('profile');
            }

        }else
            {
                $this->session->set_flashdata('error','All Fields are required');
                redirect('profile');
            }}

    }

    public function deletecredit()
    {
        $data=[
            'id' => $_GET['id']
        ];
        $response = apiCall('DeleteCreditCard',$data,'');
        if($response['code'] == 1)
        {
            $this->session->set_flashdata('success', $response['msg']);
            redirect('profile');
        }else{
            $this->session->set_flashdata('error', $response['msg']);
            redirect('profile');
        }
    }


    public function getcreditlist()
    {
        $credit = apiCall('CrediCartList');
        if($credit['code']==1){
            foreach ($credit['details']['data'] as $key => $row)
            {
                $credit['details']['data'][$key]['action']= '<a class="edit_credit" href="creditcard?id='.$row['id'].'"><i style="font-size: 25px;" class="ion-ios-compose-outline"></i></a> <a href="creditcard/deletecredit?id='.$row['id'].'" class="delete_credit"><i style="font-size: 25px;" class="ion-ios-trash"></i></a>';
            }
        }

        echo json_encode($credit['details']);
    }
}