<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
                :disabledDate="disabledDate"
                v-model="time"
                id="cleanDetail-time"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </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-select
                id="cleanDetail-stationNo"
                placeholder="请选择网点名称"
                allowClear
                v-model="queryParam.stationNo"
                showSearch
                option-filter-prop="children"
                :filter-option="filterOption">
                <a-select-option v-for="item in optionData" :key="item.stationNo" :value="item.stationNo">{{ item.name }}</a-select-option>
              </a-select>
            </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-select
                style="max-height: 50px;overflow: auto;margin-top: 3px;"
                id="cleanDetail-labelNoList"
                placeholder="请选择网点标签"
                mode="multiple"
                allowClear
                v-model="queryParam.labelNoList"
                :showSearch="true"
                option-filter-prop="children"
                :filter-option="filterOption"
              >
                <a-select-option v-for="item in lableData" :key="item.labelNo" :value="item.labelNo">{{ item.labelName }}</a-select-option>
              </a-select>
            </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 placeholder="请输入设备编号" v-model.trim="queryParam.srcDeviceCode" id="cleanDetail-srcDeviceCode" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="4">
            <a-button type="primary" @click="refresh" style="margin: 4px 10px 0 20px" id="cleanDetail-refresh">查询</a-button>
            <a-button type="" @click="reset" style="margin-top: 4px" id="cleanDetail-reset">重置</a-button>
          </a-col>
        </a-row>
        <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-select
                id="cleanDetail-dirverUserId"
                placeholder="请选择清运司机"
                allowClear
                v-model="queryParam.dirverUserId"
                showSearch
                option-filter-prop="children"
                :filter-option="filterOption">
                <a-select-option v-for="item in cleanUserData" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </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> {{ cleanWeightTotal }} </span>kg</div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      ref="table"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered
    >
      <span slot="action" slot-scope="text,record">
        <a-button type="primary" @click="handleDetail(record)" v-hasPermission="'B_cleanDetail'" id="cleanDetail-handleDetail">查看清运明细</a-button>
        <span v-if="!$hasPermissions('B_cleanDetail')">--</span>
      </span>
    </s-table>
    <!-- 详情弹窗 -->
    <cleanDetailModal :deviceNo="itemDeviceNo" :cleanTime="itemCleanTime" :visible="isOpenModal" @close="isOpenModal=false"></cleanDetailModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { stationList } from '@/api/releaseRecord.js'
import cleanDetailModal from './cleanDetailModal.vue'
import { cleanDetailsList, getCleanDetailsTotal, cleanUserList } from '@/api/cleanManage.js'
import { lableSeleteList } from '@/api/labelSetting.js'
import moment from 'moment'
export default {
  components: { STable, VSelect, cleanDetailModal },
  data () {
    return {
      formItemLayout: {
        labelCol: {
          span: 7
        },
        wrapperCol: {
          span: 17
        }
      },
      isOpenModal: false, // 默认弹窗关闭
      itemDeviceNo: '', // 每行设备编号
      itemCleanTime: '', // 每行清运时间
      // 查询参数
      queryParam: {
        dirverUserId: undefined, // 操作人id
        stationNo: undefined, // 网点编号
        srcDeviceCode: null, // 设备编号
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        labelNoList: [] // 网点标签
      },
      optionData: [], // 网点名称数据
      lableData: [], // 网点标签数据
      cleanUserData: [], // 操作人数据
      // 将默认当天时间日期回显在时间选择框中
      time: [],
      dateFormat: 'YYYY-MM-DD', // 日期格式
      totalNum: 0, // 总条数
      cleanWeightTotal: 0, // 可回收物清运量总计
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '清运时间', dataIndex: 'cleanTime', width: 100, align: 'center' },
        { title: '网点名称', dataIndex: 'stationName', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '网点标签', dataIndex: 'labelName', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '设备编号', dataIndex: 'srcDeviceCode', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '可回收物清运量(kg)', dataIndex: 'cleanWeight', width: 100, align: 'center', sorter: true, customRender: (text) => { return text || 0 } },
        { title: '清运司机', dataIndex: 'driverName', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '操作人', dataIndex: 'operatorName', width: 100, align: 'center' },
        { title: '备注', dataIndex: 'remarks', width: 100, align: 'center', ellipsis: true, customRender: (text) => { return text || '--' } },
        { title: '操作', dataIndex: 'action', width: 100, align: 'center', scopedSlots: { customRender: 'action' } }],
      // 加载数据方法 必须为 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 cleanDetailsList(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.getStationList()
    this.getTotal()
    this.getCleanUserList()
    this.getlableSeleteList()
  },
  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
      )
    },
    // 获取标签数据
    getlableSeleteList () {
	  lableSeleteList().then(res => {
	    if (res.status == 200) {
	      this.lableData = res.data || []
	    }
	  })
    },
    // 获取操作人数据
    getCleanUserList () {
      cleanUserList().then(res => {
        if (res.status == 200) {
          this.cleanUserData = res.data || []
        }
      })
    },
    // 获取合作商数据
    getStationList () {
      stationList().then(res => {
        if (res.status == 200) {
          this.optionData = res.data || []
        }
      })
    },
    // 获取网点标签数据
    getLableList () {
      stationList().then(res => {
        if (res.status == 200) {
          this.lableData = res.data || []
        }
      })
    },
    // 合计
    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
      }
      getCleanDetailsTotal(params).then(res => {
        if (res.status == 200) {
          if (res.data != 0 || null) {
            this.cleanWeightTotal = (res.data.cleanWeight / 1000).toFixed(3)/1
          }
        } else {
          this.cleanWeightTotal = 0
        }
      })
    },
    // 查询
    refresh () {
      this.$refs.table.refresh(true)
      this.getTotal()
    },
    // 重置
    reset () {
      this.queryParam = {
        dirverUserId: undefined, // 操作人id
        stationNo: undefined, // 网点编号
        srcDeviceCode: null, // 设备编号
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        labelNoList: []
      }
      this.time = []
      this.refresh()
    },
    // 查看清运详情
    handleDetail (record) {
      this.itemDeviceNo = record.deviceNo
      this.itemCleanTime = record.cleanTime
      this.isOpenModal = true
    }
  }
}
</script>
<style lang='less' scoped>
	.cleanDetail-container {
		.cleanDetail-container-searchForm {
			.ant-form-inline .ant-form-item {
				width: 100%;
				margin-bottom: 15px;
			}
		}
	}
</style>
