<?php

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

class Aspek_kinerja extends MY_Controller {

    public function index() {
        $this->load->view('kpi/user/approval/detail/table/aspek_kinerja');
    }

    public function list_data()
    {
        $this->load->model('kpi/user/team/detail_m');
        $this->load->helper('kpi/kpi');
        
        $list    = $this->detail_m->get_datatables();
        $dataTrx = $this->detail_m->getDataTrx()->row();

        $data        = array();
        $namaObject  = '';
        $indek       = 0;
        $total_bobot = 0;
		foreach ($list as $field) {
			$row   = array();
			
            $row[]      = $field->nm_object == $namaObject ? '' : $field->nm_object;
            $namaObject = $field->nm_object != $namaObject ? $field->nm_object : $namaObject;

			$row[] = $field->nm_aspek_kinerja;
			$row[] = $field->target;
			$row[] = $field->bobot."%";
			$row[] = $field->poin_1;
			$row[] = $field->poin_2;
			$row[] = $field->poin_3;
			$row[] = $field->poin_4;
			$row[] = $field->poin_5;
			$row[] = $field->actual_poin;
			$row[] = $field->actual_bukti;
			$row[] = actual($field, $indek, $dataTrx, $this->id_karyawan, 'id_pi_trx');

			$data[] = $row;
            $indek++;
            $total_bobot += $field->bobot;
		}

        $row   = array();
        $row[] = "TOTAL";
        $row[] = "100 %";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "";
        $row[] = "colspan-total";
        $data[] = $row;

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

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

		echo json_encode($output);
    }

    public function save() {
        $this->load->model('kpi/user/approval/home_m');
        $this->load->model('kpi/user/approval/aspek_kinerja_m');
        $this->load->model('kpi/user/approval/nilai_individu_m');

        $data = $this->aspek_kinerja_m->data();
        $this->db->trans_start();
        $this->home_m->setDraftApproval();
        $this->db->update_batch('kpi_aspek_kinerja_trx', $data, 'id_pi_trx');
        $this->nilai_individu_m->insertSkorTemporary('Aspek Kinerja');
        $this->db->trans_complete();
        __response_save($this->db->trans_status());
    }
}