<template>
  <a-modal
    title="清运详情"
    :footer="null"
    v-model="isShow"
    @cancel="isShow=false"
    :centered="true"
    width="60%">
    <s-table
      ref="table"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered
      :scroll="{ y: 400 }">
    </s-table>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { networkDetail } from '@/api/cleanManage.js'
import moment from 'moment'
export default {
  name: 'CleanDetailModal',
  components: {
    STable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    stationNo: {
      type: [Number, String],
      default: ''
    },
    searchDate: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      isShow: this.visible, // 弹窗的状态
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '清运时间', dataIndex: 'cleanTime', width: 100, align: 'center' },
        { title: '设备编号', dataIndex: 'srcDeviceCode', width: 100, align: 'center' },
        { title: '清运重量(kg)', dataIndex: 'cleanWeight', width: 100, align: 'center' },
        { title: '清运司机', dataIndex: 'driverName', width: 100, align: 'center' }],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const searchDateModal = this.searchDate
        if (this.searchDate && this.searchDate.length) {
          searchDateModal.beginDate = moment(this.searchDate[0]).format('YYYY-MM-DD 00:00:00')
          searchDateModal.endDate = moment(this.searchDate[1]).format('YYYY-MM-DD 23:59:59')
        } else {
          searchDateModal.beginDate = null
          searchDateModal.endDate = null
        }
        const searchData = Object.assign(parameter, { stationNo: this.stationNo }, { beginDate: searchDateModal.beginDate }, { endDate: searchDateModal.endDate })
        return networkDetail(searchData).then(res => {
          if (res.status == 200) {
            const no = (res.data.pageNo - 1) * res.data.pageSize
            for (let i = 0; i < res.data.list.length; i++) {
              const _item = res.data.list[i]
              _item.no = no + i + 1
              if (_item.cleanWeight != null || 0) {
                _item.cleanWeight = (_item.cleanWeight / 1000).toFixed(3)/1
              }
            }
            return res.data
          }
        })
      }
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isShow = newValue
    },
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style lang="less" scoped="scoped">
	.detail-item {
		text-align: center;
		margin: 10px 0;
		letter-spacing: 0.5px;
	}
</style>
