<?php

namespace App\Http\Controllers\sales;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MdDepartmentAuth extends Controller
{
    //

    public function verify(){

        return view('auth.verify');
    }

    public function verifyAuth(Request $request){

        $accessCode = $request->input('access_code');
        $department = DB::table('department_auths')
            ->where('access_code', $accessCode)
            ->value('department');

        if (!$department){
            return response()->json('invalid access code');
        }


        $sales = DB::table('sale_order')
            ->join('sale_order_line', 'sale_order.id', '=', 'sale_order_line.order_id')
            ->join('res_partner', 'sale_order.partner_id', '=', 'res_partner.id')
            ->join('res_users', 'sale_order_line.salesman_id', '=', 'res_users.id')
            ->join('product_product', 'sale_order_line.product_id', '=', 'product_product.id')
            ->join('product_template', 'product_product.product_tmpl_id', '=', 'product_template.id')
            ->select(
                'sale_order.id as id',
                'sale_order.amount_total as amount',
                'sale_order.name as order_id',
                'sale_order.date_order',
                'res_partner.name as customer_name',
                'product_template.name as house_type',
                DB::raw("CASE product_template.detailed_type
                    WHEN 'millenium' THEN 'Millenium City Kaduna'
                    WHEN 'consu' THEN 'LIFE CAMP, IDOGWARI'
                    WHEN 'karsana' THEN 'KARSANA'
                    WHEN 'ace_1' THEN 'ACE Phase 1'
                    WHEN 'ace_2' THEN 'ACE Phase 2'
                    WHEN 'ace_3' THEN 'ACE Phase 3'
                    ELSE 'Unknown'
                END as project")
            )
            ->get();

        $total_applications = DB::table('document_document')->count();
        $total_projects = DB::table('product_product')->count();
        $total_agents = DB::table('product_product')->count();

        return view('md.index', ([
            'sales' => $sales,
            'total_applications' => $total_applications,
            'total_projects' => $total_projects,
            'total_agents' => $total_agents,
        ]));


    }


}
