import toast from '../../../utils/tool/toast';
import order from "../../../utils/controls/order";

import notice from '@/utils/notice/index';

export default {


	methods:{
		/* 触发更新规则 */
		changeStatus:function(item){



			if (this.updateStatus[item.name]) {


				return this.updateStatus[item.name].map((target)=>{

					if (typeof target === 'string') target = {name:target};

					if (this.target[target.name]) {

						let id = this.target[target.name].flatIdGETIndex(item.id);

						if (target.reload === undefined?item.reload:target.reload) {
							return this.target[target.name].flatReload(1,true);
						}  else if (target.delete === undefined?item.delete:target.delete) {
							return this.target[target.name].flatSplice(id);
						} else {

							if (id !== undefined) {
								let useItem = this.target[target.name].flatGetData(id);
								if (useItem) {
									/* 更新状态值 */
									useItem.statusOrder = order.getOrderStatus(item.toStatus);
									/* 更新状态值 */
									return this.target[target.name].flatSaveData(id,useItem);
								}
							}

						}

					}

				});
			}

		},
	},

	// /* 通知系统 */
	// onMonitor:function (item) {
	//
	// 	if (item.notice) {
	// 		toast.success(item.notice);
	// 	}
	//
	// 	if (item.update) {
	// 		return this.changeStatus(item.update,true);
	// 	}
	//
	// },

	onShow(){
		if (this.storageMsg) {
			toast.success(this.storageMsg);
			this.storageMsg = null;
		}
	},



	created(){
		/* 更新规则 */
		this.updateStatus = {

			/* 如果为取消 将更新此两个模式 */
			cancel:['all',{
				name:'pay',
				delete:true
			}],

			/* 如果为支付 将触发此更新规则 */
			pay:['all',
				{
					name:'pay',
					delete:true
				},
				'received'
			],

			/* 如果为确认收货触发 */
			confirm:[
				'all',
				{
					name:'goods',
					delete:true
				},
				'comment'
			],

			/* 如果为售后进入触发 */
			aftermarket:['all',{
				name:'received',
				delete:true
			}],

			/* 如果为评价触发 */
			comment:[
				'all',
				{
					name:'comment',
					delete:true
				}
			],

			/* 如果为删除 */
			delete:['all'],

			paySuccess:[{
				name:'all',
				reload:true
			},{
				name:'received',
				reload:true
			}],

			payFail:[{
				name:'all',
				reload:true
			},{
				name:'pay',
				reload:true
			}],

		}

		notice.on('my-order',(item)=>{
			if(item.page === 'order' && item.msg) {
				this.storageMsg = item.msg;
			}
			return this.changeStatus(item);
		},'order');
	},

	beforeDestroy(){
		return notice.off('my-order','order');
	}


}
