<?php

namespace App\Http\Controllers\Client;

use App\Exports\OuterClientsExport;
use App\Exports\ProductsExport;
use App\Http\Controllers\Controller;
use App\Imports\OuterClientsImport;
use App\Imports\ProductsImport;
use App\Models\OuterClient;
use App\Models\Product;
use App\Models\Supplier;
use Illuminate\Http\Request;
use App\Exports\SuppliersExport;
use App\Imports\SuppliersImport;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;

class ImportExportController extends Controller
{
    public function export_suppliers()
    {
        return Excel::download(new SuppliersExport, 'الموردين.xlsx');
    }
    
    public function import_suppliers(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $suppliers = Supplier::where('company_id',$company_id)
            ->get();
        $tempSuppliers = $suppliers;    
        foreach ($suppliers as $supplier) {
            $supplier->delete();
        }
        Excel::import(new SuppliersImport, $request->file('file'));
        foreach ($tempSuppliers as $tempSupplier) {
            $supplier = Supplier::where('supplier_name',$tempSupplier->supplier_name)->get();
            if(count($supplier) === 0){
                $data = 
                [
                    'company_id'     => $tempSupplier->company_id,
                    'supplier_name'     => $tempSupplier->supplier_name,
                    'supplier_category'     => $tempSupplier->supplier_category,
                    'balance'     => $tempSupplier->balance,
                    'created_at'     => $tempSupplier->created_at,
                    'updated_at'     => $tempSupplier->updated_at,
                ];
                Supplier::create($data);
            }
        }
        return redirect()->back();
    }

    public function export_outer_clients()
    {
        return Excel::download(new OuterClientsExport, 'العملاء.xlsx');
    }
    
    public function import_outer_clients(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $outer_clients = OuterClient::where('company_id',$company_id)
            ->get();
        $tempOuterClients = $outer_clients;    
        foreach ($outer_clients as $outer_client) {
            $outer_client->delete();
        }
        foreach ($tempOuterClients as $tempOuterClient) {
            $client = OuterClient::where('client_name',$tempOuterClient->client_name)->get();
            if(count($client) === 0){
                $data = 
                [
                    'company_id' => $tempOuterClient->company_id,
                    'client_name' => $tempOuterClient->client_name,
                    'client_category' => $tempOuterClient->client_category,
                    'balance' => $tempOuterClient->balance,
                    'created_at'     => $tempOuterClient->created_at,
                    'updated_at'     => $tempOuterClient->updated_at,
                ];
                OuterClient::create($data);
            }
        }
        Excel::import(new OuterClientsImport, $request->file('file'));
        return redirect()->back();
    }

    public function export_products()
    {
        return Excel::download(new ProductsExport, 'المنتجات.xlsx');
    }
    
    public function import_products(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $products = Product::where('company_id',$company_id)
            ->get();
        $items = $products;    
        foreach ($products as $product) {
            $product->delete();
        }
        Excel::import(new ProductsImport, $request->file('file'));
        foreach ($items as $item) {
            $product = Product::where('product_name',$item->product_name)->get();
            $balance = $item->balance;
            if(count($product) === 0){
                $data = 
                [
                    'category_id'=>$item->category_id,    
                    'store_id'=>$item->store_id,    
                    'company_id'=>$company_id,    
                    'product_name'=>$item->product_name,
                    'purchasing_price'=>$item->purchasing_price,
                    'sector_price'=>$item->sector_price,
                    'wholesale_price'=>$item->wholesale_price,
                    'balance'=>$item->balance
                ];
                Product::create($data);
            }
        }
        return redirect()->back();
    }
}
