<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"
	:title="vdata.record.appName" :subtitle="vdata.record.appId" :moreBtnList="list" />

	<JeepayCard editText="编辑信息"
	 @editTap="go.to('/pageDevice/editPage/editPage', { navTitle: '应用名称', info: vdata.record.appName, id: vdata.record.appId, field: 'appName', api: 'app' })"
	 >
	 
	 <JeepayDescview>
		 <JeepayDescviewItem title="应用名称" :desc="vdata.record.appName" />
		 <JeepayDescviewItem title="AppId" :desc="vdata.record.appId" />
		 <JeepayDescviewItem title="创建时间" :desc="vdata.record.createdAt" />
	 </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>

  </JeepayBackground>

</template>

<script setup>
import { reactive, ref } from 'vue'
import { onLoad,onUnload } from '@dcloudio/uni-app'
import { reqLoad, API_URL_MCH_APP_LIST } 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'

onLoad((options) => {
  reqLoad.getById(API_URL_MCH_APP_LIST, options.appId).then(({ bizData }) => {
    vdata.record = bizData
  })
})
onUnload( () => uni.$off(emit.ENAME_REF_TABLE_MCH_APP_DETAILS) )
uni.$on(emit.ENAME_REF_TABLE_MCH_APP_DETAILS, function(data){
	  reqLoad.getById(API_URL_MCH_APP_LIST, vdata.record.appId).then(({ bizData }) => {
    vdata.record = bizData
  })
})
const vdata = reactive({
  record: {},
})

const payConfig = () => go.to("PAGES_APP_CONFIG", {appId: vdata.record.appId } )

const deleteApp = () => {
	reqLoad.delById(API_URL_MCH_APP_LIST, vdata.record.appId).then(() => {
	  infoBox.showSuccessToast('删除成功')
	  return go.back(1, emit.ENAME_REF_TABLE_MCH_APP)
	})
}
const list = reactive([
  { label: '支付配置', value: 'pay', fun: payConfig },
  { label: '删除应用', value: 'delete', color: 'red', confirmText:'您确认删除应用吗？', fun: deleteApp },
])

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

// 更新状态
function updateDefault(defaultFlag) {
  return reqLoad.updateById(API_URL_MCH_APP_LIST, vdata.record.appId, { defaultFlag: defaultFlag }).then(() => {
    emit.pageEmit(emit.ENAME_REF_TABLE_MCH_APP) // 修改列表页面。
    infoBox.showSuccessToast('修改成功')
  })
}
</script>

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