<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="areaId">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父ID" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="祖级列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖级列表"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拼音" prop="pinyin">
        <el-input
          v-model="queryParams.pinyin"
          placeholder="请输入拼音"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份" prop="provinceName">
        <el-input
          v-model="queryParams.provinceName"
          placeholder="请输入省份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份简称" prop="provinceShortname">
        <el-input
          v-model="queryParams.provinceShortname"
          placeholder="请输入省份简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份拼音" prop="provincePinyin">
        <el-input
          v-model="queryParams.provincePinyin"
          placeholder="请输入省份拼音"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="cityName">
        <el-input
          v-model="queryParams.cityName"
          placeholder="请输入城市"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市简称" prop="cityShortname">
        <el-input
          v-model="queryParams.cityShortname"
          placeholder="请输入城市简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市拼音" prop="cityPinyin">
        <el-input
          v-model="queryParams.cityPinyin"
          placeholder="请输入城市拼音"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区县" prop="districtName">
        <el-input
          v-model="queryParams.districtName"
          placeholder="请输入区县"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区县简称" prop="districtShortname">
        <el-input
          v-model="queryParams.districtShortname"
          placeholder="请输入区县简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区县拼音" prop="districtPinyin">
        <el-input
          v-model="queryParams.districtPinyin"
          placeholder="请输入区县拼音"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简拼" prop="jianpin">
        <el-input
          v-model="queryParams.jianpin"
          placeholder="请输入简拼"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="首字母" prop="firstChar">
        <el-input
          v-model="queryParams.firstChar"
          placeholder="请输入首字母"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区号" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入区号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮编" prop="zipCode">
        <el-input
          v-model="queryParams.zipCode"
          placeholder="请输入邮编"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="lontitude">
        <el-input
          v-model="queryParams.lontitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:area:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="areaList"
      row-key=""
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="ID" align="center" prop="areaId" />
      <el-table-column label="父ID" prop="parentId" />
      <el-table-column label="祖级列表" align="center" prop="ancestors" />
      <el-table-column label="级别" align="center" prop="levelType" />
      <el-table-column label="名称" align="center" prop="areaName" />
      <el-table-column label="简称" align="center" prop="shortName" />
      <el-table-column label="拼音" align="center" prop="pinyin" />
      <el-table-column label="省份" align="center" prop="provinceName" />
      <el-table-column label="省份简称" align="center" prop="provinceShortname" />
      <el-table-column label="省份拼音" align="center" prop="provincePinyin" />
      <el-table-column label="城市" align="center" prop="cityName" />
      <el-table-column label="城市简称" align="center" prop="cityShortname" />
      <el-table-column label="城市拼音" align="center" prop="cityPinyin" />
      <el-table-column label="区县" align="center" prop="districtName" />
      <el-table-column label="区县简称" align="center" prop="districtShortname" />
      <el-table-column label="区县拼音" align="center" prop="districtPinyin" />
      <el-table-column label="简拼" align="center" prop="jianpin" />
      <el-table-column label="首字母" align="center" prop="firstChar" />
      <el-table-column label="区号" align="center" prop="areaCode" />
      <el-table-column label="邮编" align="center" prop="zipCode" />
      <el-table-column label="经度" align="center" prop="lontitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="特别提示" align="center" prop="remark" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="区域状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:area:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:area:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:area:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改区域-省市区县对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="areaRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入ID" />
        </el-form-item>
        <el-form-item label="父ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父ID" />
        </el-form-item>
        <el-form-item label="名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="form.pinyin" placeholder="请输入拼音" />
        </el-form-item>
        <el-form-item label="省份" prop="provinceName">
          <el-input v-model="form.provinceName" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="省份简称" prop="provinceShortname">
          <el-input v-model="form.provinceShortname" placeholder="请输入省份简称" />
        </el-form-item>
        <el-form-item label="省份拼音" prop="provincePinyin">
          <el-input v-model="form.provincePinyin" placeholder="请输入省份拼音" />
        </el-form-item>
        <el-form-item label="城市" prop="cityName">
          <el-input v-model="form.cityName" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="城市简称" prop="cityShortname">
          <el-input v-model="form.cityShortname" placeholder="请输入城市简称" />
        </el-form-item>
        <el-form-item label="城市拼音" prop="cityPinyin">
          <el-input v-model="form.cityPinyin" placeholder="请输入城市拼音" />
        </el-form-item>
        <el-form-item label="区县" prop="districtName">
          <el-input v-model="form.districtName" placeholder="请输入区县" />
        </el-form-item>
        <el-form-item label="区县简称" prop="districtShortname">
          <el-input v-model="form.districtShortname" placeholder="请输入区县简称" />
        </el-form-item>
        <el-form-item label="区县拼音" prop="districtPinyin">
          <el-input v-model="form.districtPinyin" placeholder="请输入区县拼音" />
        </el-form-item>
        <el-form-item label="简拼" prop="jianpin">
          <el-input v-model="form.jianpin" placeholder="请输入简拼" />
        </el-form-item>
        <el-form-item label="首字母" prop="firstChar">
          <el-input v-model="form.firstChar" placeholder="请输入首字母" />
        </el-form-item>
        <el-form-item label="区号" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入区号" />
        </el-form-item>
        <el-form-item label="邮编" prop="zipCode">
          <el-input v-model="form.zipCode" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="经度" prop="lontitude">
          <el-input v-model="form.lontitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="特别提示" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入特别提示" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Area">
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/system/area"

const { proxy } = getCurrentInstance()

const areaList = ref([])
const areaOptions = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const title = ref("")
const isExpandAll = ref(true)
const refreshTable = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    areaId: null,
    parentId: null,
    ancestors: null,
    levelType: null,
    areaName: null,
    shortName: null,
    pinyin: null,
    provinceName: null,
    provinceShortname: null,
    provincePinyin: null,
    cityName: null,
    cityShortname: null,
    cityPinyin: null,
    districtName: null,
    districtShortname: null,
    districtPinyin: null,
    jianpin: null,
    firstChar: null,
    areaCode: null,
    zipCode: null,
    lontitude: null,
    latitude: null,
    type: null,
    orderNum: null,
    status: null
  },
  rules: {
    areaId: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询区域-省市区县列表 */
function getList() {
  loading.value = true
  listArea(queryParams.value).then(response => {
    areaList.value = proxy.handleTree(response.data, "", "")
    loading.value = false
  })
}

/** 查询区域-省市区县下拉树结构 */
function getTreeselect() {
  listArea().then(response => {
    areaOptions.value = []
    const data = { area_id: 0, name : '顶级节点', children: [] }
    data.children = proxy.handleTree(response.data, "", "")
    areaOptions.value.push(data)
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    areaId: null,
    parentId: null,
    ancestors: null,
    levelType: null,
    areaName: null,
    shortName: null,
    pinyin: null,
    provinceName: null,
    provinceShortname: null,
    provincePinyin: null,
    cityName: null,
    cityShortname: null,
    cityPinyin: null,
    districtName: null,
    districtShortname: null,
    districtPinyin: null,
    jianpin: null,
    firstChar: null,
    areaCode: null,
    zipCode: null,
    lontitude: null,
    latitude: null,
    type: null,
    remark: null,
    orderNum: null,
    delFlag: null,
    status: null
  }
  proxy.resetForm("areaRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset()
  getTreeselect()
  if (row != null && row.areaId) {
    form.value.parentId = row.areaId
  } else {
    form.value.parentId = 0
  }
  open.value = true
  title.value = "添加区域-省市区县"
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset()
  await getTreeselect()
  if (row != null) {
    form.value.parentId = row.parentId
  }
  getArea(row.areaId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改区域-省市区县"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["areaRef"].validate(valid => {
    if (valid) {
      if (form.value.areaId != null) {
        updateArea(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addArea(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除区域-省市区县编号为"' + row.areaId + '"的数据项？').then(function() {
    return delArea(row.areaId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

getList()
</script>
