import inputData from "../data/input";
import test from '@/utils/test';
import order from "../../../utils/controls/order";
import notice from '@/utils/notice/index';
export default {

    data(){
        return {
            inputShopData:[]
        }
    },

    methods:{
        submit(){

            if (this.submitStatus) return;

            this.verificationData().then((data)=>{

                let imageAllPromise = data.map((item,index)=>{
                    item.product_id = this.inputShopData[index].id;
                    return this.uploadImageAll({
                        images:item.images
                    });
                });

                this.openSubmitLoading('评价中');

                Promise.all(imageAllPromise).then((imagesData)=>{
                    imagesData.map((item,index)=>{
                       data[index] = {
                           ...data[index],
                           ...item
                       };
                    });

                    return this.submitComment(data);
                }).catch(()=>{
                    this.closeSubmitLoading('图片上传错误');
                });


            });

        },

        submitComment(data){

            let resultData = data.map((item)=> this.$request({
                url:'product/addCommond',
                data:{
                    ...item,
                    order_id: this.$params.id
                },
                token:true,
                failMessage:true
            }));

            Promise.all(resultData).then((data)=>{
                this.closeSubmitLoading();

                notice.trigger('user',{
                    update:true
                });

                notice.trigger('order',order.createdUpdateObj('comment',this.$memoryParams.item,this.$params.index,'评价成功',this.$params.page));

                return this.$router.navigateBack();

            }).catch(()=>this.closeSubmitLoading());

        },

        verificationData(){
            return Promise.all(this.inputShopData.map((item)=> test.verificationPromise(item.inputData,true)));
        },

        install(data){

            this.inputShopData = data.map((item)=>{
                item.inputData = inputData();
                return item;
            });

        }
    },

    onLoad(){
        this.install(this.$memoryParams.data || []);
    }

}