<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"
        row-key="code"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="名字" align="left" prop="name" />
      <el-table-column label="行政区划代码" align="center" prop="code"/>
      <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="enableStatus" >
        <template v-slot="scope">
          <cai-dict-tag :options="enableStatusList" :value="scope.row.enableStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xq:areaCode:edit']"
          >修改</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xq:areaCode:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

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

    <areacode-update-dialog v-if="areaCodeUpdateDialogVisible" ref="areaCodeUpdateDialog" @refreshDataList="getList" />
  </div>
</template>

<script>
import { delAreaCode, listAreaCode } from '@/api/xq/areaCode'
import { areaCodeLevelList, enableStatusList } from '@/constant/statusMap'
import AreacodeUpdateDialog from '@/views/xq/areaCode/areacode-update-dialog.vue'
import BannerAddUpdateDialog from '@/views/xq/banner/banner-add-update-dialog.vue'

export default {
  name: "AreaCode",
  components: { BannerAddUpdateDialog, AreacodeUpdateDialog},
  data() {
    return {
      enableStatusList,areaCodeLevelList,
      areaCodeUpdateDialogVisible: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行政区划表格数据
      areaCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        enableStatus: undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询行政区划列表 */
    getList() {
      this.loading = true;
      listAreaCode(this.queryParams).then(response => {
        this.areaCodeList = this.handleTree(response.data, "code","pcode");
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.areaCodeUpdateDialogVisible = true
      this.$nextTick(() => {
        this.$refs.areaCodeUpdateDialog.init(row)
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const codes = row.code || this.ids;
      this.$modal.confirm('是否确认删除行政区划编号为"' + codes + '"的数据项？').then(() => {
        this.loading = true;
        return delAreaCode(codes);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
