import dateParse from '@/utils/tool/date';
import modal from "../../../utils/tool/modal";
export default {

    data(){
        return {
            data:[],
            is_refound:0,
            async:true
        }
    },


    methods:{

        fetch(obj){
            return this.$request({
                url:'order/getPlanList',
                data:{
                  order_id: this.$params.order_id
                },
                token:true
            }).then((data)=>{
                data.data = data.data || {};

                this.is_refound = data.data.is_refound;

                this.data = (data.data.data || []).map((item)=>{
                    return {
                        date:dateParse(item.deliver_time,'YYYY-MM-DD'),
                        status: item.status,
                        id: item.id
                    }
                });

                return obj.success();
            }).catch(obj.fail);
        },

        installTime(data){

            if (data) {

            } else {
                data = [];
                let intCount = parseInt(this.$params.count || 6);
                let nowDate = dateParse.createDate(parseInt(this.$params.start));
                let dateAfter = parseInt(this.$params.date) || 5;
                let afterFormat = dateAfter < 10 ? '0'+dateAfter:dateAfter;
                for (let i=0;i<intCount;i++) {

                    data.push({
                        date: dateParse(nowDate,'YYYY-MM-'+afterFormat),
                        status:0,
                        id:0
                    });

                    nowDate = dateParse.nextMonth(nowDate,afterFormat);

                }
            }

            this.data = data;
        },

        confirm(item){

            if (this.confirmStatus) return;

            modal.confirm({
                title:'收货确认',
                content:'是否确认收货',
                confirm:()=>{

                    this.$request({
                        url:'order/saveOrderPlan',
                        loading:'确认中',
                        token:true,
                        data:{
                            plan_id: item.id
                        },
                        message:true,
                        failMessage:true,
                        next:({status})=> this.confirmStatus = status,
                    }).then((data)=>{
                        if (data.isSuccess) {
                            item.status = 1;
                        }
                    });
                }
            })
        }

    },

    onLoad(){

        if (this.$params.order_id === undefined) {
            this.async = false;
            this.installTime(this.$memoryParams.data);
        }
    }

}