<?php
include "../../inc/config.php";

require_once '../../lib/Writer.php';



if (isset($_POST['provinsi'])) {
    if ($_POST['provinsi']=='all') {
        $provinsi = '';
        $nama_provinsi = "";
    } else {
        $provinsi = " and data_wilayah.id_wil='".$_POST['provinsi']."'";
        $nama_provinsi = $db->fetch_single_row("data_wilayah",'id_wil',$_POST['provinsi']);
        $nama_provinsi = preg_replace("/[[:blank:]]+/"," ",$nama_provinsi->nm_wil);
        $nama_provinsi = str_replace(" ","_",$nama_provinsi);
    }

    if ($_POST['kabupaten']=='all') {
      $kabupaten = '';
       $nama_kabupaten = "";
    } else {
      $kabupaten = " and dw.id_wil='".$_POST['kabupaten']."'";
      $nama_kabupaten = $db->fetch_single_row("data_wilayah",'id_wil',$_POST['kabupaten']);
      $nama_kabupaten = preg_replace("/[[:blank:]]+/"," ",$nama_kabupaten->nm_wil);
        $nama_kabupaten = str_replace(" ","_",$nama_kabupaten);
     
    }

        }


$writer = new XLSXWriter();
$style =
        array (
                      array(
              'border' => array(
                'style' => 'thin',
                'color' => '000000'
                ),
            'allfilleddata' => true
            ),
            array(
                'fill' => array(
                    'color' => '00ff00'),
                'cells' => array(
                    'A1',
                    'B1',
                    'C1',
                    'D1',
                    'E1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                ),
            )
        ;



//column width
$col_width = array(
  1 => 7,
  2 => 19,
  3 => 27,
  4 => 35,
  5 => 19
  );
$writer->setColWidth($col_width);


$header = array(
  'No'=>'string',
  'Provinsi'=>'string',
  'Kabupaten'=>'string',
  'Kecamatan'=>'string',
  'ID Kecamatan'=>'@',
);



  
$no=1;
$index = 2;

$data_rec = array();

        $order_by = "order by kode_mk,nama_kelas ASC";

    
        $temp_rec = $db->fetch_custom("select dwc.id_wil as id_wil, data_wilayah.nm_wil as provinsi,dw.nm_wil as kab,dwc.nm_wil as kecamatan from data_wilayah
inner join data_wilayah dw on data_wilayah.id_wil=dw.id_induk_wilayah
inner join data_wilayah dwc on dw.id_wil=dwc.id_induk_wilayah
where data_wilayah.id_level_wil='1' $provinsi $kabupaten");
                    foreach ($temp_rec as $key) {
          
            $data_rec[] = array($no,$key->provinsi,$key->kab,$key->kecamatan,"$key->id_wil");

             $no++;
             $index++;

            }


$filename = 'Data Wilayah '.$nama_provinsi.$nama_kabupaten.'.xlsx';
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$writer->writeSheet($data_rec,'Data Wilayah', $header, $style);
$writer->writeToStdOut();
exit(0);

?>