<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// ============================================================
//  HELPER NWS (NOTE WE ARE SORRY) — QC LAUNDRY
// ============================================================
if (!function_exists('az_nws_ensure')) {
function az_nws_ensure() {
    $ci =& get_instance();
    $ci->db->query("CREATE TABLE IF NOT EXISTS `nws` (
        `id` INT NOT NULL AUTO_INCREMENT,
        `idtransaction_group` INT NOT NULL,
        `idtransaction` INT NOT NULL,
        `num` INT NOT NULL DEFAULT 1,
        `kategori` VARCHAR(10) NOT NULL,
        `posisi` VARCHAR(120) NOT NULL DEFAULT '',
        `keterangan` VARCHAR(160) NOT NULL DEFAULT '',
        `note_manual` TEXT,
        `iduser` INT DEFAULT NULL,
        `created` DATETIME DEFAULT NULL,
        `status` INT NOT NULL DEFAULT 1,
        PRIMARY KEY (`id`), KEY `idx_grp` (`idtransaction_group`), KEY `idx_trx` (`idtransaction`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
    $ci->db->query("CREATE TABLE IF NOT EXISTS `nws_option` (
        `id` INT NOT NULL AUTO_INCREMENT,
        `jenis` VARCHAR(30) NOT NULL,
        `name` VARCHAR(120) NOT NULL,
        `status` INT NOT NULL DEFAULT 1,
        PRIMARY KEY (`id`), KEY `idx_jenis` (`jenis`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
    $ci->db->query("CREATE TABLE IF NOT EXISTS `nws_photo` (
        `id` INT NOT NULL AUTO_INCREMENT,
        `idtransaction` INT NOT NULL,
        `photo` VARCHAR(255) NOT NULL,
        `status` INT NOT NULL DEFAULT 1,
        PRIMARY KEY (`id`), KEY `idx_trx` (`idtransaction`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
}
}
if (!function_exists('az_nws_options')) {
function az_nws_options() {
    $ci =& get_instance(); az_nws_ensure();
    $out = array('posisi_pakaian'=>array(),'posisi_linen'=>array(),'ket_noda'=>array(),'ket_kusut'=>array());
    $ci->db->where('status',1)->order_by('id','asc');
    foreach ($ci->db->get('nws_option')->result() as $r) {
        if (isset($out[$r->jenis])) $out[$r->jenis][] = $r->name;
    }
    return $out;
}
}
if (!function_exists('az_nws_group')) {
function az_nws_group($idgroup) {
    $ci =& get_instance(); az_nws_ensure();
    $ci->db->where('idtransaction_group', $idgroup)->where('status',1);
    $ci->db->order_by('num asc, id asc');
    $map = array();
    foreach ($ci->db->get('nws')->result() as $r) {
        $map[$r->idtransaction][] = array(
            'id'=>(int)$r->id, 'num'=>(int)$r->num, 'kategori'=>$r->kategori,
            'posisi'=>$r->posisi, 'keterangan'=>$r->keterangan, 'note_manual'=>$r->note_manual,
        );
    }
    return $map;
}
}
if (!function_exists('az_nws_photos')) {
function az_nws_photos($ids) {
    $map = array(); if (empty($ids)) return $map;
    $ci =& get_instance(); az_nws_ensure();
    $ci->db->where_in('idtransaction', $ids)->where('status',1);
    foreach ($ci->db->get('nws_photo')->result() as $r) $map[$r->idtransaction] = $r->photo;
    return $map;
}
}
if (!function_exists('az_nws_phrase')) {
function az_nws_phrase($n) {
    $k = $n['kategori']; $pos = trim((string)$n['posisi']); $ket = trim((string)$n['keterangan']); $man = trim((string)$n['note_manual']);
    if ($k == 'NODA') { $s = 'noda'; if ($ket !== '') $s .= ' '.strtolower($ket); if ($pos !== '') $s .= ' di bagian '.strtolower($pos); return $s; }
    if ($k == 'KUSUT') { $s = ($ket !== '') ? strtolower($ket) : 'kusut'; if ($pos !== '') $s .= (stripos($pos,'menyeluruh')!==false) ? ' menyeluruh' : ' di bagian '.strtolower($pos); return $s; }
    if ($k == 'BAU')  return 'bau yang masih tersisa';
    return ($man !== '') ? $man : 'catatan QC';
}
}
if (!function_exists('az_nws_sentence')) {
function az_nws_sentence($notes) {
    $parts = array();
    foreach ($notes as $n) $parts[] = az_nws_phrase($n);
    $parts = array_values(array_unique($parts));
    if (count($parts) == 0) return '';
    if (count($parts) == 1) return $parts[0];
    $last = array_pop($parts);
    return implode(', ', $parts).' serta '.$last;
}
}