<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" >
            <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">
            <a-form-model-item label="单号" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input allowClear v-model.trim="queryParam.orderReserveNo" placeholder="请输入单号" @pressEnter="$refs.table.refresh(true)"/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" >
            <a-form-model-item label="联系人信息" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input allowClear v-model.trim="queryParam.userPhoneOrName" placeholder="请输入名称或电话" @pressEnter="$refs.table.refresh(true)"/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" >
            <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-row>
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12" :md="6" >
            <a-form-model-item label="状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select code="ORDER_RESERVE_STATUS" v-model="queryParam.orderStatus" allowClear placeholder="请选择状态" id="userList-check-enable-state"></v-select>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" >
            <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="contact" slot-scope="text,record">
        <div>{{ record.contactName }}</div>
        <div>{{ record.contactMobile }}</div>
        <div v-if="!record.contactName && !record.contactMobile">--</div>
      </template>

      <template slot="rider" slot-scope="text,record">
        <div>{{ record.name }}</div>
        <div>{{ record.phone }}</div>
        <div v-if="!record.name && !record.phone">--</div>
      </template>
      <!-- 查看详情 -->
      <template slot="detail" slot-scope="text, record">
        <a-icon
          type="eye"
          title="详情"
          v-if="$hasPermissions('B_riderOrderRecord_detail')"
          class="actionBtn icon-blues"
          @click="handelDetail(record)" />
        <span v-else>--</span>
      </template>
    </s-table>
    <!-- 详情弹窗 -->
    <riderOrderModal :visible="isOpenModal" :itemOrderNo="recordOrderNo" @close="isOpenModal=false"></riderOrderModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getOrderList, getOrderTotal } from '@/api/recoveryManage.js'
import moment from 'moment'
import riderOrderModal from './riderOrderModal.vue'
export default {
  components: { STable, VSelect, riderOrderModal },
  data () {
    return {
      formItemLayout: {
        labelCol: {
          span: 7
        },
        wrapperCol: {
          span: 17
        }
      },
      // 查询参数
      queryParam: {
        orderReserveNo: '', // 骑手信息
        orderStatus: '', // 骑手信息
        phoneOrName: '', // 骑手信息
        userPhoneOrName: ''
      },
      totalNum: 0, // 总条数
      weightTotal: 0, // 回收物总计
      moneyTotal: 0, // 金额总计
      isOpenModal: false,
      recordOrderNo: '', // 行订单编号
      // 将默认近一周时间日期回显在时间选择框中
      time: [],
      dateFormat: 'YYYY-MM-DD',
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '下单时间', dataIndex: 'createDate', width: 200, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '单号', dataIndex: 'orderReserveNo', width: 200, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '下单账号', dataIndex: 'mobile', width: 200, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '联系人信息', dataIndex: 'contact', width: 180, align: 'center', scopedSlots: { customRender: 'contact' } },
        { title: '骑手信息', dataIndex: 'rider', width: 200, align: 'center', scopedSlots: { customRender: 'rider' } },
        { title: '回收重量(kg)', dataIndex: 'totalWeight', width: 150, align: 'center', customRender: (text) => { return (text / 1000).toFixed(3) / 1 || 0 } },
        { title: '交易金额(元)', dataIndex: 'originalAmount', width: 150, align: 'center', customRender: (text) => { return text || 0 } },
        { title: '状态', dataIndex: 'orderStatusDictValue', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '详情', width: 80, align: 'center', scopedSlots: { customRender: 'detail' } }],
      // 加载数据方法 必须为 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 getOrderList(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.getTotal()
            return res.data
          }
        })
      }
    }
  },
  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
      }
      getOrderTotal(params).then(res => {
        if (res.status == 200) {
          if (res.data) {
            this.totalNum = res.data.orderCount || 0 // 总条数
            this.weightTotal = (res.data.totalAllWeight / 1000).toFixed(3) / 1 || 0// 回收物总计
            this.moneyTotal = res.data.originalAllAmount || 0 // 金额总计
          }
        }
      })
    },
    // 查看详情
    handelDetail (row) {
      this.recordOrderNo = row.orderReserveNo
      this.isOpenModal = true
    },
    // 查询
    refreshSearch () {
      this.$refs.table.refresh(true)
      this.getTotal()
    },
    // 重置
    reset () {
      this.queryParam = {
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        orderReserveNo: '', // 骑手信息
        orderStatus: '', // 骑手信息
        phoneOrName: '', // 骑手信息
        userPhoneOrName: ''
      }
      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>
