<!--
  Jeepay 门店选择 / 应用选择 
  
  @author terrfly
  @site https://www.jeequan.com
  @date 2022/12/06 12:55
-->
<template>
	<!-- 选择门店 -->
	<JeepayPopupListSelect
		ref="jeepayPopupListSelectByBizinfos"
		:reqTableDataFunc="reqTableDataByBizsFunc"
		:searchInputName="getSearchInputName()"
		:fields="getField()"
		:isCheckbox="props.isCheckbox"
		:addUse="props.addUse"
		@confirm="confirm"
	/>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { reqLoad, API_URL_MCH_STORE_LIST, API_URL_MCH_APP_LIST, API_URL_MCH_ISV_LIST } from '@/http/apiManager.js';

// 定义组件参数
const props = defineProps({
	configMode: '',
	// 是否多选框， 默认单选。
	isCheckbox: { type: Boolean, default: false },

	// 自动关闭， 点击确定， 自动关闭
	autoClose: { type: Boolean, default: true },

	// 业务类型： 默认门店
	bizType: { type: String, default: 'store' },
	range: { type: String, default: 0 },
	ifCode: { type: String, default: '' },

	// 是否支持选择 【 全部门店 、 全部应用 】
	isShowAllBiz: { type: Boolean, default: false },
	// 特殊参数处理
	params: { type: Object, default: () => ({}) },
	addUse: { type: Boolean, default: false }
});

const jeepayPopupListSelectByBizinfos = ref();
const emits = defineEmits(['confirm']);
function confirm(data) {
	console.log(data);
	emits('confirmData', data);
}
console.log(props.isCheckbox);
function getField() {
	if (props.bizType == 'store') {
		return { key: 'storeId', left: 'storeName', right: 'storeId' };
	}

	if (props.bizType == 'mchApp') {
		return { key: 'appId', left: 'appName', right: 'appId' };
	}

	if (props.bizType == 'isvApp') {
		return { key: 'isvNo', left: 'isvName', right: 'isvNo' };
	}
}

function getSearchInputName() {
	if (props.bizType == 'store') {
		return 'storeName';
	}

	if (props.bizType == 'mchApp') {
		return 'appName';
	}
	if (props.bizType == 'isvApp') {
		return 'isvName';
	}
}

function reqTableDataByBizsFunc(params) {
	Object.assign(params, props.params);
	console.log(params);

	let apiResult = null;

	if (props.bizType == 'store') {
		apiResult = reqLoad.list(API_URL_MCH_STORE_LIST, params);
	}
	if (props.bizType == 'mchApp') {
		apiResult = reqLoad.list(API_URL_MCH_APP_LIST, params);
	}

	if (props.bizType == 'isvApp') {
		params.ifCode = props.ifCode;
		params.range = props.range;
		apiResult = reqLoad.list(API_URL_MCH_ISV_LIST, params);
	}
	return apiResult.then((apiRes) => {
		return processApiResBizData(params, apiRes);
	});
}

// 选择门店
function open(defaultVal) {
	console.log(defaultVal, 'defaultValdefaultVal');
	return jeepayPopupListSelectByBizinfos.value.open(defaultVal).then((selected) => {
		// 自动关闭
		if (props.autoClose) {
			close();
		}

		return selected;
	});
}

function close() {
	jeepayPopupListSelectByBizinfos.value.close(); //自行关闭
}

function processApiResBizData(params, apiRes) {
	// 第一页 拼接全部门店 (index = 0 )
	if (params.pageNumber == 1 && props.isShowAllBiz) {
		if (props.bizType == 'store') {
			apiRes.bizData.records.unshift({ storeName: '全部门店', storeId: '' });
		}

		if (props.bizType == 'mchApp') {
			apiRes.bizData.records.unshift({ appName: '全部应用', appId: '' });
		}

		if (props.bizType == 'isvApp') {
			apiRes.bizData.records.unshift({ appName: '全部渠道', appId: '' });
		}
	}

	return apiRes;
}

// 将表格事件暴露出去  https://www.jianshu.com/p/39d14c25c987
defineExpose({ open, close });
</script>

<style></style>
