<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ShippingController extends Controller
{
    public function index(Request $request)
    {
        /*
        $results = DB::table('districts')
            ->leftJoin('regencies','regencies.id','=','districts.regency_id')
            ->leftJoin('provinces','provinces.id','=','regencies.province_id')
            ->select('districts.id','districts.name as kec','regencies.name as kota','provinces.name as prov','districts.cost')
            ->get();

        $regions = array();
        foreach($results as $result){
            $region = array(
                'id'=>$result->id,
                'name'=>$result->prov.", ".$result->kota.", ".$result->kec,
                'kec'=>$result->kec,
                'cost'=>$result->cost
            );
            $regions[] = $region;
        }
        return view('admin/shipping-cost',['title'=>'Shipping Cost','regions'=>$regions]);
        */
        return view('admin/shipping-cost',['title'=>'Shipping Cost']);
    }

/*
    public function getDistricts(){
        $results = DB::table('districts')
            ->leftJoin('regencies','regencies.id','=','districts.regency_id')
            ->leftJoin('provinces','provinces.id','=','regencies.province_id')
            ->select(
                'districts.id',
                'districts.name as kec',
                'districts.regency_id',
                'regencies.name as kota',
                'provinces.name as prov',
                'districts.cost'
            )
            ->get();
        
        //Reformat data for Alpinejs filtering
        $districts = array();
        foreach($results as $result){
            $districts[] = array(
                'id'=>$result->id,
                'name'=>$result->prov.", ".$result->kota.", ".$result->kec,
                'kec'=>$result->kec,
                'cost'=>$result->cost,
                'regency_id'=>$result->regency_id
            );
        }
        return $districts;
    }*/
    
    public function getDistricts(){
        $results = DB::table('districts')
            ->leftJoin('regencies','regencies.id','=','districts.regency_id')
            ->leftJoin('provinces','provinces.id','=','regencies.province_id')
            ->select(
                'districts.id','districts.regency_id','districts.cost', DB::raw("CONCAT(provinces.name,', ',regencies.name,', ',districts.name,', => ',districts.cost) as name")
            )
            ->get();
        
        //Reformat data for Alpinejs filtering
        /*
        $districts = array();
        foreach($results as $result){
            $districts[] = array(
                'id'=>$result->id,
                'name'=>$result->prov.", ".$result->kota.", ".$result->kec,
                'kec'=>$result->kec,
                'cost'=>$result->cost,
                'regency_id'=>$result->regency_id
            );
        }
        return $districts;*/
        return $results;
    }
    
    public function store(Request $request){
        $values['cost'] = $request->cost;
        
        //$results = DB::table('districts')->where('id',$request->id)->update($values);
        $results = DB::table('districts')->where('regency_id',$request->regency_id)->update($values);
        
        return $values;
        /*
            return response()->json([
                'success' => true,
                'message' => "Update data berhasil",
                'data'    => $values
            ]);
            */
    }

    public function storeByRegencyId(Request $request){
        $values['cost'] = $request->cost;
        
        $results = DB::table('districts')->where('regency_id',$request->regency_id)->update($values);
        
        return $values;
        /*
            return response()->json([
                'success' => true,
                'message' => "Update data berhasil",
                'data'    => $values
            ]);
            */
    }
    
}
