<?php
include "classes/db.class.php";

$db = new db();


if (isset($_POST['edit'])) {


    $index = 0;
    $questions = '';
    $answers = '';
    $warning = '';

    while ($index < $_POST['index']) {
        if (strlen($_POST['q_' . $index]) > 1) {

            $questions = $questions . strtoupper($_POST['q_' . $index]) . ',';
            $answers = $answers . strtoupper($_POST['a_' . $index]) . ',';
            $warning = $warning . strtoupper($_POST['w_' . $index]) . ',';
        }
        $index++;
    }


    $res = $db->Update('pre_inspection_content', ['name', 'question', 'answer_type', 'warning'], [$_POST['name'], $questions, $answers, $warning], "record_id = {$_POST['list_id']}");
    if ($res) {
        echo "<script>window.location.href = 'inspection_lists.php'; </script>";
        // echo 1;
    } else {
        echo 0;
    }
} else {

    $index = 0;
    $questions = '';
    $answers = '';
    $warning = '';

    while ($index < $_POST['index']) {
        if (strlen($_POST['q_' . $index]) > 1) {

            $questions = $questions . strtoupper($_POST['q_' . $index]) . ',';
            $answers = $answers . strtoupper($_POST['a_' . $index]) . ',';
            $warning = $warning . strtoupper($_POST['w_' . $index]) . ',';
        }
        $index++;
    }

    $res = $db->insert("INSERT INTO `pre_inspection_content` (`name`,`question`,`answer_type`,`warning`) VALUES ('{$_POST['name']}','$questions','$answers','$warning')");

    if ($res) {
        echo "<script>window.location.href = 'inspection_lists.php'; </script>";
    } else {
        echo 0;
    }
}
