<?php
if (!defined('BASEPATH'))
  exit('No direct script access allowed');
class District_head_model extends MY_Model {

  public function get_district_heads($limit = '10', $offset = '0', $param = array()) {

    $admin_type = @$param['admin_type'];
    $status = @$param['status'];
    $admin_id = @$param['admin_id'];
    $state = @$param['state'];
    $city = @$param['city'];

    $admin_country = @$param['admin_country'];
    $admin_state = @$param['admin_state'];
    $admin_district = @$param['admin_district'];
    $admin_block = @$param['admin_block'];
    $admin_panchayat = @$param['admin_panchayat'];
    $admin_village = @$param['admin_village'];

    $keyword = trim($this->input->get_post('keyword', TRUE));
    $keyword = $this->db->escape_str($keyword);

    

    if ($admin_id != '') {
      $this->db->where("admin_id", "$admin_id");
    }

    if ($admin_type != '') {
      $this->db->where("admin_type", "$admin_type");
    }
    if ($status != '') {
      $this->db->where("status", "$status");
    }
    if ($state != '') {
      $this->db->where("state", "$state");
    }

    if ($city != '') {
      $this->db->where("city", "$city");
    }

    if ($admin_country != '') {
      $this->db->where("admin_country", $admin_country);
    }
    if ($admin_state != '') {
      $this->db->where("admin_state", $admin_state);
    }
    if ($admin_district != '') {
      $this->db->where("admin_district", $admin_district);
    }
    if ($admin_block != '') {
      $this->db->where("admin_block", $admin_block);
    }
    if ($admin_panchayat != '') {
      $this->db->where("admin_panchayat", $admin_panchayat);
    }
    if ($admin_village != '') {
      $this->db->where("admin_village", $admin_village);
    }

    if ($keyword != '') {
      $this->db->where("(admin_name LIKE '%" . $keyword . "%' OR admin_email LIKE '%" . $keyword . "%' OR phone LIKE '%" . $keyword . "%' )");
    }

    $this->db->order_by('admin_id', 'desc');
    if($limit=='Unlimited'){
      $this->db->limit(100, $offset);
    }else{
      $this->db->limit($limit, $offset);
    }

    $this->db->select("SQL_CALC_FOUND_ROWS * ", FALSE);
    $this->db->from('wps_admin');
    $this->db->where('status !=', '2');
    $q = $this->db->get();
    //echo_sql();
    $result = $q->result_array();
    $result = ($limit == '1') ? $result[0] : $result;
    return $result;

  }



  public function get_district_head_row($id, $condtion = '') {

    $id = (int) $id;

    if ($id != '' && is_numeric($id)) {

      $condtion = "status !='2' AND admin_id=$id $condtion ";

      $fetch_config = array(

          'condition' => $condtion,

          'debug' => FALSE,

          'return_type' => "array"

      );

      $result = $this->find('wps_admin', $fetch_config);

      return $result;

    }

  }

  public function update_password($old_pass, $id) {

    $cond = "admin_id ='".$id."' AND admin_password ='".$old_pass."'";
    $num_row = $this->findCount('wps_admin', $cond);
    if ($num_row > 0) {
      $data = array(
          'admin_password' => $this->input->post('new_pass'),
      );
      $where = "admin_id=" . $id . " ";
      $this->safe_update('wps_admin', $data, $where, FALSE);

      $this->session->set_userdata('msg_type', "success");
      $this->session->set_flashdata('success', lang('successupdate'));
    } else {
      $this->session->set_userdata(array('msg_type' => 'error'));
      $this->session->set_flashdata('error', lang('password_incorrect'));
    }
  }

  public function update_profile($id,$uploaded_file) {
    $cond = "admin_id = '" . $id . "'";
    $num_row = $this->findCount('wps_admin', $cond);
    if ($num_row > 0) {
      $data = array(
          'admin_name' => $this->input->post('admin_name'),
          'admin_email' => $this->input->post('admin_email'),
          'admin_image' => $uploaded_file,
          'phone' => $this->input->post('phone'),
          'address' => $this->input->post('address'),
          //'facebook' => $this->input->post('facebook'),
          //'twitter' => $this->input->post('twitter'),
          //'linkedin' => $this->input->post('linkedin'),
          //'youtube' => $this->input->post('youtube'),
          //'whatsapp' => $this->input->post('whatsapp'),
          
      );
      $where = "admin_id=" . $id . " ";
      $this->safe_update('wps_admin', $data, $where, FALSE);
      $this->session->set_userdata('msg_type', "success");
      $this->session->set_flashdata('success', lang('successupdate'));
    } else {
      $this->session->set_userdata(array('msg_type' => 'error'));
      $this->session->set_flashdata('error', "Something went wrong, please try again!");
    }
  }


/*
  public function get_kisan_mitra_factsheet($id, $condtion = '') {

    $id = (int) $id;

    if ($id != '' && is_numeric($id)) {

      $condtion = "admin_id = '" . $id . "' $condtion ";

      $fetch_config = array(

          'condition' => $condtion,

          'debug' => FALSE,

          'return_type' => "array"

      );

      $result = $this->find('wps_admin_factsheet', $fetch_config);

      return $result;

    }

  }*/

}

