
<template>
  <div class="root">
    <el-form :inline="true" :model="listQuery" class="form-listQuery">
      <el-form-item label="收费组编号">
        <el-input v-model="listQuery.billId" placeholder="收费组编号" />
      </el-form-item>
      <el-form-item label="收费组名">
        <el-input v-model="listQuery.billName" placeholder="收费组名" />
      </el-form-item>
      <el-form-item label="收费组说明">
        <el-input v-model="listQuery.billExplain" 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="addBill">新增收费组</el-button>
        <el-button type="danger" @click="deleteBillBeach">删除</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="billList"
      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.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收费组名称" align="center">
        <template slot-scope="scope">
          <span style="color: #409EFF;cursor: pointer" @click="getInfo(scope.row.id, scope.row.billStatus)">{{ scope.row.billName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收费组说明" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.billExplain }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应缴户数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.payTotalNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已缴户数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.payedNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="未缴户数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.unpayedNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收费组状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.billStatus === '0'">正在收费</span>
          <span v-if="scope.row.billStatus === '1'">收费完成</span>
          <span v-if="scope.row.billStatus === '2'">草稿</span>
        </template>
      </el-table-column>
      <el-table-column label="收费组发布时间" align="center">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费截止时间" align="center">
        <template slot-scope="scope">{{ formatDate(scope.row.endDate) }}</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 { mapState, mapActions, mapMutations } from 'vuex'
import waves from '@/directive/waves' // Waves directive
import { parseTime } from '@/utils'

export default {
  computed: {
    ...mapState('bill', {
      billList: s => s.billList,
      total: s => s.total
    })
  },
  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
      },
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        billId: undefined,
        billExplain: undefined,
        billName: undefined
      },
      ids: [], // id集合
      tableKey: 0,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    ...mapMutations('bill', {
    }),
    ...mapActions('bill', [
      'FetchBillList'
    ]),
    setCurrent(item) {
      this.setDetail({ ...item })
    },
    getList() {
      this.listLoading = true
      this.FetchBillList(this.listQuery).then(() => {
        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 }})
      }
    },
    addBill() {
      this.$router.push({ name: 'bill-add' })
    },
    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]
        }
      }))
    },
    deleteBillBeach() { // 删除
      if (this.ids.length <= 0) {
        this.$message.error('请至少选择一项')
        return
      }
      this.$store.dispatch('bill/DeleteBillBeach', this.ids).then(res => {
        const resCode = res.code
        if (resCode === '0') {
          this.$message.success(res.message)
          this.getList()
          return
        }
        this.$message.error(res.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>
