<template>
  <a-card :bordered="false" class="cleanDetail-container">
    <!-- 搜索条件 -->
    <div class="cleanDetail-container-searchForm">
      <a-form-model
        ref="queryParam"
        :model="queryParam"
        layout="inline"
        @keyup.enter.native="refresh"
        v-bind="formItemLayout">
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="下单时间" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-range-picker
                id="netWorkCleanRecord-time"
                v-model="time"
                style="width: 100%;"
                :format="dateFormat"
                :placeholder="['开始时间','结束时间']"
                :disabledDate="disabledDate"
                @change="onChange" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="骑手信息" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input allowClear v-model.trim="queryParam.phoneOrName" placeholder="请输入名称或电话" @pressEnter="$refs.table.refresh(true)"/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-button type="primary" @click="refreshSearch" id="netWorkCleanRecord-refresh" style="margin: 4px 10px 0 20px">查询</a-button>
            <a-button type="" @click="reset" id="netWorkCleanRecord-reset" style="margin-top: 4px">重置</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <!-- 合计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div class="ftext" slot="message">总计<span> {{ totalNum }} </span>条，回收重量 <span> {{ weightTotal }} </span>kg，交易金额 <span>{{ moneyTotal }}</span>元</div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      ref="table"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered
    >
      <template slot="stationName" slot-scope="text,record">
        <div>{{ record.name }}</div>
        <div>{{ record.phone }}</div>
        <div v-if="!record.name && !record.phone">--</div>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getOrderTotalList, getOrderTotalNum } from '@/api/recoveryManage.js'
import moment from 'moment'
export default {
  components: { STable, VSelect },
  data () {
    return {
      formItemLayout: {
        labelCol: {
          span: 7
        },
        wrapperCol: {
          span: 17
        }
      },
      // 查询参数
      queryParam: {
        phoneOrName: '' // 骑手信息
      },
      totalNum: 0, // 总条数
      weightTotal: 0, // 回收物总计
      moneyTotal: 0, // 金额总计
      // 将默认近一周时间日期回显在时间选择框中
      time: [],
      dateFormat: 'YYYY-MM-DD',
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '骑手信息', dataIndex: 'stationName', width: 200, align: 'center', scopedSlots: { customRender: 'stationName' } },
        { title: '回收重量(kg)', dataIndex: 'totalAlWeight', width: 50, align: 'center', sorter: true, customRender: (text) => { return (text / 1000).toFixed(3) / 1 || 0 } },
        { title: '交易金额(元)', dataIndex: 'originalAlAmount', width: 100, align: 'center', sorter: true, customRender: (text) => { return text || 0 } }],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const searchData = Object.assign(parameter, this.queryParam)
        if (this.time && this.time.length) {
          searchData.beginDate = moment(this.time[0]).format('YYYY-MM-DD 00:00:00')
          searchData.endDate = moment(this.time[1]).format('YYYY-MM-DD 23:59:59')
        } else {
          searchData.beginDate = null
          searchData.endDate = null
        }
        return getOrderTotalList(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
              }
            }
            this.totalNum = res.data.count
            return res.data
          } else {
            this.totalNum = 0
          }
        })
      }
    }
  },
  mounted () {
    this.getTotal()
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 创建时间change
    onChange (dates, dateStrings) {
      if ((dates, dateStrings)) {
        this.queryParam.beginDate = dateStrings[0]
        this.queryParam.endDate = dateStrings[1]
      }
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    // 总计
    getTotal () {
      const params = this.queryParam
      if (this.time && this.time.length) {
        params.beginDate = moment(this.time[0]).format('YYYY-MM-DD 00:00:00')
        params.endDate = moment(this.time[1]).format('YYYY-MM-DD 23:59:59')
      } else {
        params.beginDate = null
        params.endDate = null
      }
      getOrderTotalNum(params).then(res => {
        if (res.status == 200) {
          if (res.data != 0 || null) {
            this.weightTotal = (res.data.totalAllWeight / 1000).toFixed(3) / 1 || 0
            this.moneyTotal = res.data.originalAllAmount || 0
          }
        } else {
          this.weightTotal = 0
        }
      })
    },
    // 查询
    refreshSearch () {
      this.$refs.table.refresh(true)
      this.getTotal()
    },
    // 重置
    reset () {
      this.queryParam = {
        phoneOrName: ''
      }
      this.time = []
      this.refreshSearch()
    }
  }
}
</script>

<style lang='less' scoped>
	.cleanDetail-container {
		.cleanDetail-container-searchForm {
			.ant-form-inline .ant-form-item {
				width: 100%;
				margin-bottom: 15px;
			}
		}
	}
</style>
