<?php
class Cust extends Cmanage {

  function __construct(){
    parent::__construct(TB_cust_list);
    $this->DB->tableExists(TB_cust_list);
  }

  public function getCustList($fields, $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    $where->add("cust_active = ?", 1);
    $orderby="cust_name";
    $limit=array();

    $rows = $this->getDataList($fields, $where, $orderby, $limit);
    for( $a=0 ; $a<@count($rows) ; $a++ ){
      if( $rows[$a]['cust_phone'] ) $rows[$a]['cust_phone'] = $this->lcrypt->dec_secure($rows[$a]['cust_phone']);
      if( $rows[$a]['cust_mail'] ) $rows[$a]['cust_mail'] = $this->lcrypt->dec_secure($rows[$a]['cust_mail']);
    }
    return $rows;
  }

  public function getCustSelect($key, $val, $where=""){
    $rows = $this->getDataList("{$key}, {$val}", $where, "{$val} asc");
    $selrows = array();
    foreach( (array)$rows as $row ){
      $selrows[$row[$key]] = $row[$val];
    }
    return $selrows;
  }

  public function getCustPage($sqldata, $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    $where->add("cust_active = ?", 1);

    if( $sqldata['stxt'] ){
      $subwhere = $where->sub_where("or");
      if( $sqldata['sfld'] ){
        $subwhere->add("{$sqldata['sfld']} like ?", "%{$sqldata['stxt']}%");
      }else{
        $subwhere->add("cust_id       like ?", "%{$sqldata['stxt']}%");
        $subwhere->add("cust_name     like ?", "%{$sqldata['stxt']}%");
        $subwhere->add("cust_fullname like ?", "%{$sqldata['stxt']}%");
        $subwhere->add("cust_charger  like ?", "%{$sqldata['stxt']}%");
      }
    }
    if( hasval($sqldata['cust_status']) ){
      $where->add("cust_status = ?", $sqldata['cust_status']);
    }

    $orderby = "reg_date desc";
    if( $sqldata['ofld'] && $sqldata['oasc'] ){
      $orderby = "{$sqldata['ofld']} {$sqldata['oasc']}";
    }

    $query = array(
      "mode"     => $sqldata['mode'],
      "viewnum"  => $sqldata['viewnum'],
      "page"     => $sqldata['page'],
      "sfld"     => $sqldata['sfld'],
      "stxt"     => $sqldata['stxt'],
      "ofld"     => $sqldata['ofld'],
      "oasc"     => $sqldata['oasc'],

      "cust_status" => $sqldata['cust_status'],
    );

    $lists = $this->getDataPage("*", $query, $where ,$orderby);
    for( $a=0 ; $a<@count($lists->rows) ; $a++ ){
      if( $lists->rows[$a]['cust_phone'] ) $lists->rows[$a]['cust_phone'] = $this->lcrypt->dec_secure($lists->rows[$a]['cust_phone']);
      if( $lists->rows[$a]['cust_mail'] ) $lists->rows[$a]['cust_mail'] = $this->lcrypt->dec_secure($lists->rows[$a]['cust_mail']);
    }
    return $lists;
  }

  public function getCust( $cust_id ){
    $urow = Array("cust_id" => $cust_id);
    $where = "";
    $row = $this->getData("*", $urow, $where);
    $row['cust_phone'] = $this->lcrypt->dec_secure($row['cust_phone']);
    $row['cust_mail'] = $this->lcrypt->dec_secure($row['cust_mail']);
    return $row;
  }

  public function putCust( $sqldata ) {
    if( $sqldata['cust_phone'] ){
      $sqldata['cust_phone'] = $this->lcrypt->enc_secure($sqldata['cust_phone']);
    }
    if( $sqldata['cust_mail'] ){
      $sqldata['cust_mail'] = $this->lcrypt->enc_secure($sqldata['cust_mail']);
    }
    return $this->putData($sqldata);
  }

  public function setCust( $sqldata ) {
    $cinfo = $this->getCust($sqldata['cust_id']);
    if( $sqldata['cust_id'] && $cinfo['cust_id'] == $sqldata['cust_id'] ){
      if( $sqldata['cust_phone'] ){
        $sqldata['cust_phone'] = $this->lcrypt->enc_secure($sqldata['cust_phone']);
      }
      if( $sqldata['cust_mail'] ){
        $sqldata['cust_mail'] = $this->lcrypt->enc_secure($sqldata['cust_mail']);
      }
      $urow = Array("cust_id" => $sqldata['cust_id']);
      $where = "";
      unset($sqldata['cust_id']);
      return $this->setData($sqldata, $urow, $where);
    }else{
      return $this->putCust( $sqldata );
    }
  }

  public function setCustParts( $sqldata, $field="", $row="", $where="" ){
    return $this->setDataParts($sqldata, $field, $row, $where);
  }

  public function delCust( $cust_id, $where ){
    $urow = Array("cust_id" => $cust_id);
    $where = "";
    return $this->delData($urow, $where);
  }

}