<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use App\Customer;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class CustomersImport implements ToModel, WithHeadingRow
{
    /**
    * @param Collection $collection
    */
    public function model(array $row){


    $name=   $row['name'];
       $father_name= $row['father_name'];
    $cnic=    $row['cnic'];
      $mobile_number=  $row['mobile_number'];
       $total_paid_amount= $row['total_paid_amount'];
    $email=   $row['email'];
       $password= $row['password'];
       $mailing_address=  $row['mailing_address'];
    $permanent_address=     $row['permanent_address'];
       $ntn_number=   $row['ntn_number'];
       $is_filer=  $row['is_filer'];



       

      // Hash::make();
        
      if( !empty($name) && !empty($father_name) && !empty($cnic) ){

       $customer= new Customer;

       $customer->name =$name;
       $customer->type = 0;
       $customer->father_name=$father_name;
       $customer->cnic= $cnic;
       $customer->email = $email;
       $customer->mobile_number = $mobile_number;
       $customer->total_paid_amount =!empty($total_paid_amount)?$total_paid_amount:0;
       $customer->mailing_address =$mailing_address;
       $customer->permanent_address=$permanent_address;
       if(!empty($password)){
        $customer->password=Hash::make( $password);
       }
      if(!empty($is_filer)){
        $is_filer= Str::lower($is_filer);
        if($is_filer == 'no'){
            $customer->is_filer=0;
        }
        if($is_filer == 'yes'){
            $customer->is_filer = 1;
        }
      }
      
       $customer->ntn_number=$ntn_number;
       $customer->save();
       return $customer;
      }

    }
}
