<?php

namespace App\Services;

use App\Customer;

class CustomerService
{
    public function getCustomerInfo($customer_noic){
        $customer_data = Customer::where('noic','=',$customer_noic)->first();

        return $customer_data;
    }

    public function createNewCustomer($data){

        $customer_last_id = Customer::latest()->first();

        // dd($data['warganegara']);
        

        if($customer_last_id['id'] == null){
            $num = 1;
            $count= sprintf("%08d", $num);
        }else{
            $num = $customer_last_id['id'] + 1;
            $count= sprintf("%08d", $num);
        }

        if($data['bangsa']== 'melayu'){
            $no_pelanggan = 'M'.substr($data['noic'],6,2).'-'.$count;
        }else if($data['bangsa']== 'cina'){
            $no_pelanggan = 'C'.substr($data['noic'],6,2).'-'.$count;
        }else if($data['bangsa']== 'india'){
            $no_pelanggan = 'I'.substr($data['noic'],6,2).'-'.$count;
        }else if($data['bangsa']== 'lain'){
            $no_pelanggan = 'L'.substr($data['noic'],6,2).'-'.$count;
        }

        if($data['warganegara'] == 'warganegara'){
            $warganegara = 1;
        }else{
            $warganegara = 0;
        }
        if($data['bumiputera'] == 'bumiputera'){
            $bumiputera = 1;
        }else{
            $bumiputera = 0;
        }

        $customer = Customer::create([
            'name' => $data['name'],
            'no_pelanggan' => $no_pelanggan,
            'noic' => $data['noic'],
            'tarikh_lahir' => $data['tarikh_lahir'],
            'negeri_lahir' => $data['negeri_lahir'],
            'jantina' => $data['jantina'],
            'alamat' => $data['alamat'],
            'poskod' => $data['poskod'],
            'daerah' => $data['daerah'],
            'negeri' => $data['negeri'],
            'main_phone' => $data['main_phone'],
            'alternate_phone' => $data['alternate_phone'],
            'alamat_menyurat' => $data['alamat_menyurat'],
            'poskod_menyurat' => $data['poskod_menyurat'],
            'negeri_menyurat' => $data['negeri_menyurat'],
            'agama' => $data['agama'],
            'kumpulan_sahabat' => $data['kumpulan_sahabat'],
            'daerah_menyurat' => $data['daerah_menyurat'],
            'umur' => $data['age'],
            'bangsa' => $data['bangsa'],
            'nama_bank' => $data['bank'],
            'no_akaun_bank' => $data['no_akaun'],
            'bumiputera' => $bumiputera,
            'warganegara' => $warganegara,
        ]);

        // if (isset($data['customer_image'])) {
        //     $customer->addMediaFromRequest('customer_image')->toMediaCollection('customer_images');
        // }

        return $customer;
    }
}