<template>
  <div class="app-container">
    <el-form :model="queryForm" inline size="small">
      <el-form-item label="关键词">
        <el-input v-model="queryForm.keyword" placeholder="关键词"/>
      </el-form-item>
    </el-form>
    <el-row class="action-bar-container" type="flex" justify="end">
      <el-button type="primary" size="small" @click.native="fetchData">查询</el-button>
      <el-button-group>
        <el-button v-permission="['bz.data.info.add']" size="small" @click.native="$refs.dialogForm.open({})">
          新建
        </el-button>
        <el-button v-permission="['bz.data.info.delete']" size="small" @click.native="deleteByIds(multipleSelection)">
          删除
        </el-button>
      </el-button-group>
    </el-row>
    <el-table
      v-loading="isLoading"
      :data="pageData.records"
      border
      fit
      highlight-current-row
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column label="ID" prop="id" width="150"/>
      <el-table-column label="名称" prop="name"/>
      <el-table-column label="缩略图" width="80">
        <template slot-scope="scope">
          {{ scope.row.litpics.length }}张
        </template>
      </el-table-column>
      <el-table-column label="地址" prop="address"/>
      <el-table-column label="联系人" prop="contacts" width="150"/>
      <el-table-column label="联系电话" prop="contactNumber" width="150"/>
      <el-table-column label="位置信息" width="200">
        <template slot-scope="scope">
          [ {{ scope.row.lng }}, {{ scope.row.lat }} ]
        </template>
      </el-table-column>
      <el-table-column label="操作" width="70" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="['bz.data.info.edit']" type="text" @click.native="$refs.dialogForm.open(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-container"
      background
      layout="prev, pager, next"
      :total="pageData.total"
      :page-size="pageData.pageSize"
      @current-change="paginationChange"
    />
    <dialog-form ref="dialogForm" @ok="fetchData"/>
  </div>
</template>

<script>
import { deleteByIds, findPage } from '@/api/base/data_info'
import { dateTimeFormatter } from '@/utils/formater'
import DialogForm from './DialogForm'

export default {
  components: { DialogForm },
  data() {
    return {
      queryForm: {},
      isLoading: true,
      pageData: {},
      currentPage: 1,
      multipleSelection: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    dateTimeFormatter,
    selectionChange(val) {
      const temp = []
      val.forEach(o => {
        temp.push(o.id)
      })
      this.multipleSelection = temp
    },
    deleteByIds(ids) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteByIds(ids).then(() => {
          this.fetchData()
        })
      })
    },
    paginationChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      this.isLoading = true
      findPage({ ...this.queryForm, current: this.currentPage })
        .then((response) => {
          this.pageData = response
        })
        .finally(() => (this.isLoading = false))
    }
  }
}
</script>
