<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\ReportModel;
use App\Models\UserModel;
use App\Models\MainpageModel;
use App\Models\VenueModel;

class Admin extends BaseController
{

    public function index()
    {
        $setting    = new SettingModel();
        $user       = new UserModel();
        $home       = new MainpageModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_users'] = $user->count_all_users_by_role('User');
        $data['count_project'] = $home->count_all_project();
        $data['count_donation'] = $home->count_all_donation_amt();
        $data['count_blog'] = $home->count_all_blog();
        $data['count_donation_month'] = $home->count_donation_month();
        $data['count_event'] = $home->count_event();
        $data['day_income_report'] =  $home->day_wsie_income_data();
        $all_dat = array();
        $month = date("t", strtotime(date('Y-m-d')));
        $data['alldatethismonth'] = range(1, $month);
        foreach($data['alldatethismonth'] as $date){
            $at =  date("Y-m-").$date;
            $all_dat[] = date('d-m', strtotime($at));
        }
        $data['dateList'] = "'" . implode ( "', '", $all_dat ) . "'";
        $product_sales_report = $home->get_all_donation_report();

        $prd_sale = array();
        $prd_name = array();
          if (!empty($product_sales_report[0])) 
          {
              for ($i = 0; $i < count($product_sales_report[0]); $i++) 
              {
                  if (!empty($product_sales_report[0][$i])) 
                  {
                    $prd_sale[] = $product_sales_report[0][$i]->total;
                    $prd_name[] = mb_strimwidth($product_sales_report[0][$i]->project_title, 0, 20);   
                  } 
                  

              }
          }
        $data['product_sale'] = "'" . implode ( "', '", $prd_sale ) . "'";
        $data['product_name'] = "'" . implode ( "', '", $prd_name ) . "'";

        $all_income = $setting->get_all_expance_and_income_by_category('Credit');
        $all_expance = $setting->get_all_expance_and_income_by_category('Debit');

        // echo '<pre>';
        // print_r($all_income);

        $expance = [];
        foreach ($all_expance as $key => $expance_d) 
        {
            if($expance_d->expense_amount)
            {
                $ex = $expance_d->expense_amount;
            }
            else
            {
                $ex = 0;
            }
            $expance[] = $ex;
        }

        $income = [];
        foreach ($all_income as $key => $income_d) 
        {
            if($income_d->expense_amount)
            {
                $in = $income_d->expense_amount;
            }
            else
            {
                $in = 0;
            }
            $income[] = $in;
        }

        $data['all_income'] = "'" . implode ( "', '", $income ) . "'";
        $data['all_expance'] = "'" . implode ( "', '", $expance ) . "'";

        echo view('admin/include/header', $data);
        echo view('admin/main');
        echo view('admin/include/footer');
        // echo '<pre>';
        // print_r($data['top_12_prd']);
    }

    function randColor( $numColors ) {
        $chars = "ABCDEF0123456789";
        $size =  strlen( $chars );
        $str = array();
        for( $i = 0; $i < $numColors; $i++ ) {
            $str[$i] = "#" ;
            for( $j = 0; $j < 6; $j++ ) {
                $str[$i] .= $chars[rand( 0, $size - 1 ) ];
            }
        }
        return $str;
    }

    public function get_aadress_detaile_by_pin_code($id)
    {
        if($id)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,'https://api.postalpincode.in/pincode/'.$id);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            $result = json_decode($response);
            curl_close($ch);

            $area= '';
            foreach ($result[0]->PostOffice as $key => $value)
            {
                 $area .= '<option value="'.$value->Name.'">'.$value->Name.'</option>'; 
            } 
            $output = [
                'city' => $result[0]->PostOffice[0]->District,
                'state' => $state = $result[0]->PostOffice[0]->State,
                'area' => $area,
            ];
        }
        echo json_encode($output);
    }

    public function get_aadress_detaile_by_pin_code_for_update($id, $post)
    {
        if($id)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,'https://api.postalpincode.in/pincode/'.$id);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            $result = json_decode($response);
            curl_close($ch);

            $area= '';
            foreach ($result[0]->PostOffice as $key => $value)
            {
                 $area .= '<option '.($value->Name == $post ? 'selected="selected"' : '').' value="'.$value->Name.'">'.$value->Name.'</option>'; 
            } 
            $output = [
                'city' => $result[0]->PostOffice[0]->District,
                'state' => $state = $result[0]->PostOffice[0]->State,
                'area' => $area,
            ];
        }
        echo json_encode($output);
    }

    public function check_email()
    {
        $user = new UserModel();
        $email = $this->request->getVar('email');
        echo $user->check_email_avaliblity($email);
    }
    public function check_mobile_avaliblity()
    {
        $user = new UserModel();
        $mobile = $this->request->getVar('mobile');
        echo $user->check_mobile_avaliblity($mobile);
    }

    function check_venue_avaliblity()
    {
        $venue = new VenueModel();
        $date = $this->request->getVar('date');
        echo $venue->check_venue_avaliblity($date);
    }

    public function check_mobile_no($mobile) 
    {

        $user = new UserModel();
        $data = $user->check_mobile_avaliblity($mobile);
        if ($data) {
            $arrayName = array(
                'msg' => '<span class="badge badge-danger">Mobile No Already Exits.</span>',
                'status' => 6,
            );
            echo json_encode($arrayName);
        } else {
            $arrayName = array(
                'msg' => '<span class="badge badge-success">Mobile No Available.</span>',
                'status' => 5,
            );
            echo json_encode($arrayName);
        }
    }

    public function check_email_id() 
    {
        $user = new UserModel();

        $email = $this->request->getVar('email');
        $data = $user->check_email_avaliblity($email);
        if ($data) {
            $arrayName = array(
                'msg' => '<span class="badge badge-danger">Email ID Already Exits.</span>',
                'status' => 6,
            );
            echo json_encode($arrayName);
        } else {
            $arrayName = array(
                'msg' => '<span class="badge badge-success">Email ID Available.</span>',
                'status' => 5,
            );
            echo json_encode($arrayName);
        }
    }

    public function check_mobile_for_student_add($mobile) 
    {

        $user = new UserModel();
        $data = $user->check_mobile_avaliblity_for_student_add($mobile);
        if ($data) 
        {
            $arrayName = array(
                'msg' => '<span class="badge badge-danger">Mobile No Already Exits.</span>',
                'status' => 6,
                'role' => $data->user_role,
                'id' => $data->user_rand_code
            );
            echo json_encode($arrayName);
        } 
        else 
        {
            $arrayName = array(
                'msg' => '<span class="badge badge-success">Mobile No Available.</span>',
                'status' => 5,
            );
            echo json_encode($arrayName);
        }
    }

    public function logout()
    {
        session()->destroy();
        return redirect()->to('login');
    }
}
