/**
 * 
 * @param {string} s 
 * @returns {HTMLElement}
 */
function E(s) {
    return document.querySelector(s);
}

/**
 * 
 * @param {string} tag 
 * @returns {HTMLElement}
 */
function A(tag) {
    return document.createElement(tag);
}

/**
 * 
 * @param {string} s 
 * @returns {[number, number, number]}
 */
function parse_date(s) {
    return s.split("/").map(x => parseInt(x));
}

/**
 * 
 * @param {[number, number, number]} d 
 * @returns {string}
 */
function fmt_date(d) {
    return "" + d[0] + "/" + d[1] + "/" + d[2];
}

/**
 * 
 * @param {[number, number, number]} d 
 * @param {number} n 
 * @returns {[number, number, number]}
 */
function add_day(d, n) {
    d = d.concat();
    d[2] += n;
    d[1] += d[2] / 30 | 0;
    d[2] = d[2] % 30;
    d[0] += d[1] / 12 | 0;
    d[1] = d[1] % 12;
    if (d[2] < 1) {
        d[2] += 30;
        d[1] -= 1;
    }
    if (d[1] < 1) {
        d[1] += 12;
        d[0] -= 1;
    }
    return d;
}

const db = {
    rprice: {
        R1: 12,
        R2: 12,
        R3: 12,
        R4: 12,
    },
    rdelay: {
        R1: 30,
        R2: 30,
        R3: 60,
        R4: 60,
    },
    pprice: {
        P1: 50,
        P2: 70,
        P3: 90,
        P4: 100,
        P5: 100,
    },
    pdelay: {
        P1: 56,
        P2: 56,
        P3: 56,
        P4: 56,
        P5: 56,
    },
    pr: {
        P1: { R1: 1 },
        P2: { R2: 1, R3: 1 },
        P3: { R1: 1, R3: 1, R4: 1 },
        P4: { R2: 1, R3: 1, R4: 2 },
        P5: { P2: 1, R4: 1 },
    },
};

/**
 * 
 * @param {string} p 
 * @param {[number, number, number]} d 
 * @return {{ [r: string]: [number, [number, number, number]] }}
 */
function buy_date_of(p, d) {
    const r = db.pr[p];
    const s = {};
    for (const key in r) {
        if (key[0] === "R") {
            s[key] = [r[key], add_day(d, -db.rdelay[key])];
        } else if (key[0] === "P") {
            s[key] = [r[key], add_day(d, -db.pdelay[key])];
            Object.assign(s, buy_date_of(key, add_day(d, -db.pdelay[key])));
        }
    }
    return s;
}

const order = ["P2", "P4", "P1", "P5", "P3"];

document.addEventListener("DOMContentLoaded", function() {
    const datestart_input = E("#datestart");
    const efficiency_input = E("#efficiency");
    const product_select = E("#product");
    const limit_input = E("#limit");
    const amount_input = E("#amount");
    const ok_button = E("#ok");
    const tables = E("#tables");
    function generate() {
        tables.innerHTML = "";
        const datestart = parse_date(datestart_input.value);
        const limit = parseInt(limit_input.value);
        const efficiency = parseInt(efficiency_input.value);
        const amount = parseInt(amount_input.value);
        const p = product_select.value;
        if (order.indexOf(p) === -1) return;
        // for (const p of order) {
        const table = A("table");
        table.classList.add("result");
        tables.appendChild(table);
        const thead = A("thead");
        table.appendChild(thead);
        let tr = A("tr");
        thead.append(tr);
        const plabel = A("td");
        plabel.textContent = p + " 规划";
        E("title").textContent = p + ": " + fmt_date(datestart);
        tr.appendChild(plabel);
        tr = A("tr");
        thead.appendChild(tr);
        const d0 = buy_date_of(p, datestart);
        plabel.colSpan = Object.keys(d0).length + 2;
        for (const r in d0) {
            const td = A("td");
            td.textContent = "购买 " + r + " * " + (d0[r][0] * amount);
            tr.appendChild(td);
        }
        let td = A("td");
        td.textContent = p + " 开产日";
        tr.appendChild(td);
        td = A("td");
        td.textContent = p + " 产出 * " + amount;
        tr.appendChild(td);
        const tbody = A("tbody");
        table.appendChild(tbody);
        let next_date = datestart;
        for (let i = 0; i < limit; ++i) {
            const tr = A("tr");
            tr.addEventListener("click", () => tr.classList.toggle("highlight"));
            tbody.appendChild(tr);
            const d = buy_date_of(p, next_date);
            for (const r in d) {
                const td = A("td");
                td.textContent = fmt_date(d[r][1]);
                tr.appendChild(td);
            }
            let td = A("td");
            td.textContent = fmt_date(next_date);
            tr.appendChild(td);
            td = A("td");
            td.textContent = fmt_date(add_day(next_date, efficiency /*db.pdelay[p]*/));
            tr.appendChild(td);
            next_date = add_day(next_date, efficiency /*db.pdelay[p]*/);
        }
        // }
    }
    ok_button.addEventListener("click", () => generate());
    product_select.addEventListener("change", () => generate());
    generate();
});