<template>
  <div class="root">
    <el-form :inline="true" :model="listQuery" class="form-listQuery">
      <!-- <el-form-item label="业务名称">
        <el-input v-model="listQuery.serviceName" placeholder="业务名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="clearListQuery">清空</el-button>
        <el-button type="primary" @click="handleFilter">查询</el-button>
      </el-form-item> -->
    </el-form>
    <div class="operation">
      <div>
        <el-button type="primary" @click="addCarousel">新增轮播图</el-button>
        <el-button type="danger" @click="deleteCarousel">删除</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="carouselList"
      border
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="业务名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.serviceName }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="业务图片" align="center">
        <template slot-scope="scope">
          <img v-if="scope.row.imageUrl !== ''" :src="scope.row.imageUrl" width="50" height="50">
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page.sync="listQuery.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size.sync="listQuery.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // Waves directive
import { parseTime } from '@/utils'

export default {
  directives: { waves },
  data() {
    var _self = this
    return {
      events: {
        click: (e) => {
          // _self.postData.Coordinate = e.lnglat.lat + ',' + e.lnglat.lng
          _self.detail.longitude = e.lnglat.lng
          _self.detail.latitude = e.lnglat.lat
        }
      },
      markers: [],
      searchOption: {
        city: '南京',
        citylimit: false
      },
      carouselList: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      ids: [], // id集合
      tableKey: 0,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    setCurrent(item) {
      this.setDetail({ ...item })
    },
    getList() {
      this.listLoading = true
      this.$store.dispatch('carousel/getCarouselList', this.listQuery).then((res) => {
        console.log(res)
        this.carouselList = res.data.data.records
        this.total = res.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.loading = false
        console.log('get list error')
      })
    },
    clearListQuery() {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = 20
      this.listQuery.billId = undefined
      this.listQuery.billName = undefined
      this.listQuery.billExplain = undefined
      this.getList()
    },
    getInfo(billId, billStatus) {
      if (billStatus === '2') {
        this.$router.push({ name: 'bill-edi', query: { id: billId }})
      } else {
        this.$router.push({ name: 'bill-info', query: { id: billId }})
      }
    },
    addCarousel() {
      this.$router.push({ name: 'carousel-edit' })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.listQuery.pageNum = val
      this.getList()
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    handleUpdate(row) {
      this.setCurrent(row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      this.getEditCityList()
      this.getEditDistrictList()
    },
    handleLook(id) {
      this.$router.push({ name: 'transaction-info', params: { id: id }})
    },
    handleCreate() {
      this.resetDetail()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    padDate(value) {
      value = value < 10 ? '0' + value : value
      return value
    },
    formatDate(val) {
      if (val === null) {
        return ''
      }
      var value = new Date(val)
      var year = value.getFullYear()
      var month = this.padDate(value.getMonth() + 1)
      var day = this.padDate(value.getDate())
      var hour = this.padDate(value.getHours())
      var minutes = this.padDate(value.getMinutes())
      var seconds = this.padDate(value.getSeconds())
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    deleteCarousel() { // 删除
      if (this.ids.length <= 0) {
        this.$message.error('请至少选择一项')
        return
      }
      this.$store.dispatch('carousel/deleteCarousel', this.ids).then(res => {
        const resCode = res.data.code
        if (resCode === '0') {
          this.$message.success('删除成功')
          this.getList()
          return
        }
        this.$message.error(res.data.message)
      }).catch(() => {
        console.log('DeleteBillBeach error')
      })
    },
    handleSelectionChange(val) { // 选择
      console.log(val)
      this.ids = []

      val.map((item, index) => {
        console.log(item, index)
        this.ids.push(item.id)
      })
    }
  }
}
</script>

<style scoped>
.root{
  display: flex;
  flex-flow: column;
}
.form-listQuery{
  margin-top: 20px;
  margin-left: 30px;
}
.operation{
  display: flex;
  justify-content: space-between;
  margin-left: 20px;
  margin-right: 20px;
}
.block{
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
