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 {
            inputData:[],
            useSelectItem:{},
            select:false,
            images:[],
            textarea:'',
            item:{
                price:'0.00',
                name:''
            }
        }
    },

    computed:{
      introduce(){
        let item = this.inputData[1] || {};

        let data = item.data || [];

        return data[item.value] || {};
      }
    },

    methods:{

        openSelectModal(item){
            this.useSelectItem = item;
            if (!this.select) {
                this.select = true;
            }
        },

        changeSelect(item){
            this.useSelectItem.value = item.index;
        },

        submit(){

            if (this.submitStatus) return ;

            let inputData = [
                ...this.inputData,
                {
                    value: this.textarea,
                    key:'content',
                    rules:'请说明原因'
                },
                {
                    value: this.images,
                    key:'images',
                    rules: '请上传相关图片'
                }
            ];

            return test.verificationPromise(inputData,true).then((data)=>{

                this.openSubmitLoading('申请中');

                this.uploadImageAll({
                    images:data.images
                }).then((resultData)=>{
                    data = {
                        ...data,
                        ...resultData
                    }

                    data[this.introduce.key] = data.content;

                    delete data.content;

                    this.submitTarget(data);
                }).catch(()=>{
                    this.closeSubmitLoading('图片上传失败');
                })

            });

        },

        submitTarget(data){

            this.$request({
                url:'order/addRefund',
                data:{
                    ...data,
                    order_id: this.$params.id
                },
                token:true,
                message:2,
                failMessage:true
            }).then((data)=>{
                this.closeSubmitLoading();
                if (data.isSuccess) {

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

                    notice.trigger('order',order.createdUpdateObj('aftermarket',this.$memoryParams.item,this.$params.index,data.msg,this.$params.page));

                    return this.$router.navigateBack();
                }
            }).catch(()=>{
                this.closeSubmitLoading();
            });
        }
    },

    onLoad(){
        this.item = {
            price: this.$params.price,
            name: this.$memoryParams.name
        }
        this.inputData = inputData(parseInt(this.$params.status) || 0);
    }

}