<?php

/**
 * Created by PhpStorm.
 * User: Opus-U3
 * Date: 8/30/2017
 * Time: 4:52 PM
 */

namespace App\Services;


use App\Company;
use App\Customer;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;

class AdminService
{
    public function addCustomer(array $data)
    {
        $status = '0';
        $message = "";
        $customer_id = strtoupper($data['customer_id']);
        $customer_name = $data['customer_name'];
        $company = $data['company'];
        $address = $data['address'];
        $mobile = $data['mobile'];
        $email = $data['email'];
        $gender = $data['gender'];
        $category = $data['category'];
        $referred_by = $data['referred_by'];
        $company_data = '0';
        if ($company == 1 || $company == 3) {
            $company_data = '0';
        } else {
            $company_data = $company;
        }
        $check_customer_id = Customer::where('customer_id', $customer_id)->exists();
        $check_email = Customer::where('email', $email)->exists();
        $check_mobile = Customer::where('mobile', $mobile)->exists();
        if ($check_customer_id) {
            $status = 0;
            $message = 'Customer id Already Exists';
        } else if ($check_email) {
            $status = 0;
            $message = 'Email Already Exists';
        } else if ($check_mobile) {
            $status = 0;
            $message = 'Mobile Already Exists';
        } else {
            $save = new Customer();
            $save->user_id = Auth::user()->id;
            $save->customer_id = $customer_id;
            $save->customer_name = $customer_name;
            $save->company = $company_data;
            $save->address = $address;
            $save->mobile = $mobile;
            $save->email = $email;
            $save->gender = $gender;
            $save->category = $category;
            $save->referred_by = $referred_by;
            $save->created_by = Auth::user()->role;
            $save->save();
            $path = public_path() . '/gallery/' . $save->customer_id;
            $result = File::makeDirectory($path, $mode = 0777, true, true);
            $status = 1;
            $message = 'Successfully Inserted';
        }
        return ['status' => $status, 'message' => $message];
    }


    public function addCompany(array $data)
    {
        $status = '0';
        $message = "";
        $company = ucfirst($data['company']);
        $check_company = Company::where('company', $company)->exists();
        if ($check_company == true) {
            $status = '0';
            $message = $company . " Already Exists!";
        } else {

            $save_company = new Company();
            $save_company->user_id = Auth::user()->id;
            $save_company->company = $company;
            $save_company->save();

            $status = '1';
            $message = $company . " SuccessFully Added";
        }

        return ['status' => $status, 'message' => $message, 'set_company' => $save_company->id, 'set_company_name' => $company];

    }

}