/**
 * 送货单解析
 */
import extend from '../utils/extend.js';
import analyze from '../utils/analyzeScannedContent.js';

/**
 * 完工送货单
 */
export default class {
    /**
     * 订单
     */
    order = null;

    /**
     * 条码集合
     */
    barcodes = new Map();

    /**
     * 订单编码
     */
    orderNumber = '';

    constructor(order) {
        this.order = order;
        this.init();
    }

    /**
     * 初始化
     */
    init(order) {
        if (!(extend.isObject(order) || extend.isObject(this.order) || order === null)) {
            return;
        }
        this.order = order;
        this.barcodes.clear();

        if (extend.isNull(order)) {
            return;
        }

        if (extend.isArray(this.order.Barcodes)) {
            for (let i in this.order.Barcodes) {
                this.barcodes.set(this.order.Barcodes[i].Text, this.order.Barcodes[i]);
            }
        }
    }

    /**
     * 送货单数量
     */
    get quantity() {
        let res = 0;
        res = this.barcodes.size;
        return res;
    }

    /**
     * 送货扫描确认数量
     */
    get scanedQty() {
        let res = 0;
        for (let [key, value] of this.barcodes) {
            res += value.Scaned ? 1 : 0;
        }
        return res;
    }

    /**
     * 剩余扫描数量
     */
    get remainQty() {
        let res = 0;
        res = this.quantity - this.scanedQty;
        return res;
    }

    /**
     * 获取已解析二维码数据列表
     */
    getBarcodeRows() {
        let res = [];
        if (extend.isArray(this.order.Barcodes)) {
            for (let i in this.order.Barcodes) {
                let row = analyze.parse(this.order.Barcodes[i].Text);
                res.push(row);
            }
        }
        this.sortBySerialNumber(res);
        return res;
    }

    /**
     * 获取已解析二维码数据列表
     */
    getScanedBarcodeRows() {
        let res = [];
        if (extend.isArray(this.order.Barcodes)) {
            for (let i in this.order.Barcodes) {
                if (this.order.Barcodes[i].Scaned) {
                    let row = analyze.parse(this.order.Barcodes[i].Text);
                    res.push(row);
                }
            }
        }
        this.sortBySerialNumber(res);
        return res;
    }

    /**
     * 获取剩余二维码列表数据
     */
    getRemainBarcodeRows() {
        let res = [];
        if (extend.isArray(this.order.Barcodes)) {
            for (let i in this.order.Barcodes) {
                if (!this.order.Barcodes[i].Scaned) {
                    let row = analyze.parse(this.order.Barcodes[i].Text);
                    res.push(row);
                }
            }
        }
        this.sortBySerialNumber(res);
        return res;
    }

    /**
     * 根据流水码排序
     */
    sortBySerialNumber(rows) {
        if (!extend.isArray(rows)) {
            rows = [];
        }
        for (let i in rows) {
            for (let j in rows) {
                if (rows[i].SerialNumber < rows[j].SerialNumber) {
                    let temp = rows[i];
                    rows[i] = rows[j];
                    rows[j] = temp;
                    temp = null;
                }
            }
        }
        return rows;
    }
}

class CompletionOrderLineBarcode {
    Barcode = '';
    CompletionConfirmed = 0;
    CompletionConfirmedOn = null;
    CompletionOrderLineId = 0;
    WarehouseConfirmed = 0;
    WarehouseConfirmedOn = null;
}