<template>
  <JeepayBackground :bgColorStyle="{}">
    <JeepayCustomNavbar textColor="#fff" bgDefaultColor="linear-gradient(270deg, rgba(72, 192, 255, 1) 0%, rgba(51, 157, 255, 1) 100%)" title="辅助终端详情" backCtrl="back" />

	<JeepayTableListItem viewClass="list-item-by-detail"
	logo="/pageDevice/static/detailsLislImg/term-white.svg"
	:title="vdata.record.trmName" :subtitle="vdata.record.trmNo" :moreBtnList="list" />

	<JeepayCard editText="编辑信息" @editTap="go.to('PAGES_APP_TERMINAL_CREATETERM', {trmId: vdata.record.trmId})">
	 
	 <JeepayDescview>
		 <JeepayDescviewItem title="设备名称" :desc="vdata.record.trmName" />
		 <JeepayDescviewItem title="绑定应用" :desc="vdata.record.appId" />
		 <JeepayDescviewItem title="绑定门店" :desc="vdata.record.storeName" />
		 <JeepayDescviewItem title="布放位置" :desc="vdata.record.detailAddress" />
	 </JeepayDescview>
	 
	</JeepayCard>
	
	<JeepayCard title="其他设置" viewStyle="margin-top: 30rpx;">
		
		<JeepayTableListItem title="终端状态" subtitle="状态禁用后，当前终端将无法使用">
			<template #titleRight>
				 <JeepayStateSwitch v-model:state="vdata.record.state" :showSwitchType="true" :updateStateFunc="updateState" />
			</template>
		</JeepayTableListItem>
		
		<JeepayTableListItem title="门店默认设备" subtitle="设为门店默认辅助终端设备后, 该门店将默认使用该设备下单">
			<template #titleRight>
				<JeepayStateSwitch v-model:state="vdata.record.defaultFlag" :showSwitchType="vdata.record.defaultFlag == 0" :updateStateFunc="updateDefault" />
			</template>
		</JeepayTableListItem>
		
	</JeepayCard>
	
	
	<JeepayCard v-if="ak.ent.has('ENT_TERMINAL_SENDUP')" title="报备管理" viewStyle="margin-top: 30rpx;">
		
		<template v-for="(item) in vdata.channelList">
			<JeepayTableListItem :logo="item.icon" :logoStyle="{'background': item.bgColor, 'border-radius': '50rpx'}" :title="item.ifName" :subtitle="item.ifCode">
				
				<template #subtitle>
					<template v-if="item.channelBindInfo.state == 2 && item.channelBindInfo.errInfo">
						<text style="color: red;">{{item.channelBindInfo.errInfo}}</text>
					</template>
				</template>
				
				<template #titleRight>
					
					<template v-if="item.channelBindInfo.state == 0" >
						<text class="state-dot state-dot-disable"></text> &nbsp;<text @tap="terminalChannelBindSendFunc(item.ifCode)">未报备</text>
					</template>
					
					<template v-if="item.channelBindInfo.state == 1">
						<text class="state-dot state-dot-enable"></text> &nbsp;<text >报备成功</text>
					</template>
					
					<template v-if="item.channelBindInfo.state == 2">
						<view class="state-dot state-dot-error"></view> &nbsp;<text @tap="terminalChannelBindSendFunc(item.ifCode)">报备失败</text>
					</template>
				</template>
			</JeepayTableListItem>
		</template>
		
	</JeepayCard>

  </JeepayBackground>

  <JeepayPopupConfirm ref="jeepayPopupConfirmRef" />
</template>

<script setup>
import { reactive, ref } from 'vue'
import { onLoad, onUnload } from '@dcloudio/uni-app'
import { reqLoad, API_URL_SYS_TERMINALS, $updateTrmDefault, $terminalChannelBindList, $terminalChannelBindSend } from '@/http/apiManager.js'
import go from '@/commons/utils/go.js'
import emit from '@/commons/utils/emit.js'
import infoBox from '@/commons/utils/infoBox.js'
import ak from '@/commons/utils/ak.js'

const jeepayPopupConfirmRef = ref()

onLoad((options) => {
	refData(options.trmId)
})

const vdata = reactive({
  record: {},
  
  channelList: [], //渠道列表
})


// 监听 更新事件 
onUnload(() => uni.$off(emit.ENAME_REF_TERMINAL_DETAIL))
uni.$on(emit.ENAME_REF_TERMINAL_DETAIL, function(data){
	refData(vdata.record.trmId)
})

function refData(id){
	
	reqLoad.getById(API_URL_SYS_TERMINALS, id).then(({ bizData }) => {
	  vdata.record = bizData
	})
	
	
	if(ak.ent.has('ENT_TERMINAL_SENDUP')){
		// 查询渠道报备列表
		$terminalChannelBindList(id).then(({bizData}) => {
			vdata.channelList = bizData
		})	
	}
	
}



function deleteFunc () {
  jeepayPopupConfirmRef.value.open('您确认删除终端吗？').then(() => {
    reqLoad.delById(API_URL_SYS_TERMINALS, vdata.record.trmId).then(() => {
      infoBox.showSuccessToast('删除成功')
      return go.back(1, emit.ENAME_REF_TERMINAL_LIST)
    })
  })
}
const list = reactive([
  { label: '删除终端', value: 'delete', color: 'red', fun: deleteFunc },
])

// 更新状态
function updateState(state) {
  return reqLoad.updateById(API_URL_SYS_TERMINALS, vdata.record.trmId, { state: state }).then(() => {
    emit.refPageAndSearchEmit(emit.ENAME_REF_TERMINAL_LIST) // 修改列表页面。
    infoBox.showSuccessToast('修改成功')
  })
}

// 更新状态
function updateDefault(defaultFlag) {
	
  return $updateTrmDefault(vdata.record.trmId, defaultFlag).then(() => {
    emit.refPageAndSearchEmit(emit.ENAME_REF_TERMINAL_LIST) // 修改列表页面。
    infoBox.showSuccessToast('修改成功')
  })
}

function terminalChannelBindSendFunc(ifCode){
	$terminalChannelBindSend(vdata.record.trmId, ifCode).then(() => {
		infoBox.showToast('操作完成').then(() => {
			refData(vdata.record.trmId)
		})
	})
}

</script>

<style lang="scss" scoped>
.default-image {
  width: 50rpx;
  height: 50rpx;
}
</style>
