Files
BMS/bms-front/src/views/price/basic/zone/components/ZoneDrawer.vue
T

223 lines
7.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<el-drawer v-model="visible" size="860px" append-to-body>
<template #header>
<div class="drawer-head">
<span class="drawer-title">国家分区</span>
<el-select
v-model="selectedRegionId"
filterable
clearable
placeholder="选择国家"
class="country-select"
@change="loadZoneList"
>
<el-option
v-for="r in regionOptions"
:key="r.regionId"
:label="regionLabel(r)"
:value="r.regionId"
/>
</el-select>
<span v-if="selectedCountry" class="drawer-sub">
当前:{{ selectedCountry.countryNameCn }}
<template v-if="selectedCountry.countryCode2">({{ selectedCountry.countryCode2 }})</template>
,共 {{ zoneList.length }} 个分区
</span>
</div>
</template>
<div v-loading="zoneLoading">
<div class="zone-toolbar">
<el-button
type="primary"
plain
icon="Plus"
size="small"
:disabled="!selectedCountry"
@click="handleZoneAdd"
v-hasPermi="['price:zone:add']"
>新增分区</el-button>
<el-button
type="danger"
plain
icon="Delete"
size="small"
:disabled="!selectedCountry"
@click="handleClearZones"
v-hasPermi="['price:zone:remove']"
>清空分区</el-button>
</div>
<el-table
:data="zoneList"
border
highlight-current-row
row-key="zoneId"
:empty-text="selectedCountry ? '该国家暂无分区,点击「新增分区」创建' : '请先选择国家'"
>
<el-table-column label="国家" align="center" min-width="180">
<template #default="{ row }">
<div class="zone-country-cell">
<div class="zone-title">{{ row.zoneName || ((selectedCountry?.countryNameCn || '') + '(' + row.zoneCode + ')') }}</div>
<el-button link type="primary" class="zone-click" @click="openPostcodeDrawer(row)" v-hasPermi="['price:zone:query']">分区点击</el-button>
</div>
</template>
</el-table-column>
<el-table-column label="国家编码" prop="countryCode" align="center" width="110" />
<el-table-column label="分区代码" prop="zoneCode" align="center" width="120" />
<el-table-column label="邮编数" prop="detailCount" align="center" width="100" />
<el-table-column label="排序" prop="zoneSort" align="center" width="80" />
<el-table-column label="操作" align="center" width="220" fixed="right">
<template #default="{ row }">
<el-button link type="primary" icon="View" @click="openPostcodeDrawer(row)" v-hasPermi="['price:zone:query']">明细</el-button>
<el-button link type="primary" icon="Edit" @click="handleZoneUpdate(row)" v-hasPermi="['price:zone:edit']">编辑</el-button>
<el-button link type="primary" icon="Delete" @click="handleZoneDelete(row.zoneId)" v-hasPermi="['price:zone:remove']">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<zone-form-dialog ref="zoneFormRef" :country="selectedCountry" @success="onZoneChanged" />
<postcode-drawer ref="postcodeDrawerRef" @changed="onZoneChanged" />
</el-drawer>
</template>
<script setup name="ZoneDrawer">
import { listRegionOptions } from '@/api/data/region'
import {
delCountryZones,
listZone, addZone, updateZone, delZone
} from '@/api/price/zone'
import ZoneFormDialog from './ZoneFormDialog.vue'
import PostcodeDrawer from './PostcodeDrawer.vue'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['changed'])
const visible = ref(false)
const selectedRegionId = ref(null)
const selectedCountry = computed(() =>
regionOptions.value.find(r => r.regionId === selectedRegionId.value) || null
)
const zoneList = ref([])
const zoneLoading = ref(false)
const regionOptions = ref([])
const zoneFormRef = ref()
const postcodeDrawerRef = ref()
function regionLabel(r) {
const cn = r.countryNameCn || r.countryNameEn || ''
return cn ? `${r.flagEmoji || ''} ${cn} (${r.countryCode2})`.trim() : r.countryCode2
}
function ensureRegions() {
if (regionOptions.value.length) return Promise.resolve()
return listRegionOptions().then(res => {
regionOptions.value = res.data || []
})
}
/** 打开抽屉;传入国家行则自动选中,不传则让用户在头部下拉选择 */
function open(country) {
visible.value = true
ensureRegions().then(() => {
selectedRegionId.value = country?.regionId || null
loadZoneList(selectedRegionId.value)
})
}
function reload() {
loadZoneList(selectedRegionId.value)
}
function loadZoneList(regionId) {
if (!regionId) {
zoneList.value = []
return
}
zoneLoading.value = true
listZone({ zoneType: 'REGULAR', regionId, pageNum: 1, pageSize: 200 }).then(res => {
zoneList.value = res.rows || []
}).catch(() => {
zoneList.value = []
}).finally(() => {
zoneLoading.value = false
})
}
function handleZoneAdd() {
zoneFormRef.value.open()
}
function handleZoneUpdate(row) {
zoneFormRef.value.open(row)
}
/** 分区增删改 / 邮编变动后:刷新分区列表,并通知父页刷新国家表格统计 */
function onZoneChanged() {
loadZoneList(selectedRegionId.value)
emit('changed')
}
function handleZoneDelete(zoneIds) {
proxy.$modal.confirm('是否确认删除分区编号为"' + zoneIds + '"的数据?将同时删除邮编明细。').then(() => {
return delZone(zoneIds)
}).then(() => {
proxy.$modal.msgSuccess('删除成功')
onZoneChanged()
}).catch(() => {})
}
function handleClearZones() {
if (!selectedRegionId.value) return
proxy.$modal.confirm('是否清空国家 regionId="' + selectedRegionId.value + '" 下的全部分区与邮编?(不会删除 region 国家主数据)').then(() => {
return delCountryZones(selectedRegionId.value)
}).then(() => {
zoneList.value = []
proxy.$modal.msgSuccess('清空成功')
emit('changed')
}).catch(() => {})
}
/** 抽屉打开期间,外部清空了某国家(国家表格「清空」按钮)时同步列表 */
function onCountryCleared(regionIds) {
if (visible.value && String(regionIds).split(',').map(String).includes(String(selectedRegionId.value))) {
zoneList.value = []
}
}
function openPostcodeDrawer(row) {
postcodeDrawerRef.value.open(row)
}
defineExpose({ open, reload, onCountryCleared })
</script>
<style scoped>
.drawer-head {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.drawer-title { font-size: 16px; font-weight: 600; }
.drawer-sub { color: #909399; font-size: 13px; }
.country-select { width: 260px; }
.zone-toolbar {
display: flex;
align-items: center;
margin-bottom: 12px;
}
.zone-country-cell {
display: flex;
flex-direction: column;
align-items: center;
gap: 2px;
padding: 4px 0;
}
.zone-title { font-weight: 500; }
.zone-click { padding: 0; height: auto; font-size: 12px; }
</style>