<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Customer;
use App\Transaction;
use Illuminate\Http\Request;
use App\Collector;
use App\Finance;

class CustomersApiController extends Controller
{
    public function index()
    {
        $customers = Customer::all();

        return $customers;
    }

    public function get_customer_by_finance($orgCode){
        $customers = Customer::where('orgCode', $orgCode)->get();

        return $customers;
    }

    public function get_finance_details($orgCode){
        $finance = Finance::where('orgCode', $orgCode)->first();

        return $finance;
    }

    public function post_trans_data(Request $request){

       
        $data = $request->get('data');
        $datas = json_decode($data,true);
       // dd($datas);
       $status = 1;
        foreach($datas as $dat){
            $trans = new Transaction;
            $trans->orgCode = $dat['orgCode'];
            $trans->collectorId = $dat['collectorId'];
            $trans->acId = $dat['acc_no'];
            $trans->amount = $dat['deposite_amount'];
            $trans->nepDate =''; //$dat['nep_date'];
            $trans->tranDate = $dat['tranac_date_time'];
            
            $trans->save();
        }
        return response(['status' => $status]);
       
    }

    public function loginCollector(Request $request)
    {
        $mobile = $request->input('mobile');
        $password = $request->input('password');
        $device_id = $request->input('device_id');
        $collector = Collector::where(['mobile'=>$mobile,'password'=>$password])->first();
        if($collector)       // If the user is found
        {
            if($collector->device_id)
            {
                if($collector->device_id == $device_id)
                {
                    return $collector;
                }else{
                    return null;
                }
            }else
            {
                $collector->device_id = $device_id;
                $collector->update();
                return $collector;
            }
        }else{
            return null;       // You can create a page for this
        }
    }
}
