<?php

namespace fgdsapp;

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

use Controller;

class dailydelplan extends Controller {

    protected $db, $table, $uid, $dbquery;

    function __construct() {
        $f3 = \Base::instance();
        $dbHost = $f3->get('DBHOST');
        $dbName = $f3->get('DBNAME');
        $dbUser = $f3->get('DBUSER');
        $dbPass = $f3->get('DBPASS');
        $dbquery = new \DB\SQL('mysql:host='.$dbHost.';port=3306;dbname='.$dbName,$dbUser,$dbPass);
        $this->dbquery = $dbquery;
        $this->table = 'CTM_DAILY_DEL_PLAN_TAB';
        $this->uid = $f3->get('SESSION.uid'); 

        // db for datatable
        require_once('app/class/lib/DataTables.php');
        $this->db = $db;


    }

    function render($f3) {

        // render
        $f3->set('message', '');
        $f3->set('site','SIM');
        $f3->set('year',$this->getYear()) ;
        $f3->set('month',$this->getMonth()) ;
        echo \Template::instance()->render('fgdsapp/dailydelplan.html');

    }

    function load($f3) {

        Editor::inst( $this->db, $this->table, array( 'CONTRACT','DEL_DATE','DISTRICT_CODE','CUST_PART_NO' ))
            ->fields(
                
                Field::inst('CONTRACT')
                ->validator(Validate::notEmpty(ValidateOptions::inst()
                    ->message('Contract/Site is required')))       
                ,
                Field::inst('DEL_DATE')
                    ->validator(Validate::dateformat('Y-m-d'))
                    ->getFormatter( Format::dateSqlToFormat('Y-m-d'))
                    ->setFormatter( Format::dateFormatToSql('Y-m-d'))                    
                    ->setFormatter( Format::ifEmpty(null))
                ,
                Field::inst('DISTRICT_CODE')
                    ->validator(Validate::notEmpty(ValidateOptions::inst()
                        ->message('Address is required')))
                ,
                Field::inst('CUST_PART_NO')
                    ->validator(Validate::notEmpty(ValidateOptions::inst()
                        ->message('Customer Part No is required')))                         
                ,
                Field::inst('CUSTOMER_ID')
                    ->validator(Validate::notEmpty(ValidateOptions::inst()
                        ->message('Customer ID is required')))                         
                ,
                Field::inst('CUST_PO_NO'),
                Field::inst('ORDER_NO'),
                Field::inst('REVISION_NO'),
                Field::inst('ETA1_QTY'),
                Field::inst('ETA2_QTY'),
                Field::inst('ETA3_QTY'),
                Field::inst('ETA4_QTY'),
                Field::inst('ETA5_QTY'),
                Field::inst('ETA6_QTY'),
                Field::inst('ETA7_QTY'),
                Field::inst('ETA8_QTY'),
                Field::inst('ETA9_QTY'),
                Field::inst('ETA10_QTY'),
                Field::inst('ETA11_QTY'),
                Field::inst('ETA12_QTY'),
                Field::inst('ETA13_QTY'),
                Field::inst('ETA14_QTY'),
                Field::inst('ETA15_QTY'),
                Field::inst('ETA16_QTY'),
                Field::inst('ETA17_QTY'),
                Field::inst('ETA18_QTY'),
                Field::inst('ETA19_QTY'),
                Field::inst('ETA20_QTY'),
                Field::inst('ETA21_QTY'),
                Field::inst('ETA22_QTY'),
                Field::inst('ETA23_QTY'),
                Field::inst('ETA24_QTY'),
                Field::inst('REVISION_NO')->set(Field::SET_CREATE),
                Field::inst('REVISION_NO')->set(Field::SET_EDIT),
                Field::inst('ROWVERSION')->set(Field::SET_CREATE),
                Field::inst('CREATED_DATETIME')->set(Field::SET_CREATE),
                Field::inst('CREATED_BY')->set(Field::SET_CREATE),
                Field::inst('MODIFIED_DATETIME')->set(Field::SET_EDIT),
                Field::inst('MODIFIED_BY')->set(Field::SET_EDIT)
            )
            ->validator( function($editor, $action, $data) {
                $i=0;
                
                // foreach($data['data'] as $pkey=>$values) {
                //     $i =$i+1;
                //     // $finishMap = $values['FINISH_MAPPING'];
                //     // $startMap = $values['START_MAPPING'];
                //     // $superVisor = $values['SUPERVISOR'];
                // };

                if ($action===Editor::ACTION_CREATE ) {
                    ;                    
                }; 
                    
                if($action===Editor::ACTION_EDIT){
                    ;
                };
                
                if($action===Editor::ACTION_EDIT ||$action===Editor::ACTION_CREATE  ){
                    
                    ;
                    //5. Supervisor harus terdaftar
                    // if ($this->getUserAccess($superVisor)!=77 ) {
                    //     return "User $superVisor tidak ada atau bukan Supervisor."; 
                    // }
                }
            })
            ->on('preCreate', function($editor, $values) {
                $editor
                    ->field('REVISION_NO')
                    ->setValue(0);
                $editor
                    ->field('ROWVERSION')
                    ->setValue(date('Y-m-d H:i:s'));
                $editor
                    ->field('CREATED_DATETIME')
                    ->setValue(date('Y-m-d H:i:s'));
                $editor
                    ->field('CREATED_BY')
                    ->setValue($this->uid );                
            })
            ->on('preEdit', function($editor, $id, $values) {
                $editor 
                    ->field('MODIFIED_DATETIME')
                    ->setValue(date('Y-m-d H:i:s'));
                $editor
                    ->field('MODIFIED_BY')
                    ->setValue($this->uid );                
            })
            ->debug(true)
            ->process($_POST)
            ->json();
    }

    function getUserAccess($npk) {
        // $thetable = 'SIM_INV_TAKING_USER_TAB';
        // $result = $this->dbquery->exec('select ACCESS_NO from  '.$thetable.'  where NPK=?   ', $npk  );
        $retval = 1; // default true, karena belom dipikirin
        // foreach ($result as $index => $nilai ) {
        //     $retval = $nilai['ACCESS_NO'];
        // }        
        return $retval;
    }

    function getYear() {
        $result = $this->dbquery->exec('select YEAR from SIM_INV_TAKING_CONCLUDING_TAB where ROWSTATE="Started"');
        if($result) {
            $i =0 ;
            foreach ($result as $item) {                
                $i = $i+1;
            }
            return $item['YEAR'];
        }   
    }
    
    function getMonth() {
        $result = $this->dbquery->exec('select MONTH from SIM_INV_TAKING_CONCLUDING_TAB where ROWSTATE="Started"');
        if($result) {
            $i =0 ;
            foreach ($result as $item) {                
                $i = $i+1;
            }
            return $item['MONTH'];
        }   
    }
    

    function loadNpk($f3) {
        $query = $f3->get('GET.q');
        $query = str_replace(' ', '.', $query);

        if ($query) {
            $ldap = new \Ldap($f3->get('LDAP_HOST'), $f3->get('LDAP_DN'), $f3->get('LDAP_USER'), $f3->get('LDAP_PASS'));
            $result = $ldap->searchByQuery($query);
    
            if($result) {
                foreach ($result as $item) {
                    if (isset($item['employeenumber'][0])) {
                        $arrayNpk[] = [
                            'label'=>get_employee_name_from_ldap($item['uid'][0]), 
                            'value'=>$item['employeenumber'][0],
                            'uid'=>$item['uid'][0]
                        ]; 
                    }
                }
                $json = json_encode($arrayNpk);
                echo $json;
            }
        }
    }

    function getEmpName($f3) {
        $query = $f3->get('GET.q');

        if ($query) {
            $ldap = new \Ldap($f3->get('LDAP_HOST'), $f3->get('LDAP_DN'),  $f3->get('LDAP_USER'), $f3->get('LDAP_PASS'));
            $arr['name'] = get_employee_name_from_ldap($ldap->getNameByEmpNumber($query));
            echo json_encode($arr);
        }
    }

    function loadLocationNo($f3)
    {
        //$result = $this->dbquery->exec("SELECT LOCATION_NO, LOCATION_NAME, LOCATION_GROUP FROM INVENTORY_LOCATION_TAB  WHERE LOCATION_NO LIKE ?", '%' . $f3->get('GET.q') . '%');
        $result = $this->dbquery->exec('SELECT LOCATION_NO, LOCATION_NAME, LOCATION_GROUP FROM INVENTORY_LOCATION_TAB  ');
        if ($result) {
            $index = 0;
            foreach ($result as $index => $row) {
                $location_no = $row['LOCATION_NO'];
                $location_name = $row['LOCATION_NAME'];
                $location_group = $row['LOCATION_GROUP'];
                $arrayLocationNo[$index] = ['label' => $location_no, 'value' => $location_no, 'location_name' => $location_name, 'location_group' => $location_group];
                $index = $index + 1;
            }
            $json = json_encode($arrayLocationNo);
            echo $json;
        }
    }
   


}


?>