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

class Dna extends CI_Controller {

  public function __construct()
  {
      parent::__construct();    
      $this->load->helper('download');
      $this->load->library('excel'); 
  } 
  function diamond_details()
  {
      header('X-Robots-Tag: noindex');
      $packet_no = $this->uri->segment(2);
      $record = array();
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $curl = curl_init();
      curl_setopt_array($curl, array(
        CURLOPT_URL => APIURL."/get-token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "username=".APIUSER."&password=".APIPASSWORD."",
        CURLOPT_HTTPHEADER => array(
          "Accept: */*",
          "Content-Type: application/x-www-form-urlencoded",
        ),
      ));
      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);
      if (empty($err)){
        $response = json_decode($response, true);
        $token = $response['token'];
      }
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $curl = curl_init();
      curl_setopt_array($curl, array(
        CURLOPT_URL => APIURL."/diamond-dna/".$packet_no,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
          "Accept: */*",
          "Authorization: Bearer ".$token         
        ),
      ));
      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);
      if (empty($err)){
        $response = json_decode($response);
        $record = $response->record;
      }     
      //print_ex($record[0]->PACKET_NO);
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $data_video360 = '';
      $data_cert = '';
      $data_still = '';
      $data_heart = '';
      $data_arrow = '';
      $data_asset = '';
      $data_white = '';
      $data_real = '';
      $data_sparklevideo = '';
      $data_whitevideo = '';
      $data_fluorvideo = '';
      $data_blackvideo = '';

      if(!empty($record)) {
          
          $video360 = DNA_URL.$record['0']->PACKET_NO."/".$record['0']->PACKET_NO.".html";

          if(checkLink($video360)) {
            $data_video360 = $video360;
          }

          $cert = DNA_URL."certificate_Images/".$record['0']->REPORT_NO.".pdf";
          if(checkLink($cert)=='200') {
            $data_cert = $cert;
          }
          $still = DNA_URL.$record['0']->PACKET_NO."/still.jpg";
          if(checkLink($still)=='200') {
            $data_still = $still;
          }
          $heart = DNA_URL."heart/".$record['0']->PACKET_NO.".jpg";
          if(checkLink($heart)=='200') {
            $data_heart = $heart;
          }
          $arrow = DNA_URL."arrow/".$record['0']->PACKET_NO.".jpg";
          if(checkLink($arrow)=='200') {
            $data_arrow = $arrow;
          }
          $asset = DNA_URL."asset/".$record['0']->PACKET_NO.".jpg";
          if(checkLink($asset)=='200') {
            $data_asset = $asset;
          }
          $white = DNA_URL."white/".$record['0']->PACKET_NO.".jpg";
          if(checkLink($white)=='200') {
            $data_white = $white;
          }
          $real = DNA_URL."realimage/".$record['0']->PACKET_NO.".jpg";
          if(checkLink($real)=='200') {
            $data_real = $real;
          }          
          $hand_image = DNA_URL."hand_images/".$record['0']->PACKET_NO.".html";
          if(checkLink($hand_image)=='200') {
            $data_hand_image = $hand_image;
          }

          $sparklevideo = DNA_URL."sparklevideo/".$record['0']->PACKET_NO.".html";
          if(checkLink($sparklevideo)=='200') {
            $data_sparklevideo = $sparklevideo;
          }
          $whitevideo = DNA_URL."whitevideo/".$record['0']->PACKET_NO.".html";
          if(checkLink($whitevideo)=='200') {
            $data_whitevideo = $whitevideo;
          }
          $fluorvideo = DNA_URL."fluorescencevideo/".$record['0']->PACKET_NO.".html";
          if(checkLink($fluorvideo)=='200') {
            $data_fluorvideo = $fluorvideo;
          }
          $blackvideo = DNA_URL."blackvideo/".$record['0']->PACKET_NO.".html";
          if(checkLink($blackvideo)=='200') {
            $data_blackvideo = $blackvideo;
          }
      } 
      $data = array(
        'record' => $record,
        'data_video360' => $data_video360,
        'data_cert' => $data_cert,
        'data_still' => $data_still,
        'data_heart' =>  $data_heart,
        'data_arrow' => $data_arrow,
        'data_asset' => $data_asset,
        'data_white' => $data_white,
        'data_real' => $data_real,
        'data_hand_image' => $data_hand_image,
        'data_sparklevideo' => $data_sparklevideo,
        'data_whitevideo' => $data_whitevideo,
        'data_fluorvideo' => $data_fluorvideo,
        'data_blackvideo' => $data_blackvideo,
      );
      //print_ex($data);
      $this->load->view('diamond/dna',$data);      

  }
  function download(){
      $stock = $this->input->get('stock');
      $type = $this->input->get('type');
      if(trim($stock)=='' || trim($type)==''){
        redirect($_SERVER['HTTP_REFERER']);
      }

      if($type=='image') {
          $zip = new ZipArchive();
          $zipFile = $stock."Image".time().".zip";

          if ($zip->open("uploads/".$zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
               die ("Could not open archive");
          }
          $file_content = file_get_contents(DNA_URL.$stock.'/still.jpg');
          $zip->addFromString('still.jpg', $file_content);

          $heart = DNA_URL."heart/".$stock.".jpg";
          if(checkLink($heart)=='200') {
            $file_content = file_get_contents($heart);
            $zip->addFromString('heart.jpg', $file_content);
          }          

          $arrow = DNA_URL."arrow/".$stock.".jpg";
          if(checkLink($arrow)=='200') {
            $file_content = file_get_contents($arrow);
            $zip->addFromString('arrow.jpg', $file_content);
          }          

          $asset = DNA_URL."asset/".$stock.".jpg";
          if(checkLink($asset)=='200') {
            $file_content = file_get_contents($asset);
            $zip->addFromString('assetScope.jpg', $file_content);
          }          

          $white = DNA_URL."white/".$stock.".jpg";
          if(checkLink($white)=='200') {
            $file_content = file_get_contents($white);
            $zip->addFromString('white.jpg', $file_content);
          }

          $zip->close();

          header('Content-disposition: attachment; filename='.$zipFile);
          header('Content-type: application/zip');
          readfile("uploads/".$zipFile);

          unlink("uploads/".$zipFile);          
      }      
      else if($type=='cert') {
          $zip = new ZipArchive();
          $zipFile = $stock."Cert".time().".zip";

          if ($zip->open("uploads/".$zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
               die ("Could not open archive");
          }
          $file_content = file_get_contents(DNA_URL.'certificate_Images/'.$stock.'.pdf');
          $zip->addFromString($stock.'.pdf', $file_content);
          $zip->close();

          header('Content-disposition: attachment; filename='.$zipFile);
          header('Content-type: application/zip');
          readfile("uploads/".$zipFile);

          unlink("uploads/".$zipFile);          
      }      
      else if($type=='video') {
          $zip = new ZipArchive();
          $zipFile = $stock."Video".time().".zip";

          if ($zip->open("uploads/".$zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
               die ("Could not open archive");
          }
          $file_content = file_get_contents(DNA_URL.$stock.'/video.mp4');
          $zip->addFromString($stock.'.mp4', $file_content);
          $zip->close();

          header('Content-disposition: attachment; filename='.$zipFile);
          header('Content-type: application/zip');
          readfile("uploads/".$zipFile);

          unlink("uploads/".$zipFile);          
      }

  }

  function export_diamond()
  {        
    $where = ""; 
    $record = array();
    $stock = $this->input->get('stock');
    $type = $this->input->get('type');
    if(trim($stock)=='' || trim($type)==''){
      redirect($_SERVER['HTTP_REFERER']);
    } 
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => APIURL."/get-token",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "username=".APIUSER."&password=".APIPASSWORD."",
      CURLOPT_HTTPHEADER => array(
        "Accept: */*",
        "Content-Type: application/x-www-form-urlencoded",
      ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if (empty($err)){
      $response = json_decode($response, true);
      $token = $response['token'];
    }
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => APIURL."/diamond-dna/".$stock,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "Accept: */*",
        "Authorization: Bearer ".$token         
      ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if (empty($err)){
      $response = json_decode($response);
      $record = $response->record;
    }     
    //print_ex($record);
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++       
  
    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle('Stock');
    $styleArray = array(
      'font'  => array(
          'color' => array('rgb' => '8c0808'),
          'size'  => 18,
          'name' => 'Verdana',
          'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,              
    )); 

    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('0','1' ,'Stock#');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('1','1' ,'Shape');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('2','1' ,'Status');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('3','1' ,'Cts');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('4','1' ,'Color');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('5','1' ,'Clarity');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('6','1' ,'Cut');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('7','1' ,'Pol');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('8','1' ,'Sym');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('9','1' ,'Fluor');    
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('10','1' ,'Disc%');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('11','1' ,'Depth%');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('12','1' ,'Table%');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('13','1' ,'Rap.($)');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('14','1' ,'$/Carat');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('15','1' ,'Total $');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('16','1' ,'Descrip');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('17','1' ,'Lab');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('18','1' ,'Pic');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('19','1' ,'Brand');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('20','1' ,'Measurements');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('21','1' ,'Report No');
  
    $this->excel->getActiveSheet()->getStyle('A1:V1')
      ->applyFromArray(
          array(
              'font' => array(                    
                  'color' => array('rgb' => 'ffffff'),
                  'size'  => 11,
                  'name' => 'Calibri'
              ),
              'fill' => array(
                  'type' => PHPExcel_Style_Fill::FILL_SOLID,
                  'color' => array('rgb' => '17375d')
              )
          )
      );
    $i=2;       
    foreach($record as $row) 
    {   
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('0',$i,$row->PACKET_NO);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('1',$i,$row->SHAPE_SEQ);         
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('2',$i,$row->MemoStatus);         
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('3',$i,number_format($row->CTS,2));
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('4',$i,$row->COLOR_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('5',$i,$row->PURITY_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('6',$i,$row->CUT_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('7',$i,$row->POLISH_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('8',$i,$row->SYMM_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('9',$i,$row->FLS_SEQ);        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('10',$i,number_format($row->DISC_PER,1));
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('11',$i,number_format($row->DEPTH_PER,1));
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('12',$i,(int)$row->TABLE_PER);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('13',$i,round($row->RATE));        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('14',$i,number_format($row->NET_RATE,2));        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('15',$i,number_format($row->NET_VALUE,2));        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('16',$i,$row->DESCRIPTION);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('17',$i,$row->LAB_SEQ);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('18',$i,'DNA');       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('19',$i,$row->HA);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('20',$i,$row->LWD);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('21',$i,$row->REPORT_NO); 

        $link = "certificate_Images/".$row->REPORT_NO.".pdf";
        $this->excel->getActiveSheet()->getCellByColumnAndRow(17,$i)->getHyperlink()->setUrl($link);        

        $link = "https://dna.dnalinks.in/d/".$row->PACKET_NO;
        $this->excel->getActiveSheet()->getCellByColumnAndRow(18,$i)->getHyperlink()->setUrl($link);

        $this->excel->getActiveSheet()->getStyle('R'.$i.':S'.$i.'')
          ->applyFromArray(
              array(
                  'font' => array(                    
                      'color' => array('rgb' => '0000FF'),
                      'size'  => 11,
                      'name' => 'Calibri',
                      'underline' => 'single'
                  )
              )
          );

        $NET_RATE += $row->NET_RATE;
        $NET_VALUE += $row->NET_VALUE;
        $i++;
    }

    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('0',$i,count($records->record));
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('14',$i,$NET_RATE);
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('15',$i,$NET_VALUE);
    //$this->excel->getActiveSheet()->setAutoFilter('A1:V1');
    foreach (range('A', $this->excel->getActiveSheet()->getHighestDataColumn()) as $col) {
        $this->excel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
    } 
    $this->excel->getActiveSheet()->getStyle('A'.$i.':V'.$i.'')
      ->applyFromArray(
          array(
              'font' => array(                    
                  'color' => array('rgb' => '000000'),
                  'size'  => 11,
                  'name' => 'Calibri'
              ),
              'fill' => array(
                  'type' => PHPExcel_Style_Fill::FILL_SOLID,
                  'color' => array('rgb' => 'd3d3d3')
              )
          )
      );
      $this->excel->getActiveSheet()
        ->getStyle( $this->excel->getActiveSheet()->calculateWorksheetDimension() )
        ->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
    
      $filename='Stock_'.date('m_d_Y_h_i_s_A').'.xls'; 
      header('Content-Type: application/vnd.ms-excel'); 
      header('Content-Disposition: attachment;filename="'.$filename.'"'); 
      header('Cache-Control: max-age=0');                     
      //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
      //if you want to save it as .XLSX Excel 2007 format
      $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
      //force user to download the Excel file without writing it to server's HD
      $objWriter->save('php://output');
  }

  
}