<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="areaCodeList" @selection-change="handleSelectionChange">
      <el-table-column label="行政区编码" align="center" prop="code"/>
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="级别" align="center" prop="level" >
        <template v-slot="scope">
          <cai-dict-tag :options="areaCodeLevelList" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="上级编码" align="center" prop="pcode" />
      <el-table-column label="上级名称" align="center" prop="pname" />
      <el-table-column label="完整名字" align="center" prop="fullname" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listAreaCode } from '@/api/xq/areaCode'
import { areaCodeLevelList } from '@/constant/statusMap'

export default {
  name: "AreaCode",
  data() {
    return {
      areaCodeLevelList,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行政区划表格数据
      areaCodeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        level: undefined,
        pcode: undefined,
        pname: undefined,
        fullname: undefined,
        longitude: undefined,
        latitude: undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询行政区划列表 */
    getList() {
      this.loading = true;
      listAreaCode(this.queryParams).then(response => {
        this.areaCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.code)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  }
};
</script>
