<?php

namespace App\Http\Controllers;

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

class GenerateDataMasterController extends Controller
{
    public function GetMsBasicProduct()
    {
        $data = DB::table('MS_BASIC_PRODUCT')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsSpajkStatus()
    {
        $data = DB::table('SPAJK_STATUS')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsMarketingType()
    {
        $data = DB::table('MS_MARKETING_TYPE')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsDeliveryStatus()
    {
        $data = DB::table('MS_DELIVERY_STATUS')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsPartner()
    {
        $data = DB::table('MS_PARTNER')
            ->select('ID', 'DESCRIPTION')
            ->get(); 

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });
        return response()->json($dropdownList);
    }

    public function GetMsPartnerRegion($id)
    {   
        
        $data = DB::table('MS_PARTNER_REGION')
        ->select('ID', 'DESCRIPTION')
        ->where('PARTNER_CODE', $id)
        ->get();
        
        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });
    
        return response()->json($dropdownList);
    }

    public function GetMsPaymentMethod()
    {
        $data = DB::table('MS_PAYMENT_METHOD')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsCountry()
    {
        $data = DB::table('MS_COUNTRY')
            ->select('ID', 'COUNTRY_NAME', 'COUNTRY_CODE')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->country_name,
                'code' => $item->country_code
            ];
        });

        return response()->json($dropdownList);
    }


    public function GetMsNationality()
    {
        $data = DB::table('MS_NATIONALITY')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    

    public function GetMsProvince()
    {
        $data = DB::table('MS_PROVINCE')
            ->select('ID', 'DESCRIPTION','CODE')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    
    public function GetMsCity($provinceId)
    {
        $data = DB::table('MS_CITY')
            ->select('ID', 'NAME','CODE')
            ->where('PROVINCE_CODE', $provinceId)
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }


    
    public function GetMsDistrict($cityId)
    {
        $data = DB::table('MS_DISTRICT')
            ->select('ID', 'NAME','CODE')
            ->where('CITY_CODE', $cityId)
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }

    
    public function GetMsSubDistrict($districtId)
    {
        $data = DB::table('MS_SUB_DISTRICT')
            ->select('ID', 'NAME','CODE')
            ->where('DISTRICT_CODE', $districtId)
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }


    public function GetMsJobType()
    {
        $data = DB::table('MS_JOB_TYPE')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsPartnerBranch($id)
    {
        $data = DB::table('MS_PARTNER_BRANCH')
            ->select('ID', 'DESCRIPTION')
            ->where('PARTNER_REGION_CODE', $id)
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsPartnerUnit($id)
    {
        $data = DB::table('MS_PARTNER_UNIT')
            ->select('ID', 'DESCRIPTION')
            ->where('PARTNER_BRANCH_CODE', $id)
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsIdentityType()
    {
        $data = DB::table('MS_IDENTITY_TYPE')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsChannel($code)
    {
        $data = DB::table('MS_CHANNEL')
            ->select('ID', 'DESCRIPTION')
            ->where('MARKETING_TYPE', $code)
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => trim($item->id),
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsChannelList()
    {
        $data = DB::table('MS_CHANNEL')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => trim($item->id),
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsDistribution()
    {
        $data = DB::table('MS_DISTRIBUTION_CHANNEL')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetProduct()
    {
        $data = DB::table('PRODUCT')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetProductCategory()
    {
        $data = DB::table('MS_PRODUCT_CATEGORY')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => trim($item->id),
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetOptions()
    {
        $roundingType = DB::table('MS_OPTIONS')
            ->select('CODE', 'DESCRIPTION')
            ->get();

        $dropdownList = $roundingType->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetRoundingType()
    {
        $roundingType = DB::table('MS_ROUNDING_TYPE')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $roundingType->map(function ($item) {
            return [
                'value' => trim($item->id),
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetBank()
    {
        $roundingType = DB::table('MS_BANK')
            ->select('ID', 'DESCRIPTION')
            ->orderBy('DESCRIPTION', 'asc')
            ->get();

        $dropdownList = $roundingType->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsSpajkdocument()
    {
        $roundingType = DB::table('MS_DOCUMENT_SPAJK')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $roundingType->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }


    public function GetDocumentFormat()
    {
        $roundingType = DB::table('MS_DOCUMENT_FORMAT')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $roundingType->map(function ($item) {
            return [
                'value' => trim($item->id),
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsDocumentSendTo()
    {
        $products = DB::table('MS_DOCUMENT_SENDTO')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $products->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsAutoUwStatus()
    {
        $products = DB::table('MS_AUTOUW_STATUS')
            ->select('ID', 'DESCRIPTION')
            ->get();

        $dropdownList = $products->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetSpajkStatus()
    {
        $products = DB::table('SPAJK_STATUS')
            ->select('ID', 'DESCRIPTION')
            ->whereIn('code', ['SWA', 'UPE', 'UPO', 'UDE', 'UAP'])
            ->get();

        $dropdownList = $products->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsPolicyByPartner($id)
    {
        $products = DB::table('POLICY')
            ->select('ID', 'POLICY_NO', 'PARTNER_CODE')
            ->where('PARTNER_CODE', $id)
            ->get();

        $dropdownList = $products->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->policy_no,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsPolicyProduct($id)
    {   
        $products = DB::table('POLICY_PRODUCT')
            ->join('PRODUCT', 'POLICY_PRODUCT.PRODUCT_ID', '=', 'PRODUCT.ID')
            ->join('MS_MARKETING_TYPE', 'PRODUCT.MARKETING_TYPE', '=', 'MS_MARKETING_TYPE.ID')
            ->join('MS_CHANNEL', 'PRODUCT.CHANNEL_CODE', '=', 'MS_CHANNEL.ID')
            ->select('POLICY_PRODUCT.POLICY_ID AS POLICY_ID', 'MS_MARKETING_TYPE.DESCRIPTION AS MARKETING', 'MS_CHANNEL.DESCRIPTION AS CHANNEL', 'MS_MARKETING_TYPE.ID AS MARKETING_ID', 'MS_CHANNEL.ID AS CHANNEL_ID')
            ->where('POLICY_PRODUCT.POLICY_ID', $id)
            ->get();

        $dropdownList = $products->map(function ($item) {
            return $item;
        });

        return response()->json($dropdownList);
    }


    public function getAllMsCity()
    {
        $data = DB::table('MS_CITY')
            ->select('ID', 'NAME','CODE')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }

    public function getAllMsDistrict()
    {
        $data = DB::table('MS_DISTRICT')
            ->select('ID', 'NAME','CODE')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }

    
    public function getAllMsSubDistrict()
    {
        $data = DB::table('MS_SUB_DISTRICT')
            ->select('ID', 'NAME','CODE')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsGender()
    {
        $data = DB::table('MS_GENDER')
            ->select('ID', 'CODE','DESCRIPTION')
            ->orderBy('DESCRIPTION', 'ASC')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsLoanType()
    {
        $data = DB::table('MS_LOAN_TYPE')
            ->select('ID', 'CODE','DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsPendingUWType()
    {
        $data = DB::table('MS_PENDING_UW')
            ->select('ID','CODE', 'NAME')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->code.' - '.$item->name,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsMedicalType()
    {
        $data = DB::table('MS_MEDICAL_TYPE')
            ->select('ID','CODE', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }

    public function GetMsEM()
    {
        $data = DB::table('MS_EXTRA_MORTALITY')
            ->select('ID','CODE', 'DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  => $item->description,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsClIcd()
    {
        $data = DB::table('MS_CL_ICD')
            ->select('ID', 'ICD_CODE','ICD_SHORT_DESC')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->id,
                'text'  =>  $item->icd_code.' - '.$item->icd_short_desc,
            ];
        });

        return response()->json($dropdownList);
    }
    public function GetMsDecision()
    {
        $data = DB::table('MS_UW_DECISION')
            ->select('ID', 'CODE','DESCRIPTION')
            ->get();

        $dropdownList = $data->map(function ($item) {
            return [
                'value' => $item->code,
                'text'  => $item->description
            ];
        });

        return response()->json($dropdownList);
    }
}


