<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pot_absensi extends MY_Controller{

	public function __construct(){
        parent::__construct();
        $this->load->model('payroll/pengaturan/Pot_absensi_m');
        $this->load->library('form_validation');
    }

	public function index(){
		$data['judul'] = 'Pengaturan Umum';
        $data['aktif'] = 'pengaturan_pot_absensi';
		$data['get_pot_pph'] = $this->Pot_absensi_m->get_pot_pph();
        $data['menu'] = $this->load->view('main_menu', $data, true);
		$data['content'] = $this->load->view('payroll/pengaturan/potongan_absensi/home', $data, true);
		$this->load->view('main_template', $data, false);
	}

	public function list_data(){
		$list = $this->Pot_absensi_m->get_datatables();
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $field) {
			$no++;
			$row = array();
			$row[] = $no;
            # JENIS
            switch ($field->jenis) {
                case 'terlambat':
                    $jenis = 'Terlambat';
                    break;
                case 'pc':
                    $jenis = 'Pulang Cepat';
                    break;
                case 'tap':
                    $jenis = 'Tidak Absen Pulang';
                    break;
                case 'alpa':
                    $jenis = 'Alpa';
                    break;

                default:
                    $jenis = '-';
                    break;
            }
			$row[] = $jenis;

            # FLAG AKTIF
            if ($field->flag_aktif == "1") {
				$row[] = '<label class="switch">
							<input id="'. md5($field->jenis) .'" data-flag="flag_aktif" type="checkbox" class="default" value="1" checked>
							<span class="slider round">Aktif</span>
						</label>';
			}else{
				$row[] = '<label class="switch">
							<input id="'. md5($field->jenis) .'" data-flag="flag_aktif" type="checkbox" class="default" value="0" >
							<span class="slider round">Nonaktif</span>
						</label>';
			}

            # FLAG PERSENTASE GAPOK
            if ($field->flag_persentase_gapok == "1") {
				$row[] = '<label class="switch">
							<input id="'. md5($field->jenis) .'" data-flag="flag_persentase_gapok" type="checkbox" class="default" value="1" checked>
							<span class="slider round">Ya</span>
						</label>';
                # PERSENTASE
                $editPersen = '
                <div class="input-group nominal-edit" data-btn="persen" style="display:none">
                    <input type="text" name="nominal" onkeyup="calculatePersentase(this)" class="form-control" value="'.str_replace('.00','', $field->persentase).'">
                    <div class="input-group-append">
                        <button class="btn_edit_nominal btn btn-primary" type="button"><i class="fa fa-check"></i></button>
                    </div>
                </div>';
                $nominalWithButton = '
                <div id="'.md5($field->jenis).'" class="nominal-with-button btn_edit">
                    <span>'.str_replace('.00','', $field->persentase) .'</span> %
                    <button type="button" class="btn btn-warning">
                        <i class="fa fa-edit (alias) "></i>
                    </button>
                </div>';
                $row[] = '<div class="nominal-form">'.$editPersen.$nominalWithButton.'</div>';
			}else{
				$row[] = '<label class="switch">
							<input id="'. md5($field->jenis) .'" data-flag="flag_persentase_gapok" type="checkbox" class="default" value="0" >
							<span class="slider round">Tidak</span>
						</label>';
                $row[] = '';
			}

            # NOMINAL POTONGAN
            $editNominal = '
            <div class="input-group nominal-edit" data-btn="nominal" style="display:none">
                <input type="text" name="nominal" onkeyup="formatangka(this);" class="form-control" value="'.number_format($field->nominal_potongan,0,',','.').'">
                <div class="input-group-append">
                    <button class="btn_edit_nominal btn btn-primary" type="button"><i class="fa fa-check"></i></button>
                </div>
            </div>';
            if ($field->flag_persentase_gapok == "1") {
                $nominalWithButton = '';
            }else{
                $nominalWithButton = '
                <div id="'.md5($field->jenis).'" class="nominal-with-button btn_edit">
                    <span>'.number_format($field->nominal_potongan,0,',','.').'</span>
                    <button type="button" class="btn btn-warning">
                        <i class="fa fa-edit (alias) "></i>
                    </button>
                </div>';
            }
			$row[] = '<div class="nominal-form">'.$editNominal.$nominalWithButton.'</div>';

            # MENIT
            if ($field->jenis == 'terlambat' || $field->jenis == 'pc') {
                $editNominal = '
                <div class="input-group nominal-edit" data-btn="menit" style="display:none">
                    <input type="text" name="nominal" onkeyup="formatangka(this);" class="form-control" value="'.$field->menit.'">
                    <div class="input-group-append">
                        <button class="btn_edit_nominal btn btn-primary" type="button"><i class="fa fa-check"></i></button>
                    </div>
                </div>';
                $nominalWithButton = '
                <div id="'.md5($field->jenis).'" class="nominal-with-button btn_edit">
                    <span>'.$field->menit.'</span> menit
                    <button type="button" class="btn btn-warning">
                        <i class="fa fa-edit (alias) "></i>
                    </button>
                </div>';
                $row[] = '<div class="nominal-form">'.$editNominal.$nominalWithButton.'</div>';
            }else{
                $row[] = '';
            }

            # KELIPATAN
            if ($field->jenis == 'terlambat' || $field->jenis == 'pc') {
                if ($field->kelipatan == "1") {
                    $row[] = '<label class="switch">
                                <input id="'. md5($field->jenis) .'" data-flag="kelipatan" type="checkbox" class="default" value="1" checked>
                                <span class="slider round">Ya</span>
                            </label>';
                }else{
                    $row[] = '<label class="switch">
                                <input id="'. md5($field->jenis) .'" data-flag="kelipatan" type="checkbox" class="default" value="0" >
                                <span class="slider round">Tidak</span>
                            </label>';
                }
            }else{
                $row[] = '';
            }

			$data[] = $row;
		}

		$output = array("draw"            => $_POST['draw'],
						"recordsTotal"    => $this->Pot_absensi_m->count_all(),
						"recordsFiltered" => $this->Pot_absensi_m->count_filtered(),
						"data"            => $data);

		$output = __response($this->MY_response, $output);

		echo json_encode($output);
	}

	public function form(){
        $this->load->view('payroll/pengaturan/potongan_absensi/form');
	}

	public function save(){
        $jenis	    = $this->input->post('jenis');
		$nominal	= $this->input->post('nominal_potongan');
		$menit	    = $this->input->post('menit');
		$persen	    = $this->input->post('persen');

        if ($persen != '') {
            $data = array('persentase' => $persen);
        }else if ($menit != '') {
            $data = array('menit' => $menit);
        }else{
            $data = array('nominal_potongan' => str_replace(".","",$nominal));
        }

		$this->Pot_absensi_m->update($data, $jenis);
	}

	public function update_flag(){
        $jenis	    = $this->input->post('id_p');
        $for_flag   = $this->input->post('for_flag');
        $status	    = $this->input->post('status');
        $persentase = $this->input->post('persentase');

        if ($for_flag == 'menit') {
            if ($status == '1') {
                $status = 'jam';
            }else{
                $status = 'menit';
            }
        }

        // if (preg_match('/^[0-9][0-9]?(\.[0-9]{0,2})?$/', $persentase)) {
        //     $persentase = $persentase;
        // }else{
        //     $persentase = '';
        // }

        if ($for_flag == 'flag_persentase_gapok') {
            $data = array(
                $for_flag => $status,
                'persentase' => $persentase
            );
        }else{
            $data = array(
                $for_flag => $status
            );
        }

		$this->Pot_absensi_m->update($data, $jenis);
	}

    public function update_pot_pph(){
        $jenis	    = $this->input->post('jenis_pph');
        $update     = $this->Pot_absensi_m->update_pot_pph($jenis);
    }
}
