import { tableList } from '@/utils/format';
import request from '@/utils/request';
import type { LinkManModel, MerchantModel, MerchantTypeModel } from './data';

// 获取列表数据
export async function getList(params?: any) {
	const search = params ? {
		searchParameter: { ...params }, keyWord: params.keyWord,
		sortstr: params.sortstr, PageIndex: params.current, pagesize: params.pagesize
	} : {};
	const data = await request(`/Merchants/GetCoopMerchantsList`, {
		method: 'POST',
		data: search,
	});

	if (data.Result_Code !== 100) {
		return {
			data: [],
			current: 1,
			pageSize: 10,
			total: 0,
			success: false
		}
	}

	return tableList(data.Result_Data);
}

export async function delMerchants(Merchantsid: number) {
	return request(`/Merchants/DeleteCoopMerchants?Merchantid=${Merchantsid}`, {
		method: 'POST',

		requestType: 'form',
	});
}

export async function updateMerchants(Merchants: MerchantModel) {
	return request(`/Merchants/SynchroCoopMerchants`, {
		method: 'POST',
		data: Merchants,
		requestType: 'form',
	});
}

// 获取联系人列表数据
export async function getLinkManList(merchartsId: any) {
	const search = { searchParameter: { COOPMERCHANTS_ID: merchartsId, LINKER_STATE: 1 }, PageIndex: 1, pagesize: 20 }
	const data = await request(`/Merchants/GetCoopMerchantsLinkerList`, {
		method: 'POST',
		data: search,
	});
	if (data.Result_Code !== 100) {
		return []
	}
	return data.Result_Data.List;
}



export async function delLinkMan(Merchantsid: number) {
	return request(`/Merchants/DeleteCoopMerchantsLinker?CoopMerchantsLinkerId=${Merchantsid}`, {
		method: 'POST',

		requestType: 'form',
	});
}

export async function updateLinkMan(Merchants: LinkManModel) {
	return request(`/Merchants/SynchroCoopMerchantsLinker`, {
		method: 'POST',
		data: Merchants,
		requestType: 'form',
	});
}

// end===


// 商户类别
export async function getTypeList(params?: any) {
	const search = params ? { ...params, PageIndex: params.current } : {};
	const data = await request(`/Merchants/GetAutoTypeList`, {
		method: 'GET',
		params: search,
	});

	if (data.Result_Code === 500) {
		return [];
	}

	return tableList(data.Result_Data);
}

export async function delMerchantsType(autoTypeId: number) {
	return request(`/Merchants/DeleteAutotype?autoTypeId=${autoTypeId}`, {
		method: 'POST',

		requestType: 'form',
	});
}

export async function updateMerchantsType(Merchants: MerchantTypeModel) {
	return request(`/Merchants/SynchroAutotype`, {
		method: 'POST',
		data: Merchants,
		requestType: 'form',
	});
}

// 获取商家类别options 数据
export async function getTypeOptions(params?: any) {
	const search = params ? { ...params, PageIndex: params.current } : {};
	const data = await request(`/Merchants/GetAutoTypeList`, {
		method: 'GET',
		params: search,
	});

	if (data.Result_Code !== 100) {
		return [];
	}
	if (data.Result_Data.List) {
		const options: { label: string; value: number }[] = data.Result_Data.List.map((n: MerchantTypeModel) => {
			const { AUTOTYPE_NAME, AUTOTYPE_ID } = n;
			return ({ label: AUTOTYPE_NAME, value: AUTOTYPE_ID });
		});


		return options;
	}
	return [];
}

// 获取列表数据
export async function getMerchantsOpt(params?: any) {

	const search = { searchParameter: { ...params, COOPMERCHANTS_STATE: 1, pagesize: 9999, pageindex: 1 } }
	const data = await request(`/Merchants/GetCoopMerchantsList`, {
		method: 'POST',
		data: search,
	});

	if (data.Result_Code !== 100) {
		return []
	}
	if (data.Result_Data.List) {
		const options: { label: string; value: number }[] = data.Result_Data.List.map((n: MerchantModel) => {
			const { COOPMERCHANTS_NAME, COOPMERCHANTS_ID } = n;
			return ({ label: COOPMERCHANTS_NAME, value: COOPMERCHANTS_ID });
		});


		return options;
	}
	return data.Result_Data.List;
}

// 商家详情信息
export async function getShopDetail(params?: any) {
	const data = await request(`/Merchants/GetCoopMerchantsDetail`, {
		method: 'GET',
		params,
	});
	if (data.Result_Code !== 100) {
		return {
			data: [],
			current: 1,
			pageSize: 10,
			total: 0,
			success: false
		}
	}
	return data.Result_Data;
}

// 获取品牌列表
export async function getBrandList(params?: any) {
	const data = await request(`/Merchants/GetRTCoopMerchantsList`, {
		method: 'GET',
		params,
	});

	if (data.Result_Code !== 100) {
		return []
	}

	return data.Result_Data.List;
}

// 获取列表数据
export async function getTreeList(params?: any) {
	const search = params ? {
		SearchParameter: { ...params }, keyWord: params.keyWord,
		PageIndex: params.current, pagesize: params.pageSize, SortStr: params.sortstr
	} : {};
	const data = await request(`/BaseInfo/GetBrandList`, {
		method: 'POST',
		data: search,
	});

	if (data.Result_Code !== 100) {
		return {
			data: [],
			current: 1,
			pageSize: 10,
			total: 0,
			success: false
		}
	}

	return tableList(data.Result_Data);
}




// 获取经营商户品牌列表
export async function handleRTCoopMerchantsList(params?: any) {
	const data = await request(`/Merchants/GetRTCoopMerchantsList`, {
		method: 'GET',
		params,
	});

	if (data.Result_Code !== 100) {
		return {
			data: [],
			current: 1,
			pageSize: 10,
			total: 0,
			success: false
		}
	}

	return data.Result_Data.List
}


// 删除经营商户品牌关联关系表
export async function handleDeleteRTCoopMerchants(params?: any) {
	const data = await request(`/Merchants/DeleteRTCoopMerchants`, {
		method: 'GET',
		params,
	});

	if (data.Result_Code !== 100) {
		return {
			data: [],
			current: 1,
			pageSize: 10,
			total: 0,
			success: false
		}
	}

	return data
}


//   同步经营商户品牌关联关系表
export async function handleSynchroRTCoopMerchants(params?: any) {
	const data = await request(`/Merchants/SynchroRTCoopMerchants`, {
		method: 'POST',
		data: params,
	});

	if (data.Result_Code !== 100) {
		return {
			data: [],
			current: 1,
			pageSize: 10,
			total: 0,
			success: false
		}
	}

	return data
}