<template>
	<view>
		<view class="" v-if="!loading && approvalInfo.PERMISSIONAPPLY_ID">
			<view v-if="pageData.type=='4'">
				<ShopApproval :businessId="pageData.id" :data="approvalInfo" ref="shop" @refresh="reload"
					:approvalData="approvalData" />
			</view>
			<view v-if="pageData.type=='7'">
				<ShopChangeState :businessId="pageData.id" :data="approvalInfo" ref="shop" @refresh="reload"
					:approvalData="approvalData" />
			</view>
			<view v-if="pageData.type=='8'">
				<MerchartInfoView :businessId="pageData.id" :data="approvalInfo" ref="shop" @refresh="reload"
					:approvalData="approvalData" />
			</view>
			<view v-if="pageData.type=='9'">
				<CancelView :businessId="pageData.id" :data="approvalInfo" ref="shop" @refresh="reload"
										:approvalData="approvalData" :effectiveDetail="effectiveDetail"/>
			</view>
		</view>
		<noFound v-show="!loading && !approvalInfo.PERMISSIONAPPLY_ID" :nodata="true" text="抱歉,没有找到该数据" />
	</view>
</template>

<script>
	import ShopApproval from './components/shopApproval.vue'
	import ShopChangeState from './components/shopChangeState.vue'
	import MerchartInfoView from './components/merchartInfoView.vue'
	import CancelView from "./components/cancelView.vue";
	import Store from "../../store/store";
	export default {
		data() {
			return {
				pageData: {
					type: null,
					id: null
				},
				loading: true,
				approvalInfo: {},
				ENDACCOUNT_ID:'',
				imagesList:[],
				Audit_List:[],
				effectiveDetail:{},
				endaccountMsg:[],
				isFACTAMOUNT_SALE:'',
				isTICKETCODE_CIGARETTE:'',
				isFACT_AMOUNT:'',
				isSELLCOUNT_PROMOTION:'',
				approvalData: {} // 审批后授权的列表
			}
		},
		components: {
			ShopApproval,
			ShopChangeState,
			MerchartInfoView,
			CancelView
		},
		methods: {
			// 验证促销数据
			checkPromotion(_data){
				if (_data.SELLCOUNT_PROMOTION != 0 && _data.SELLCOUNT_PROMOTION != '') {
					return true
				} else {
					return false
				}
			},
			// 获取单品数据、移动支付等校验数据
			getEndAccountDate(datatype) { // 获取更新过的数据校验数据
				let _this = this
				let _detail = this.effectiveDetail
				_this.$request.$get('GetEndAccountData', {
					serverpart_Id: _detail.SERVERPART_ID,
					serverpart_Code: _detail.SERVERPARTCODE,
					machine_Code: _detail.MACHINECODE,
					shop_Code: _detail.SHOPCODE,
					province_Code: _detail.PROVINCE_CODE,
					endAccount_StartDate: _detail.ENDACCOUNT_STARTDATE,
					endAccount_Date: _detail.ENDACCOUNT_DATE,
					fact_Amount: _detail.FACT_AMOUNT,
					cash_Pay: _detail.CASHPAY,
					sale_Correct: _detail.SALE_CORRECT,
					mobile_Payment: _detail.MOBILEPAYMENT,
					mobile_Correct: _detail.MOBILE_CORRECT,
					endAccount_Id: _detail.ENDACCOUNT_ID,
					data_Type: datatype
				}).then(res => {
					let _data = res.Result_Data
					if (datatype == 1) {
						_detail.FACTAMOUNT_SALE = _data.FactAmount_Sale = Number(_data.FactAmount_Sale) //单品金额
						_detail.SELLCOUNT_SALE = _data.SellCount_Sale //单品金额
						_this.isFACTAMOUNT_SALE = _this.checkSinglePro(_detail)
					}
					if (datatype == 2) {
						_detail.TICKETCODE_CIGARETTE = _data.TicketCode_Cigarette //香烟客单数量
						_detail.SELLCOUNT_CIGARETTE = _data.SellCount_Cigarette
						_detail.FACTAMOUNT_CIGARETTE = _data.FactAmount_Cigarette
						_this.isTICKETCODE_CIGARETTE = _this.checkCigarette(_detail)
					}
					if (datatype == 3) {
						_detail.FACT_AMOUNT = Number(_data.Fact_Amount) //移动支付销售金额
						_detail.TICKETCOUNT = _data.Ticket_Count //移动支付销售金额
						_this.isFACT_AMOUNT = _this.checkMobileCorrect(_detail)
					}
					if (datatype == 4) {
						_detail.SELLCOUNT_PROMOTION = _data.SellSount_Promotion //促销数量
						_detail.FACTAMOUNT_PROMOTION = _data.FactAmount_Promotion //促销数量
						_this.isSELLCOUNT_PROMOTION = _this.checkPromotion(_detail)
					}
				})
			},
			// 验证香烟数据
			checkCigarette(_data) {
				if (_data.TICKETCODE_CIGARETTE && _data.TICKETCODE_CIGARETTE != NaN && Number(_data.TICKETCODE_CIGARETTE) !== 0) {
					return true
				} else {
					return false
				}
			},
			calcWuchalv(){ // 误差率
				let _data = this.effectiveDetail
				if ( _data.CASH == 0 ) {
					if(_data.DIFFERENT_PRICE!=0) {
						return "100.00";

					}else {
						return "0.00";
					}
				}
				else {

					return (Math.abs(_data.DIFFERENT_PRICE)  / _data.CASH * 100).toFixed(2);

				}
			},
			// 验证移动支付
			checkMobileCorrect(_data){

				if ((_data.FACT_AMOUNT + _data.SALE_CORRECT) == _data.MOBILEPAYMENT) { // 公式： fa+sc=mp+mc; sc=mc-mpb
					_data.MOBILE_CORRECT = 0
					this.mobliePayBalance = - _data.SALE_CORRECT
					return 1
				} else {
					this.mobliePayBalance = _data.FACT_AMOUNT - _data.MOBILEPAYMENT // 移动冲正支付差额 实收金额-移动支付金额

					if(_data.MOBILE_CORRECT ==''){ // 默认到账差额为零，使等式成立
						this.effectiveDetail.MOBILE_CORRECT = this.mobliePayBalance // 移动冲正
						this.effectiveDetail.SALE_CORRECT = 0  // 到账实差
					}
					return 0 // 移动冲正 = 移动支付差额+到账实差

				}
			},
			//  单品数据判断
			checkSinglePro(_data) { // 单品计算公式：FACTAMOUNT_SALE = TOTALSELLAMOUNT 或者 TOTALSELLAMOUNT = FACTAMOUNT_SALE + FACTAMOUNT 或 (_data.TOTALSELLAMOUNT+FACTAMOUNT) == _this.FACTAMOUNT_SALE

				if (_data.FACTAMOUNT_SALE == _data.TOTALSELLAMOUNT || (_data.TOTALSELLAMOUNT + _data.FACTAMOUNT) == _data.FACTAMOUNT_SALE || _data.FACTAMOUNT_SALE == _data.CASHPAY) {
					return 1
				} else {
					return 0
				}
			},
			// 数据校验门店列表
			getEffectiveDetail() {
				let _this = this
        console.log('_this.ENDACCOUNT_ID',_this.ENDACCOUNT_ID)
				this.$request.$get('GetEndaccountDetails', {
					Endaccount_ID: _this.ENDACCOUNT_ID
				}).then(res => {
					_this.loading = false
					uni.hideLoading()
					console.log('res111',res)
					if (!res.Result_Code || res.Result_Code != '100') return

					_this.imagesList = res.Result_Data.Image_List ? res.Result_Data.Image_List.List : []
					if (res.Result_Data.Audit_List) { // 若稽核数据存在，则赋值
						_this.Audit_List = res.Result_Data.Audit_List.List
					}
					let _data = res.Result_Data.Endaccount_List


					_this.effectiveDetail = _data

          if (_data){
            _this.endaccountMsg = _data.ENDACCOUNT_DESC.split("|");
          }

					// 如果当前数据为日结校验状态，并且单品数据为空 则异步请求远程单品数据，移动支付数据，香烟数据、促销数据;否则，验证这些数据是否正确


					if (_data.SCAN_TYPE != 3) { // 若数据传输类型为接口传输 则不需要远程请求单品数据,默认单品销售金额=收银端销售金额， 移动支付实收金额 = 收银端移动支付
						if (_data.TREATMENT_MARKSTATE==0 && _data.FACTAMOUNT_SALE == "") {
							_data.FACTAMOUNT = _data.FACTAMOUNT == '' ? 0 : _data.FACTAMOUNT
							_data.SALE_CORRECT = _data.SALE_CORRECT == '' ? 0 : _data.SALE_CORRECT
							_this.getEndAccountDate(1) // 单品数据
							_this.getEndAccountDate(2) // 香烟数据
							_this.getEndAccountDate(3) // 移动支付
							_this.getEndAccountDate(4) //促销数据
						}else {
							_this.isFACTAMOUNT_SALE = _this.checkSinglePro(_data) // 单品数据 是否一致
							_this.isTICKETCODE_CIGARETTE = _this.checkCigarette(_data) // 香烟数据
							_this.isFACT_AMOUNT = _this.checkMobileCorrect(_data) // 移动支付
							_this.isSELLCOUNT_PROMOTION = _this.checkPromotion(_data) // 促销数据
						}

					} else {
						_data.FACTAMOUNT_SALE = _data.TOTALSELLAMOUNT
						_data.FACT_AMOUNT = _data.MOBILEPAYMENT
						_this.isFACTAMOUNT_SALE = _this.checkSinglePro(_data) // 单品数据
						_this.isFACT_AMOUNT = _this.checkMobileCorrect(_data) // 移动支付
						_this.isTICKETCODE_CIGARETTE = _this.checkCigarette(_data) // 香烟数据
					}

					if (_data.PENDSTATE == 0) {
						console.log('store',Store.state.userData.AuthorityInfo['51b6817d-62a9-4b3a-8e6a-abd7e077271a'])
						// if (Store.state.userData.AuthorityInfo['51b6817d-62a9-4b3a-8e6a-abd7e077271a']===1){
						if (_data.TREATMENT_MARKSTATE == 0) { // 待处理
							_this.isCheck = 1
							_this.isdisabled = false
						}
						if (_data.TREATMENT_MARKSTATE == 3) { // 主任复核
							_this.isCheck = 2
							// _this.Toexamine = true;
							_this.examineType = "SubmitEndaccount"
						} else if (_data.TREATMENT_MARKSTATE == 1) { // 财务审核
							_this.isCheck = 2
							// _this.Toexamine = true;
							_this.examineType = "ApproveEndaccount"
						}
						// }
					} else { // 已审结
						_this.examineType = "";
						_this.isCheck = 0;
					}

					_this.effectiveShow = true
					_this.$forceUpdate()
				})
			},

			getDetail(opt) {
				let _this = this
				this.$request.$webGet('/EShangApiMain/Platform/GetPermissionApplyDetail?permissionApplyid=' + opt.id).then(
					res => {
						uni.hideLoading()
						_this.loading = false

						if (res.Result_Code !== 100) {
							return
						}
						_this.approvalInfo = res.Result_Data
						_this.ENDACCOUNT_ID = _this.approvalInfo.APPLYSHOP_NAME
						console.log('approvalInfo',res.Result_Data)
            if (_this.pageData.type==='9'){
              _this.getEffectiveDetail()
            }
						if ( ['4','8'].indexOf(_this.pageData.type)>-1) {
							_this.$nextTick(()=>{

							_this.$refs['shop'].getShops(res.Result_Data)
							})
						}
						_this.getDetailList({
							id: opt.id,
							type: res.Result_Data.PERMISSIONAPPLY_TYPE
						})
					})
			},
			getDetailList(obj) {
				let _this = this
				this.$request.$webGet('/EShangApiMain/BusinessProcess/GetPermissionApplyDetailList?permissionApplyid=' +
					obj.id + '&PermissionApplyType=' + obj.type).then(res => {
					uni.hideLoading()
					if (res.Result_Code !== 100) {
						return
					}
					_this.approvalData = res.Result_Data

				})
			},
			reload() {
				this.getDetail(this.pageData)
				this.getDetailList(this.pageData)
			}
		},
		onLoad(option) {

			console.log('option',option)
			uni.showLoading()
			this.pageData = option
      if (this.pageData.type=='9'){
        uni.setNavigationBarTitle({
          title: ' '
        })
      }
			this.loading = true
			this.getDetail(option)

			this.$forceUpdate()
		}
	}
</script>

<style>

</style>
