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

class Anggota extends CI_Controller
{

  public function __construct()
  {
    parent::__construct();
    // Codeigniter : Write Less Do More
    $this->load->model("M_anggota");
    $this->load->model("M_detail_hutang_anggota");
  }

  public function index()
  {
    echo "Anggota";
    // $data=array(
    //   'data' => array(
    //     'anggota' => $this->M_anggota->get_data($_SESSION['idpusat']),
    //   ),
    //   'content' => 'Anggota/data_anggota'
    // );
    // $this->load->view('template/template', $data);
  }

  public function detail($id)
  {
    $data = array(
      'data' => array(
        "anggota" => $this->M_detail_hutang_anggota->get_anggota($id)->row_array()
      ),
      'content' => 'Anggota/detail_anggota'
    );
    $this->load->view('template/template', $data);
  }

  function update_anggota()
  {
    $id = $this->input->post("id");
    $data = array(
      'no_anggota'  => $this->input->post("no_anggota"),
      'nama'        => $this->input->post("nama"),
      'jekel'       => $this->input->post("jekel"),
      'status_anggota'       => $this->input->post("status_anggota"),
      'telp'        => $this->input->post("telp"),
      'alamat'      => $this->input->post("alamat"),
    );
    $this->db->where('iddata_user', $id);
    if ($this->db->update('data_user', $data)) {
      $this->session->set_flashdata('notif', $this->notif->berhasil("Berhasil Update Data"));
      redirect(base_url() . 'Anggota/Anggota');
    } else {
      $this->session->set_flashdata('notif', $this->notif->gagal("Gagal Update Data"));
      redirect(base_url() . 'Anggota/Anggota');
    }
  }

  public function editKurir($id)
  {
    $data = array(
      'data' => array(
        "anggota" => $this->M_detail_hutang_anggota->get_anggota($id)->row_array(),
        "kurir"   => $this->M_anggota->getKurir(),
      ),
      'content' => 'Anggota/PilihKurir'
    );
    $this->load->view('template/template', $data);
  }

  public function insertKurir($id)
  {
    $data = array(
      'idkurir' => $this->input->post("idkurir"),
    );
    $this->db->where("data_user_iddata_user", $id);
    if ($this->db->update('user', $data)) {
      $this->session->set_flashdata('notif', $this->notif->berhasil("Berhasil Menambah Data"));
    } else {
      $this->session->set_flashdata('notif', $this->notif->gagal("Gagal Menambah Data"));
    }
    redirect(base_url() . 'Anggota/Anggota');
  }

  public function input()
  {
    $data = array(
      'data' => array(),
      'content' => 'Anggota/input_anggota'
    );
    $this->load->view('template/template', $data);
  }

  public function insert_anggota()
  {
    $noanggota = @$this->input->post("no_anggota");
    $email = $this->input->post("email");
    $status_anggota = "1";
    $data = array(
      'no_anggota' => $noanggota,
      'status_anggota' => $status_anggota,
      'nama' => $this->input->post("nama"),
      'pusat_idpusat' => $_SESSION['idpusat'],
      'email' => $this->input->post("email"),
      'telp' => $this->input->post("telp"),
      'alamat' => $this->input->post("alamat"),
      'create_on' => date("Y-m-d H:i:s"),
    );
    $email = $this->db->get_where("data_user", array('email' => $email))->num_rows();
    if ($email < 1 || $email == "") {
      $username = $this->input->post("telp");
      $cek_user = $this->db->get_where("user", array('username' => $username));
      if ($cek_user->num_rows() < 1) {
        if ($this->db->insert('data_user', $data)) {
          $id = $this->db->insert_id();
          $pass = "belankon123";
          $roles = "user";
          $data2 = array(
            'username'    => $username,
            'password' => password_hash($pass, PASSWORD_DEFAULT, array("cost" => 10)),
            'data_user_iddata_user' => $id,
            'roles'   => json_encode($roles)
          );
          $this->db->insert("user", $data2);
          $data['id'] = $id;
          $this->session->set_flashdata('notif', $this->notif->berhasil("Berhasil Menambah Data"));
          redirect(base_url() . 'Anggota/Anggota');
        } else {
          $this->session->set_flashdata('notif', $this->notif->gagal("Daftar Anda Gagal, Cek Kembali Koneksi Anda!"));
          redirect(base_url() . 'Anggota/Anggota');
        }
      } else {
        $this->session->set_flashdata('notif', $this->notif->gagal("Mohon Maaf No.Telp Sudah Digunakan"));
        redirect(base_url() . 'Anggota/Anggota');
      }
    } else {
      $this->session->set_flashdata('notif', $this->notif->gagal("Mohon Maaf Email Sudah Digunakan"));
      redirect(base_url() . 'Anggota/Anggota');
    }
  }
}
