import {
	addWest,
	addRecipe,
	changeChinese,
	changeWest,
	addGranularRecipe,
	changeGranular
} from '@/api/all.js'
import {addServicePackageRecipe} from "../../../../api/servicePackage";
module.exports = {
	data() {
		return {
			wests: [],
			chineses: [],
			granular:[],
			servicePackage:[],
		}
	},
	methods: {
		getUsage(item = {}) {
			const conetent = JSON.parse(item.content)
			let gram = conetent.reduce((g, it) => {
				g += parseInt(it['number'])
				return g
			}, 0)
			return '浓煎' + item.volume + 'ml' + '，共' + item
			.dosage + '剂' + '，每剂' + conetent.length + '味' + gram + '克'
		},
		use(data, item) {
			uni.showModal({
				title: '提示',
				content: '是否确认使用该处方，使用后将覆盖已有内容',
				success: (res) => {
					if (res.confirm) {
						uni.removeStorageSync('addWests');
						uni.removeStorageSync('addChineses');
						uni.removeStorageSync('addGranular');
						uni.removeStorageSync('addServicePackage');
						let arr = []
						let list = this.info || data
						let it = this.item || item
						let obj = {}
						if (this.type == 1) {
							const wr_ids = it.wr_ids.split(',')
							for (var i = 0; i < list.length; i++) {
								let info = list[i]
								let content = JSON.parse(info.content)
								console.log(info,'info',content)
								let obj = {
									available_days: info.available_days,
									drug_name: content.drug_name,
									f_id: info.f_id,
									frequency_id: info.frequency.id,
									grain_number: info.grain_number,
									id: content.id,
									content: content.id,
									image: content.image,
									instruction: info.instruction,
									number: info.number,
									pId: wr_ids[i],
									price: content.price,
									buy_price: content.buy_price,
									specification: content.specification,
									stock: content.stock,
									time_id: info.time_id,
									type: info.time_id,
									type_id: info.type_id,
									usage: info.instruction,
									wu_id: info.wu_id,
									drugStoreDrug:{
										price:info.total_price
									}
								}
								arr.push(obj)
							}
							uni.setStorageSync('wests', arr)
						} else {

							for (var i = 0; i < list.length; i++) {
								console.log(list[i], 'JSON.parse(list[i].content)')
								const cmIds = this.type == 2 ? list[i].cm_id.split(',') : list[i].gm_id.split(',')
								obj = {
									item: JSON.parse(list[i].content).map((item, index) => {
										return {
											...item,
											price: item.price,
											cm_id: cmIds[index],
											drugStoreDrug:{
												price: item.number * item.price
											}
										}
									}),
									obj: {
										consumption: list[i].consumption,
										deployment: list[i].deployment,
										directions: this.getUsage(list[i]),
										dosage: list[i].dosage,
										volume: list[i].volume,
										is_deepfry: list[i].is_deepfry,
										pId: list[i].id,
										remark: list[i].remark
									},
									pId: it.id
								}
								arr.push(obj)
							}
							console.log(this.type, arr, arr[0].obj, arr[0].obj.pId, '0000')
							if (this.type == 2) {
								uni.setStorageSync('mInfo', JSON.stringify(arr[0].item))
								uni.setStorageSync('mParms', JSON.stringify(arr[0].obj))
								uni.setStorageSync('cr_ids', arr[0].obj.pId)
							} else {
								uni.setStorageSync('mInfo', JSON.stringify(arr[0].item))
								uni.setStorageSync('mParms', JSON.stringify(arr[0].obj))
								uni.setStorageSync('cr_ids', arr[0].obj.pId)
							}
						}
						uni.setStorageSync('doctor_order', it.doctor_order)
						uni.setStorageSync('clinical_diagnose', it.clinical_diagnose)
						this.getDrug()
						if (this.info) {
							uni.navigateBack({ delta: 2 })
						} else {
							uni.navigateBack({ delta: 1 })
						}
					}
				}
			});
		},
		getDrug() {
			let wests = uni.getStorageSync(uni.getStorageSync('add') ? 'addWests' : 'wests') || []
			let chineses = uni.getStorageSync(uni.getStorageSync('add') ? 'addChineses' : 'chineses') || [{
				item: [],
				obj: {}
			}]
			let granular = uni.getStorageSync(uni.getStorageSync('add') ? 'addGranular' : 'granular') || [{
				item: [],
				obj: {}
			}]
			let servicePackage = uni.getStorageSync(uni.getStorageSync('add') ? 'addServicePackage' : 'servicePackage') || []

			this.$set(this, 'wests', wests)
			this.$set(this, 'chineses', chineses)
			this.$set(this, 'granular', granular)
			this.$set(this, 'servicePackage', servicePackage)

			uni.setStorageSync(uni.getStorageSync('add') ? 'addWests' : 'wests', wests)
			uni.setStorageSync(uni.getStorageSync('add') ? 'addChineses' : 'chineses', chineses)
			uni.setStorageSync(uni.getStorageSync('add') ? 'addGranular' : 'granular', granular)
			uni.setStorageSync(uni.getStorageSync('add') ? 'addServicePackage' : 'servicePackage', servicePackage)

		},
		async addDrug(data = {}, item = {}, idx = 0) {
			this.getDrug();
			if (this.tabsActive == 1) {
				this.chineses.push({
					item: [],
					obj: {}
				})
				uni.setStorageSync(uni.getStorageSync('add') ? 'addChineses' : 'chineses', this.chineses);
				this.getDrug();
				return
			}
			if (this.tabsActive == 2) {
				this.granular.push({
					item: [],
					obj: {}
				})
				uni.setStorageSync(uni.getStorageSync('add') ? 'addGranular' : 'granular', this.granular);
				this.getDrug();
				return
			}

			if (this.tabsActive === 3) {
				this.servicePackage.push({
					item: [],
					obj: {}
				})
				uni.setStorageSync(uni.getStorageSync('add') ? 'addServicePackage' : 'servicePackage', this.servicePackage);
				this.getDrug();
				return
			}


			let type = this.tabsActive >= 0 ? this.tabsActive + 1 : this.type;
			uni.showLoading({
				title: '请稍后...'
			})
			this.getDrug();
			let res = {}
			type === 1 && (res = await addWest(data))
			type === 2 && (res = await addRecipe(data))
			type === 3 && (res = await addServicePackageRecipe(data))
			type === 4 && (res = await addGranularRecipe(data))
			if (res.errcode === 0) {
				this.$toast('添加成功')
				this.$nextTick(() => {
					this.$toast('添加成功')
					if (type === 1) {
						this.wests.push({
							...item,
							...data,
							pId: res.data[0]
						})
						uni.setStorageSync(uni.getStorageSync('add') ? 'addWests' : 'wests', this.wests)
						this.$emit('add', {
							count: item.count || 1,
							nub: item.nub || 1
						})
					}
					if(type===2) {
						const obj = {
							item,
							obj: {
								...data,
								pId: res.data[0]
							}
						}
						this.$set(this.chineses, idx, obj)
						uni.setStorageSync(uni.getStorageSync('add') ? 'addChineses' : 'chineses', this
							.chineses)
						this.$emit('add')
					}
					if(type===3) {
						this.servicePackage.push({
							...item,
							...data,
							pId: res.data[0]
						})
						uni.setStorageSync(uni.getStorageSync('add') ? 'addServicePackage' : 'servicePackage', this.servicePackage)
						this.$emit('add', {
							count: item.count || 1,
							nub: item.nub || 1
						})
					}
					if(type===4) {
						const obj = {
							item,
							obj: {
								...data,
								pId: res.data[0]
							}
						}
						this.$set(this.granular, idx, obj)
						uni.setStorageSync(uni.getStorageSync('add') ? 'addGranular' : 'granular', this
							.granular)
						this.$emit('add')
					}
				})
			} else {
				this.$toast(res.msg)
			}
		},
		async setDrug(data = {}, arr) {
			let type = this.tabsActive >= 0 ? this.tabsActive + 1 : this.type;
			uni.showLoading({
				title: '请稍后...'
			})
			this.getDrug();
			let res = {}
			let index = -1;
			if (type == 1) {
				index = this.wests.findIndex((item => item['pId'] == data['pId']))
				res = await changeWest({
					...data,
					recipe_id: data['pId'],store_id: uni.getStorageSync('store_id') || 11001,
				})
			}
			if (type == 2) {
				index = this.chineses.findIndex((item => item['obj']['pId'] == data['pId']))
				res = await changeChinese({
					...data,
					recipe_id: data['pId'],store_id: uni.getStorageSync('store_id') || 11001,
				})
			}
			if (type == 3) {
				index = this.granular.findIndex((item => item['obj']['pId'] == data['pId']))
				res = await changeGranular({
					...data,
					recipe_id: data['pId'],store_id: uni.getStorageSync('store_id') || 11001,
				})
			}
			if (res.errcode == 0) {
				this.$nextTick(() => {
					if (type == 1) {
						this.$toast('修改成功')
						let obj = {
							...this.wests[index],
							...data,
							number: data['number']
						}
						this.$set(this.wests, index, obj)
						console.log(this.wests,obj);
						uni.setStorageSync(uni.getStorageSync('add') ? 'addWests' : 'wests', JSON.parse(JSON.stringify(this.wests)))
						this.$emit('set')
					} 	if (type == 2) {
						const obj = {
							item: arr,
							obj: data,
							pId: data.pId
						}
						this.$set(this.chineses, index, obj)
						uni.setStorageSync(uni.getStorageSync('add') ? 'addChineses' : 'chineses', this
							.chineses)
						this.$go(1, 4)
					}
					if (type == 3) {
						const obj = {
							item: arr,
							obj: data,
							pId: data.pId
						}
						this.$set(this.granular, index, obj)
						uni.setStorageSync(uni.getStorageSync('add') ? 'addGranular' : 'granular', this
							.granular)
						this.$go(1, 4)
					}
				})
			} else {
				this.$toast(res.msg)
			}
		},
		removeDrug(index = -1) {
			console.log(index,'delt-xxxxxxxxxxxxxxx');
			let type = this.tabsActive >= 0 ? this.tabsActive + 1 : this.type;
			if (index >= 0) {
				type == 1 && this.wests.splice(index, 1);
				type == 2 && this.chineses.splice(index, 1);
				type == 3 && this.servicePackage.splice(index, 1);
				type == 4 && this.granular.splice(index, 1);
				let key=null
				if(type==1){
					key=uni.getStorageSync('add') ? 'addWests' : 'wests'
				}
				if(type==2){
					key=uni.getStorageSync('add') ?'addChineses' : 'chineses'
				}

				if(type==3){
					key=uni.getStorageSync('add') ?'addServicePackage' : 'servicePackage'
				}

				if(type==4){
					key=uni.getStorageSync('add') ?'addGranular' : 'granular'
				}

				let data;
				if (type === 1) {
					data = this.wests;
				} else if (type === 2) {
					data = this.chineses && this.chineses.length > 0 ? this.chineses : [{ item: [], obj: {} }];
				} else if (type === 3) {
					data = this.servicePackage && this.servicePackage.length > 0 ? this.servicePackage : [];
				} else {
					data = this.granular && this.granular.length > 0 ? this.granular : [{ item: [], obj: {} }];
				}

				uni.setStorageSync(key, data);
				this.getDrug()
			} else {
				this.wests = []
				this.chineses = []
				this.granular = []
				this.servicePackage = []
				uni.removeStorageSync(uni.getStorageSync('add') ? 'addWests' : 'wests')
				uni.setStorageSync(uni.getStorageSync('add') ? 'addChineses' : 'chineses', [{
					item: [],
					obj: {}
				}])
				uni.setStorageSync(uni.getStorageSync('add') ? 'addGranular' : 'granular', [{
					item: [],
					obj: {}
				}])
				uni.setStorageSync(uni.getStorageSync('add') ? 'addServicePackage' : 'servicePackage')
			}
		}
	},
}
