/**
 * workbook 保存操作
 */

import { $CONST } from "@birt/funclib/ConstUtil";

export default {
    data() {
        return {
        };
    },
    computed: {
        // 请求类
        axios() {
            debugger
            return this.conf.axios || (this.$HI && this.$HI[$CONST.HIREQUEST]);
        },
        // 流程或功能模型路径
        modelPath() {
            return this.conf.modelPath;
        },
        // 保存路径
        saveUrl() {
            debugger
            let saveUrl = this.conf.saveUrl || (this.$HI && this.$HI[$CONST.HIURLSAVE]);
            if (this.modelPath && !saveUrl.endsWith(this.modelPath)) {
                saveUrl += this.modelPath;
            }
            return saveUrl
        },
        // 保存自定义方法
        saveMethod() {
            if (this.conf.saveMethod) {
                return this.conf.saveMethod;
            } else if (this.axios && this.saveUrl) {
                return this._defaultSaveMethod;
            }
        },
        // 删除自定义方法
        deleteMethod() {
            if (this.conf.deleteMethod) {
                return this.conf.deleteMethod;
            } else if (this.axios && this.saveUrl) {
                return this._defaultDeleteMethod;
            }
        },
        // 保存成功自定义回调
        saveSucced() {
            return this.conf.saveSucced;
        }
    },
    methods: {
        // 默认数据保存方法
        _defaultSaveMethod(stores, option, saveSucc) {
            let saveData = this._getSaveData(stores);
            this.axios.post(this.saveUrl, { __body: JSON.stringify(saveData) }).then((res) => {
                // 保存成功回调
                saveSucc && saveSucc.call(stores, saveData, res);
            });
        },
        // 默认数据删除方法
        _defaultDeleteMethod(stores, option, deleteSucc) {
            let deleteData = this._getDeleteData(stores);
            this.axios.post(this.saveUrl, { __body: JSON.stringify(deleteData) }).then((res) => {
                // 保存成功回调
                deleteSucc && deleteSucc.call(stores, deleteData, res);
            });
        },
        // 保存成功后续处理
        _afterSaveSucced(stores, saveSucced, saveData, response, message) {
            if (saveSucced) {
                saveSucced.call(this, saveData, response);
            }
            if (this.saveSucced) {
                this.saveSucced.call(this, saveData, response);
            }
            stores.forEach((ds) => {
                ds.saveSuccessed(response);
            });
            this.$message({
                message: message || "保存成功",
                type: "success",
            });
        },
        // 调用数据集保存验证
        _saveValidate() {
            for (let dsId in this.dataset) {
                let ds = this.dataset[dsId];
                let validateInfo = ds.validate();
                if (!validateInfo.canSave) {
                    if (validateInfo.message) {
                        this.$message({
                            message: validateInfo.message,
                            type: "warning",
                        });
                    }
                    return false;
                }
            }
            return true;
        },
        // 获取保存数据包
        _getSaveData(stores) {
            let saveData = [];
            stores.forEach((ds) => {
                let mpath = ds.getModelFilePath();
                let data = ds.getDirtyData();
                if (mpath && data.length > 0) {
                    saveData.push({ modelFilePath: mpath, data: data });
                }
            });
            return saveData;
        },
        // 获取删除数据包
        _getDeleteData(stores) {
            let deleteData = [];
            stores.forEach((ds) => {
                let mpath = ds.getModelFilePath();
                let data = ds.getDeleteData();
                if (mpath && data.length > 0) {
                    deleteData.push({ modelFilePath: mpath, data: data });
                }
            });
            return deleteData;
        },
        // 调用所有数据集保存
        save(option, saveSucced) {
            if (this.saveMethod) {
                if (!this._saveValidate()) return;
                let stores = this.getStores();
                this.saveMethod.call(this, stores, option, (saveData, response) => {
                    this._afterSaveSucced(stores, saveSucced, saveData, response);
                });
            } else {
                console.error("workbook.saveMethod 保存方法未定义.")
            }
        },
        // 取消数据修改
        cancel() {
            this.getStores().forEach((ds) => {
                // 调用 取消会出现同步锁
                // ds.cancel();
                ds.remove(ds.getDirtyData());
            });
        },
        // 彻底删除数据
        delete(option, deleteSucced) {
            if (this.deleteMethod) {
                let stores = this.getStores();
                this.deleteMethod.call(this, stores, option, (deleteData, response) => {
                    this._afterSaveSucced(stores, deleteSucced, deleteData, response, "删除成功");
                });
            } else {
                console.error("workbook.deleteMethod 删除方法未定义.")
            }
        }
    }
}