<window id="inventory">
    <layout type="vertical">
        <layout type="horizontal">
            <label>Transaction type: </label>
            <sqldrop id="transactiontype" />
            <stretch />
        </layout>
        <layout type="horizontal">
            <sqldrop data="0" display="1" showdata="true" id="item">
                <query><![CDATA[SELECT id, name FROM coffees WHERE quantity <> 0 ORDER BY name ASC]]></query>
            </sqldrop>
            <line id="quantity" />
            <sqldrop id="units" />
            <layout type="stack" id="optional">
                <page />
                <page>    
                    <layout type="horizontal">
                        <label id="reasonlabel">Reason: </label>
                        <line id="reason" />
                    </layout>
                </page>
            </layout>
        </layout>
        <layout type="horizontal">
            <button type="push" name="Update" id="update" />
            <stretch />
        </layout>
        <textarea id="current" />
    </layout>
    <program>
        <![CDATA[
            var types = findChildObject(this, 'transactiontype');
            types.addItem("inventory");
            types.addItem("loss");
            var optionalDisplay = findChildObject(this, 'optional');
            var units = findChildObject(this, 'units');
            units.addItem("bag");
            units.addItem("lb");
            units.addItem("kg");
            var items = findChildObject(this, 'item');
            var status = findChildObject(this, 'current');
            function updateStatus() {
                query = new QSqlQuery();
                query.prepare("SELECT quantity, (quantity / 2.2)::numeric(12,3), (quantity / (SELECT conversion FROM lb_bag_conversion WHERE item = :id1))::numeric(12,2) FROM items WHERE id = :id2");
                query.bind(":id1", items.currentData());
                query.bind(":id2", items.currentData());
                query.exec();
                query.next();
                var text = items.currentText;
                text += " Current inventory: ";
                text += query.value(0);
                text += " Lb (";
                text += query.value(1);
                text += " Kg), ";
                text += query.value(2);
                text += (query.value(2) == "1" ? " bag" : " bags");
                query = query.invalidate();
                status.plainText = text;
            }
            var button = findChildObject(this, 'update');
            var value = findChildObject(this, 'quantity');
            var reason = findChildObject(this, 'reason');
            var reasonlabel = findChildObject(this, 'reasonlabel');
            reason.enabled = false;
            button.clicked.connect(function() {
                q = "INSERT INTO ";
                q += (types.currentIndex == 0 ?
                    "inventory (time, item, quantity)" :
                    "loss (time, item, quantity, reason)");
                q += " VALUES ('now', ";
                q = q + items.currentData();
                q = q + ", ";
                if(units.currentText == "lb") {
                    q = q + value.text;
                } else if (units.currentText == "kg") {
                    q = q + (value.text * 2.2);
                }
                else {
                    q = q + value.text;
                    q = q + " * (SELECT conversion FROM lb_bag_conversion WHERE item = ";
                    q = q + items.currentData();
                    q = q + ")";
                }
                q += (types.currentIndex == 0 ?
                    ")" :
                    ", '" + reason.text + "')");
                query = new QSqlQuery();
                query.exec(q);
                updateStatus();
            });
            items['currentIndexChanged(int)'].connect(updateStatus);
            types['currentIndexChanged(int)'].connect(optionalDisplay.setCurrentIndex);
            updateStatus();
        ]]>
    </program>
</window>
