import { findWithRelatedTemp, addWithRelatedTemp } from '../api'

export let historyModule = {

    methods: {
        //暂存
        history() {
            if (!this.mainlist) {
                this.$message.success(this.$lang('请先提取单据'));
                return;
            }
            let params = this.getDetailData(false, false)
            
            if (params.rcvLinesDTOList.length == 0) {
                this.$message.success('明细数量为0');
                return;
            }
            addWithRelatedTemp(params).then((res) => {
                if (!res.success) {
                    this.$exception(res)
                    return
                }
                this.$message.success(this.$lang('暂存成功'))
                this.items.forEach((tt) => {
                    tt.value = ''
                })
                this.handleReset()
            })
        },
        //提取暂存订单
        getHistory(a, data) {
            if (!data) return;
            findWithRelatedTemp({ id: data.mainlist[0].tempSaveId }).then((res) => {
                if (!res.success) {
                    this.$exception(res)
                    return
                }
                let data = JSON.parse(res.data)
                let params = {
                    mainlist: [{ ...data.rcvHeadersDTO, ...data.rcvHeadersProfilesDTO }],
                    sublist: data.rcvLinesDTOList.map(t => ({
                        ...t,
                        rcvLinesPicDTOList: Array.isArray(t.rcvLinesPicDTOList) ? t.rcvLinesPicDTOList.map(tt => tt.picFileurl).join(',') : null,
                    }))
                }
                this.setinfo(null, params);
                this.$message.success(this.$lang('提取暂存成功'))
            })
        },
    }

};