<?php
namespace App\Controllers\manage;

use App\Controllers\BaseController;
use App\Models\manage\AreasModel;

class Areas extends BaseController
{
    private $areaModel;
    public function __construct()
    {
        $this->areaModel = new AreasModel();
    }

    // 지역코드조회
    public function getAreaList()
    {
        $sido = $this->request->getPost('srcSido');
        $gugun = $this->request->getPost('srcGugun');


        $datas = $this->areaModel->getAreaList($sido, $gugun);

        return $this->response->setJson($datas);

    }

    public function lists(): string
    {

        $sido = $this->areaModel->getAreaList();
        $bonbu = $this->areaModel->getBonbuList();
        $team = $this->areaModel->getTeamList();
        $user = $this->areaModel->getUserList();

        $this->data['sido'] = $sido;
        $this->data['bonbu'] = $bonbu;
        $this->data['team'] = $team;
        $this->data['user'] = $user;

        return view("pages/manage/areas/lists", $this->data);
    }


    public function getSvcArea()
    {
        $start = (int) $this->request->getGet('start') ?: 0;
        $end = (int) $this->request->getGet('length') ?: 10;

        $srcSido = $this->request->getGet('srcSido'); // 시도
        $srcGugun = $this->request->getGet('srcGugun'); // 시도
        $srcDong = $this->request->getGet('srcDong'); // 시도

        $region = "";
        if (!empty($srcDong)) {
            $region = substr($srcDong, 0, 8);
        } else if (!empty($srcGugun)) {
            $region = substr($srcGugun, 0, 5);
            if (substr($region, 4) == "0") {
                $region = substr($srcGugun, 0, 4);
            }
        } else if (!empty($srcSido)) {
            $region = substr($srcSido, 0, 2);
        }

        $data = [
            'region' => $region, // 지역코드
            'srcUserbonbu' => $this->request->getGet('srcUserbonbu'), // 본부
            'srcUserteam' => $this->request->getGet('srcUserteam'), // 팀
            'positionYn' => $this->request->getGet('positionYn'), // 지정유무
        ];

        $totalCount = $this->areaModel->getTotalCount($data);
        $datas = $this->areaModel->getSvcArea($start, $end, $data);

        return $this->response->setJSON(body: [
            'recordsTotal' => $totalCount,
            'recordsFiltered' => $totalCount,
            'data' => $datas,
        ]);
    }

    // 지역관리정보저장
    public function saveRegion()
    {
        try {

            $json = $this->request->getJSON(true);   // true = 배열로 반환

            if (!$json) {
                return $this->response->setJSON([
                    'status' => 'error',
                    'msg' => '데이터 누락'
                ]);
            }


            foreach ($json as $row) {
                $regioncd = $row['region_cd'];
                $bonbu = $row['bonbu'];
                $team = $row['team'];
                $user = $row['user'];

                // UPDATE region_codes
                $this->areaModel->saveRegion($regioncd, $team, $user);

            }

            return $this->response->setJSON([
                'code' => '0',
                'msg' => 'success'
            ]);

        } catch (\Exception $e) {
            return $this->response->setJSON([
                'code' => '9',
                'msg' => $e->getMessage(),
            ]);
        }
    }



    public function saveAllRegion()
    {
        try {

            $json = $this->request->getJSON(true);   // true = 배열로 반환

            if (!$json) {
                return $this->response->setJSON([
                    'status' => 'error',
                    'msg' => '데이터 누락'
                ]);
            }

            foreach ($json as $row) {
                $regioncd = $row['region_cd'];
                $bonbu = $row['bonbu'];
                $team = $row['team'];
                $user = $row['user'];

                // UPDATE region_codes
                $this->areaModel->saveRegion($regioncd, $team, $user);

            }

            return $this->response->setJSON([
                'code' => '0',
                'msg' => 'success'
            ]);

        } catch (\Exception $e) {
            return $this->response->setJSON([
                'code' => '9',
                'msg' => $e->getMessage(),
            ]);
        }
    }
}