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

$db = new db();


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

    echo "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('jobcard_q_a_list', ['list_name', 'questions', 'answers', 'warnings', 'jobcard_type_id'], [$_POST['name'], $questions, $answers, $warning, $_POST['jobcard_type_id']], "record_id = {$_POST['jobcard_list_id']}");
    if ($res) {
        echo "<script>window.location.href = 'jobcard_lists.php'; </script>";
        // echo 1;
    } else {
        echo 0;
    }
} else {
    echo $_POST['jobcard_type_id'];
    $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++;
    }

    // echo "INSERT INTO `jobcard_q_a_list` (`list_name`,`questions`,`answers`,`warnings`,`jobcard_type_id`) VALUES ('{$_POST['name']}','$questions','$answers','$warning',{$_POST['jobcard_type_id']})";
    $res = $db->insert("INSERT INTO `jobcard_q_a_list` (`list_name`,`questions`,`answers`,`warnings`,`jobcard_type_id`) VALUES ('{$_POST['name']}','$questions','$answers','$warning',{$_POST['jobcard_type_id']})");

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