<?php

namespace App\Controllers\Admin;
use App\Controllers\BaseController;
// use App\Models\UserModel;
use App\Models\CustomerModel;

class Crm extends BaseController
{
    public function dashboard()
    {
         $customerModel = new CustomerModel();

        $data['customers'] = $customerModel
            ->where('c_status !=','deleted')
            ->orderBy('cid', 'DESC')
            ->findAll();

        return view('admin/crm/crmdashboard',$data);
    }
    
    public function addcustomer()
    {
        $id='';
         $data = [
            'customer_id'=>$id,
            'customer_code'=>'CUS0123',
            'company_cr'=>'12345678',
            'company_name'=>'Al Baqai Builder',
            'company_address'=>'address: daflkdsjflkasdjflkaj',
            'contact_person'=>'Mohammad',
            'mobile'=>'053275531',
            'email'=>'info@domain.com',
            'vat'=>'12345678',
            'city'=>'Riyadh',
            'credit_limit'=>'50,000',
            'credit_duration'=>'15',
            'balance'=>'5,000',
        ];
        $hasVal = ($id>0) ? $data : 
         $data = [
            'customer_id'=>"",
            'customer_code'=>'',
            'company_cr'=>'',
            'company_name'=>'',
            'company_address'=>'',
            'contact_person'=>'',
            'mobile'=>'',
            'email'=>'',
            'vat'=>'',
            'city'=>'',
            'credit_limit'=>'',
            'credit_duration'=>'30',
            'balance'=>'',
        ]; ;
        return view('admin/crm/addcustomer',$hasVal);
    }

    public function updateCustomer($cid){
        // $data = [
        //     'customer_id'=>"",
        //     'customer_code'=>'',
        //     'company_cr'=>'',
        //     'company_name'=>'ABC',
        //     'company_address'=>'',
        //     'contact_person'=>'',
        //     'mobile'=>'',
        //     'email'=>'',
        //     'vat'=>'',
        //     'city'=>'',
        //     'credit_limit'=>'',
        //     'credit_duration'=>'30',
        //     'balance'=>'',
        // ];
        $customerModel = new CustomerModel();
        $data['customer'] = $customerModel->find($cid);

            return view('admin/crm/addcustomer',$data);
    }
    public function viewcustomer($cid)
    {
        $customerModel = new CustomerModel();
        
        $customer = $customerModel->find($cid);
        
        if (!$customer) {
        throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        return view('admin/crm/template-view-customer',['customer'=>$customer]);
    }
    public function save() {
    $customerModel = new CustomerModel();

        $cid = $this->request->getpost('cid');
        
            $data = [

                'c_fname'        => $this->request->getPost('fn'),
                'c_lname'        => $this->request->getPost('ln'),
                'c_email'        => $this->request->getPost('e'),
                'c_mobile'       => $this->request->getPost('m'),
                'c_cname'        => $this->request->getPost('cn'),
                'c_ccr'          => $this->request->getPost('cr'),
                'c_cvat'         => $this->request->getPost('vat'),
                'c_ccity'        => $this->request->getPost('city'),
                'c_cna'          => $this->request->getPost('na'),
                'c_ccrlimit'     => $this->request->getPost('cl'),
                'c_ccrduration'  => $this->request->getPost('duration'),
                'c_status'       => $this->request->getpost('customerstatus'),
            ];
            // Check CID Already Exist 
            // IF EXIST Update
            
            if (!empty($cid)) {
                $customerExists = $customerModel->find($cid,$data['c_cvat']);
                if ($customerExists) {
                    $customerModel->update($cid,$data);
                    return redirect()
                        ->back()
                        ->with('success','customer Update Successfully');
                }
            }

            // Else Create New 
            
                $data['c_status'] = 'active';
                $data['c_quotes'] = 0;
                $data['c_invoices'] = 0;
                $data['c_date_creation'] = date('Y-m-d H:i:s');
            if ($customerModel->insert($data))
            {
                return redirect()
                    ->back()
                    ->with('success', 'Customer created successfully.');
            }

            return redirect()
                ->back()
                ->with('error', 'Failed to create customer.');
        
    }
    public function deletecustomer($cid){
        $customerModel = new CustomerModel();
        $customer = $customerModel->find($cid);
        if(!$customer){
            return redirect()
            ->back()
            ->with('error','Customer Not Found');
        }
        $customerModel->update(
            $cid,['c_status'=>'deleted']
        );
        return redirect()
            ->back()
            ->with('success',"Customer Successfully Deleted");
    }
  
}