<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable standout="bg-primary text-white" class="col-6 q-pr-lg q-pr-lg"
            v-model="msg.Name" label="名称" maxlength="20" @clear="resetSearch" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-tow-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">地区信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" class="q-mr-md" size="sm" icon="add" label="新增地区" @click="editArea(null)" />
          </div>
        </template>
        <template v-slot:body-cell-CodeLevel="props">
          <q-td :props="props">
            <span v-if="props.row.CodeLevel==1">
              国家
            </span>
            <span v-if="props.row.CodeLevel==2">
              省
            </span>
            <span v-if="props.row.CodeLevel==3">
              市
            </span>
            <span v-if="props.row.CodeLevel==4">
              区
            </span>
          </q-td>
        </template>
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-toggle size="md" color="primary" :false-value="1" :true-value="0" v-model="props.row.Status"
              title="注意：关闭后，分类将无法正常使用." />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="editArea(props.row)" />

            <q-btn flat size="xs" icon="delete" color="negative" style="font-weight:400" label="删除"
              @click="delArea(props.row)" />
          </q-td>
        </template>
      </q-table>
      <area-form v-if="isShowAreaForm" :save-obj="areaObjOption" @close="refreshPage" @success="refreshPage">
      </area-form>
    </div>
  </div>
</template>
<script>
  import {
    queryAreaPage,
    deleteArea
  } from '../../api/common/common'
  import areaForm from '../../components/system/area-form'
  export default {
    meta: {
      title: "地区管理"
    },
    components: {
      areaForm,
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'ID',
            label: '编号',
            field: 'ID',
            align: 'left'
          },
          {
            name: 'Name',
            required: true,
            label: '名称',
            align: 'left',
            field: row => row.Name
          },
          {
            name: 'CodeLevel',
            label: '行政级别',
            field: 'CodeLevel',
            align: 'left'
          },
          {
            name: 'CountryName',
            label: '所属国家',
            field: 'CountryName',
            align: 'left'
          },
          {
            name: 'ProvinceName',
            label: '所属省份',
            field: 'ProvinceName',
            align: 'left'
          },
          {
            name: 'CityName',
            label: '所属市',
            field: 'CityName',
            align: 'left'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'MenuId'
          }
        ],
        data: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          Name: "",
        },
        pageCount: 0,
        isShowAreaForm: false, //是否显示新增修改地区表单
        areaObjOption: null,
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getAreapage();
    },
    methods: {
      //删除地区
      delArea(item) {
        var that=this;
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          let delMsg = {
            ID: item.ID,
          }
          deleteArea(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.refreshPage();
            }
          })
        })
      },
      //刷新页面
      refreshPage() {
        this.isShowAreaForm = false;
        this.areaObjOption = null;
        this.getAreapage();
      },
      //新增修改地区
      editArea(obj) {
        if (obj != null) {
          this.areaObjOption = obj;
        }
        this.isShowAreaForm = true;
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getAreapage();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getAreapage()
      },
      //获取地区分页列表
      getAreapage() {
        this.loading = true;
        queryAreaPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
    }
  }
</script>
<style lang="sass">
  @import url('~assets/css/table.sass')
</style>