<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
      <Form v-show="searchOpen" inline onsubmit="return false" :label-width="90" class="search-form">
        <label class="label-sign"></label>
        <Form-item class="search-item" label="省份(中文):">
          <el-input v-model="formObj.cn" placeholder="请输入省份名称" @keyup.enter.native="_formSearch" clearable/>
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </el-col>
      <!--操作-->
      <el-col :span="24">
      <btn-list :msg="btn" :open="searchOpen" :show-search-btn="true" @on-result-change="_btnClick" />
      </el-col>
      <!-- 表格 -->
      <el-col :span="24">
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="80"
        :icon-msg="iconMsg"
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          show-overflow-tooltip
          sortable
          :prop="item.key"
          :label="item.title"
          :min-width="item.width"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <span>{{ scope.row[item.key] }}</span>
          </template>
        </el-table-column>
      </element-table>
      </el-col>
    </el-row>
    <!-- 添加、编辑 -->
    <sysProvinceEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { sysProvince } from '../../../api'
import sysProvinceEdit from './sysProvinceEdit.vue'

export default {
  components: {
    sysProvinceEdit
  },
  data: function() {
    return {
      formObj: {
        cn: ''
      },
      btn: [{ type: 'success', id: 'sys-province-add', name: '添加' }],
      iconMsg: [{ type: 'pt-edit', id: 'sys-province-edit', name: '编辑' }],
      pageColumns: [
        { title: '省份编码', key: 'code' },
        { title: '中文名', key: 'cn' },
        { title: '英文名', key: 'en' },
        { title: '国家二字码', key: 'countryCode2' }
      ],
      getPage: {},
      searchOpen: false
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysProvince.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _editModal: async function(id) {
      const result = await sysProvince.getById(id)
      this.$refs.editModal._open(result)
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    }
  }
}
</script>
