<?php
require_once (PATH_TABLES . "suspect/SuspectContactTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class SuspectContactClass extends MasterBase {
    function __construct($db) {
        $this->db       = $db ;
        $this->tbl      = SuspectContactTable::C_TABLE ;
        $this->fldid2   = SuspectContactTable::C_ID ;
        $this->fldid    = SuspectContactTable::C_CONTACT_ID ;
        // $this->flddesc  = SuspectContactTable::C_NAME ;
    }
    function __destruct() {
    }

    function deleteSuspectContactByArray($array){
        $sql = "DELETE FROM " . SuspectContactTable::C_TABLE 
                ." WHERE " . SuspectContactTable::C_CONTACT_ID . " IN (" . $array . ")";
        $this->db->executeSql($sql);
    }
    function getConversationHistory($id, $contact_id){
        $sql = "SELECT ". ProspectConversationTable::C_DESC .
                       "," . ProspectConversationTable::C_CREATE_DATE .
                " FROM " . ProspectConversationTable::C_TABLE . " WHERE " . ProspectConversationTable::C_ID . " = " . $id . 
                                                                 " AND " . ProspectConversationTable::C_CONTACT_ID . " = " . $contact_id;

        $rows = $this->db->getTable($sql);
        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        return null;
    }
    
    function getSuspectContactByID($id){
        $sql = "SELECT * FROM " . SuspectContactTable::C_TABLE . " WHERE " . SuspectContactTable::C_ID . " = " . $id;

        $rows = $this->db->getTable($sql);
        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        return null;
    }
    // function getSuspectContact($id, $orderby='', $api=false){
    //     if ($api) {
    //          $sql = "SELECT ".$this->fldid2.",".$this->flddesc." FROM " ;
    //     } else {
    //         $sql = "SELECT * FROM " ;
    //     }

    //     $sql .= SuspectContactTable::C_TABLE . " WHERE " . SuspectContactTable::C_ID . " = " . $id;
    //     if($orderby != "")
    //         $sql .= " ORDER BY " . $orderby;

    //     $rows = $this->db->getTable($sql);

    //     if(!is_null($rows) && count($rows) > 0){
    //         return $rows;
    //     }

    //     return null;
    // }
    // function getSuspectContactByContactId($id, $contactId,$active=""){
    //     $sql = "SELECT * FROM " . SuspectContactTable::C_TABLE . " WHERE " . SuspectContactTable::C_ID . " = " . $id .
    //             " AND " . SuspectContactTable::C_CONTACT_ID . " = " . $contactId;

    //     if($active==""){
    //         $sql .= " AND " .SuspectContactTable::C_ACTIVE. "= 1 ";
    //     }
    //     $rows = $this->db->getTable($sql);

    //     if(!is_null($rows) && count($rows) > 0){
    //         return $rows[0];
    //     }

    //     return null;
    // }

    // function deleteSuspectContactByContactId($id, $contactId){
    //         $sql = "DELETE FROM " . SuspectContactTable::C_TABLE .
    //                 " WHERE " . SuspectContactTable::C_ID . " = " . $id .
    //                 " AND " . SuspectContactTable::C_CONTACT_ID . " = " . $contactId;

    //     return $this->db->executeSql($sql);
    // }
    // function deleteSuspectContact($id){
    //         $sql = "DELETE FROM " . SuspectContactTable::C_TABLE .
    //                 " WHERE " . SuspectContactTable::C_ID . " = " . $id;

    //     return $this->db->executeSql($sql);
    // }

    // function getSuspectContactbyDesignation($custId) {
    //     $sql = "SELECT * FROM " ;
    //     $sql .= SuspectContactTable::C_TABLE . " WHERE " . SuspectContactTable::C_ID . " = " . $custId;
    //     $sql .= " AND (" . SuspectContactTable::C_DESIGNATION . " LIKE '%Project Sponsor%'";
    //     $sql .= " OR " . SuspectContactTable::C_DESIGNATION . " LIKE '%Project Advisor%'";
    //     $sql .= " OR " . SuspectContactTable::C_DESIGNATION . " LIKE '%Project Manager%'";
    //     $sql .= " OR " . SuspectContactTable::C_DESIGNATION . " LIKE '%Finance%' )";

    //     $rows = $this->db->getTable($sql);

    //     if(!is_null($rows) && count($rows) > 0){
    //         $datas = [];
    //         $datas['project_sponsor'] = " ";
    //         $datas['project_advisor'] = " ";
    //         $datas['project_manager'] = " ";
    //         $datas['finance'] = " ";

    //         foreach ($rows as $row) {
    //             if(strpos($row[SuspectContactTable::C_DESIGNATION], 'Project Sponsor') !== false) {
    //                 $datas['project_sponsor'] = $row[SuspectContactTable::C_NAME];
    //             }
    //             if(strpos($row[SuspectContactTable::C_DESIGNATION], 'Project Advisor') !== false) {
    //                 $datas['project_advisor'] = $row[SuspectContactTable::C_NAME];
    //             }
    //             if(strpos($row[SuspectContactTable::C_DESIGNATION], 'Project Manager') !== false) {
    //                 $datas['project_manager'] = $row[SuspectContactTable::C_NAME];
    //             }
    //             if(strpos($row[SuspectContactTable::C_DESIGNATION], 'Finance') !== false) {
    //                 $datas['finance'] = $row[SuspectContactTable::C_NAME];
    //             }
    //         }
    //         return $datas;
    //     }

    //     return null;
    // }

    // function getSuspectFinanceAndIT($custId) {
    //     $sql = "SELECT TOP 4 * FROM " ;
    //     $sql .= SuspectContactTable::C_TABLE . " WHERE " . SuspectContactTable::C_ID . " = " . $custId;
    //     $sql .= " AND (" . SuspectContactTable::C_DESIGNATION . " LIKE '%Finance%'";
    //     $sql .= " OR " . SuspectContactTable::C_DESIGNATION . " LIKE '%IT%' )";
    //     $sql .= " ORDER BY " . SuspectContactTable::C_CONTACT_ID . " ASC";

    //     $rows = $this->db->getTable($sql);

    //     if(!is_null($rows) && count($rows) > 0){
    //         $datas = [];
    //         $datas['it'] = "";
    //         $datas['finance'] = "";
    //         foreach ($rows as $row) {
    //             if(strpos($row[SuspectContactTable::C_DESIGNATION], 'Finance') !== false) {
    //                 if($datas['finance']=="") {
    //                     $datas['finance'] = $row[SuspectContactTable::C_NAME];
    //                 } else {
    //                     $datas['finance2'] = $row[SuspectContactTable::C_NAME];
    //                 }
    //             }
    //             if(strpos($row[SuspectContactTable::C_DESIGNATION], 'IT') !== false) {
    //                 if($datas['it']=="") {
    //                     $datas['it'] = $row[SuspectContactTable::C_NAME];
    //                 } else {
    //                     $datas['it2'] = $row[SuspectContactTable::C_NAME];
    //                 }
    //             }
    //         }
    //         // Log::write(print_r($datas,true));
    //         return $datas;
    //     }

    //     return null;
    // }

    // function getIdByContactName($cust_id, $desc) {
    //     $sql = "select " . $this->fldid . " from " . $this->tbl
    //     . " where " . $this->db->fieldParam($this->flddesc)
    //     . " and " . $this->db->fieldParam($this->fldid2);

    //     $params = array();
    //     $params[] = $this->db->valueParam($this->flddesc, strtolower($desc));
    //     $params[] = $this->db->valueParam($this->fldid2, $cust_id);

    //     $rows = $this->db->getRow($sql, $params);
    //     // Log::write(print_r($sql, true));
    //     if (is_null($rows) || count($rows) == 0)
    //         return "";
    //     else
    //         return $rows[0][$this->fldid];
    // }

    // public function getExistingRecords($cust_id, $contact_name)
    // {
    //     $sql  = "SELECT * FROM " . SuspectContactTable::C_TABLE .
    //         " WHERE " . SuspectContactTable::C_ID . " = '" . $cust_id . "' " .
    //         " AND " . SuspectContactTable::C_NAME . " = '" . $contact_name . "' ";

    //     $rows = $this->db->getTable($sql);

    //     if (is_null($rows) || count($rows) == 0) {
    //         return null;
    //     } else {
    //         return $rows;
    //     }
    // }
}
