<template>
  <div class="base-district-container">
    <div class="pd10">
      {{
        __(
          '编辑地区信息后，需手动更新地区缓存(平台 > 设置 > 清理缓存 >地区)，前台才会生效。'
        )
      }}
      <br />
      {{
        __(
          '所在层级为该地区的所在的层级深度，如北京>北京市>朝阳区,其中北京层级为1，北京市层级为2，朝阳区层级为3'
        )
      }}
      <br />
    </div>
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(null, 0)"
        >
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="fetchData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="table"
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      lazy
      :load="load"
      row-key="district_id"
      :tree-props="{ children: 'children', hasChildren: 'isParent' }"
    >
      <el-table-column align="center" :label="__('操作')" width="200px">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleEdit(row, row.district_id)">
            {{ __('新增') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('地区编号')"
        prop="district_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('地区名称')"
        prop="district_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('父编号')"
        prop="district_parent_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="district_sort"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatLeaf"
        :label="__('是否叶节点-无子类')"
        prop="district_is_leaf"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('等级')"
        prop="district_level"
        show-overflow-tooltip
      />
    </el-table>
    <edit
      ref="edit"
      @fetch-data="refreshNodeBy"
      @refreshNodeBy="refreshNodeBy"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doDelete } from '@/api/base/district'
  import Edit from './components/BaseDistrictEdit'

  export default {
    name: 'BaseDistrict',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        maps: new Map(),
        list: [],
        listLoading: true,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      handleEdit(row, district_parent_id) {
        if (district_parent_id === 0 || district_parent_id) {
          this.$refs['edit'].showEdit(null, district_parent_id)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            district_id: row.district_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          this.refreshNodeBy(row.district_parent_id)
        })
      },
      formatLeaf(row) {
        return !row.isParent ? this.__('是') : this.__('否')
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList()
        this.list = data
        this.listLoading = false
      },
      async load(tree, treeNode, resolve) {
        this.maps.set(tree.district_id, { tree, treeNode, resolve })
        const { data } = await getList({
          district_parent_id: tree.district_id,
          district_level: tree.district_level,
        })
        resolve(data)
      },
      refreshNodeBy(district_parent_id) {
        // 懒加载刷新当前数据
        if (district_parent_id && this.maps.get(district_parent_id)) {
          const { tree, treeNode, resolve } = this.maps.get(district_parent_id)
          this.load(tree, treeNode, resolve)
        }
      },
    },
  }
</script>
