<template>
	<view class="bg-[var(--page-bg-color)] min-h-[100vh] overflow-hidden form-edit" :style="themeColor()">
		<u-form labelPosition="left" :model="formData" errorType='toast' :rules="rules" ref="formRef"  labelWidth="180rpx">
			<view class="sidebar-margin card-template mt-[var(--top-m)] py-[20rpx]">
				<view>
					<u-form-item :label="t('name')" prop="name" borderBottom required>
						<u-input v-model="formData.name" :placeholder="t('namePlaceholder')" border="none"></u-input>
					</u-form-item>
				</view>
				<view class="mt-[16rpx]">
					<u-form-item :label="t('linkman')" prop="linkman" borderBottom>
						<u-input v-model="formData.linkman" :placeholder="t('linkmanPlaceholder')" border="none"></u-input>
					</u-form-item>
				</view>
				<view class="mt-[16rpx]">
					<u-form-item :label="t('phone')" prop="phone" borderBottom>
						<u-input v-model="formData.phone" :placeholder="t('phonePlaceholder')" border="none"></u-input>
					</u-form-item>
				</view>
				<view class="mt-[16rpx]">
					<u-form-item :label="t('postal')" prop="postal" borderBottom>
						<u-input v-model="formData.postal" :placeholder="t('postalPlaceholder')" border="none"></u-input>
					</u-form-item>
				</view>
				<view class="mt-[16rpx]">
					<u-form-item :label="t('city')" prop="cityName" borderBottom>
						<view class="flex w-full items-center h-[52rpx]" @click="selectArea">
							<view v-if="!formData.cityName" class="text-[#888] text-[28rpx] flex-1">{{ t('cityPlaceholder') }}</view>
							<view v-else class="text-[28rpx] flex-1 leading-[1.4]">{{ formData.cityName }}</view>
						</view>
					</u-form-item>
				</view>
				<view class="mt-[16rpx]">
					<u-form-item :label="t('address')" prop="address" borderBottom>
						<u-input v-model="formData.address" :placeholder="t('addressPlaceholder')" border="none"></u-input>
					</u-form-item>
				</view>
				<view class="mt-[16rpx]">
					<u-form-item :label="t('remark')" prop="remark" borderBottom>
						<u-textarea v-model="formData.remark" :placeholder="t('remarkPlaceholder')" border="none"></u-textarea>
					</u-form-item>
				</view>
			</view>
		</u-form>
		<view class="w-full footer">
			<view class="py-[var(--top-m)] px-[var(--sidebar-m)] footer w-full fixed bottom-0 left-0 right-0 box-border">
				<button hover-class="none" class="!bg-[var(--primary-color)] text-[#fff] h-[80rpx] leading-[80rpx] rounded-[100rpx] text-[26rpx] font-500" 
				@click="handSave" :disabled="loading" :class="{'opacity-50': loading}">{{t('save')}}</button>
			</view>
		</view>
		<area-select ref="areaRef" @complete="areaSelectComplete" :area-id="0"/>
		
	</view>
</template>

<script setup lang="ts">
	import { ref, reactive } from 'vue'
	import { t } from '@/locale'
	import { addWarehouse } from '@/addon/erp/api/base';
	import { generatedCode } from '@/addon/erp/utils/common';
	import { redirect } from '@/utils/common';
	
	const formRef: any = ref(null)
	const loading = ref(false)
	const areaRef = ref()
	const isSelectAddress = ref(false)
	
	
	const rules = reactive({
		name: {
			type: 'string',
			required: true,
			message: t('namePlaceholder'),
			trigger: ['blur', 'change'],
		}
	});
	const formData = reactive({
		number: generatedCode("C"),
		name: '',
		linkman: '',
		phone: '',
		postal: '',
		city: '',
		cityName: '',
		address: '',
		remark: ''
	})
	
	const selectArea = () => {
		isSelectAddress.value = true
	    areaRef.value.open()
	}
	
	const areaSelectComplete = (event: any) => {
		let cityArr = [] as any[];
		cityArr = [event.province.id || 0, event.city.id || 0, event.district.id || 0];
		
	    formData.city = cityArr.join(',');
	    formData.cityName = `${event.province.name || ''}${event.city.name || ''}${event.district.name || ''}`
		isSelectAddress.value = false;
	}
	
	// 保存
	const handSave = () => {
		formRef.value.validate().then(() => {
		    if (loading.value) return
		    loading.value = true
		
		    addWarehouse(formData).then((res: any) => {
				loading.value = false
				if(res.code == 1){
					redirect({ url: '/addon/erp/pages/base/warehouse/list' })
				}
		    }).catch((err) => {
				// 如果是4001，没有绑定企业账号，强制跳转绑定
				if(err.code == 4001){
					redirect({ url: '/addon/erp/pages/member/bind' })
				}
		        loading.value = false
		    })
		})
	}
</script>

<style>
</style>