<?php
namespace App\Controllers\Article;

use App\Controllers\BaseController;
use App\Models\article\ProcessibleModel;

class Processible extends BaseController
{

  private $model;

  public function __construct()
  {
    $this->model = new ProcessibleModel();
  }

  public function datecount(): string
  {

    $sido = $this->model->getAreaList(); // 지역조회

    $this->data['sido'] = $sido;

    return view("pages/article/processible/datecount", $this->data);
  }

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

    $data = [
      'sdate' => $this->request->getGet('sdate'),           // 시작일
      'edate' => $this->request->getGet('edate'),           // 종료일
    ];

    $totalCount = $this->model->getTotal1($data);

    $datas = $this->model->getList1($start, $end, $data);

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

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

    $data = [
      'sdate' => $this->request->getGet('sdate'),           // 시작일
      'region' => $this->request->getGet('region'),           // 종료일
    ];

    $totalCount = $this->model->getTotal2($data);

    $datas = $this->model->getList2($data);

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

  public function getList3()
  {

    $data = [
      'region' => $this->request->getGet('region'),           // 종료일
    ];

    $totalCount = $this->model->getTotal3($data);

    $datas = $this->model->getList3($data);

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

  // 엑셀다운로드
  public function excel()
  {
    try {

      $data = [
        'sdate' => $this->request->getGet('sdate'),
        'edate' => $this->request->getGet('edate'),
      ];

      $datas = $this->model->getExcelList($data);

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

    } catch (\Exception $e) {
      $e->getPrevious()->getTraceAsString();
    }
  }


  // 지역별 수량 저장
  public function saveArea()
  {
    try {

      $rows = $this->request->getPost('rows');
      $rows = json_decode($rows, true);

      // dd($rows);
      // exit;

      if (count($rows) > 0) {

        foreach ($rows as $row):
          $this->model->saveArea($row);
        endforeach;

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

      } else {
        return $this->response->setJSON([
          'code' => '9',
          'msg' => '저장가능한 데이터가 없습니다.'
        ]);
      }


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

  // 기본 수량 저장
  public function saveCount()
  {
    try {

      $rows = $this->request->getPost('rows');
      $rows = json_decode($rows, true);

      // dd($rows);
      // exit;

      if (count($rows) > 0) {

        foreach ($rows as $row):
          $this->model->saveCount($row);
        endforeach;

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

      } else {
        return $this->response->setJSON([
          'code' => '9',
          'msg' => '저장가능한 데이터가 없습니다.'
        ]);
      }


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