<template>
  <a-card :bordered="false" class="unRecyclableRubbishDetails-container">
    <!-- 搜索条件 -->
    <div class="unRecyclableRubbishDetails-container-searchForm">
      <a-form-model
        ref="queryParam"
        :model="queryParam"
        layout="inline"
        @keyup.enter.native="$refs.table.refresh(true)"
      >
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="查询时间区间" :label-col="{ span:8 }" :wrapper-col="{ span:16}">
              <a-range-picker
                id="unRecyclableRubbishDetails-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="{ span:8}" :wrapper-col="{ span:16}">
              <a-input
                id="unRecyclableRubbishDetails-queryWord"
                allowClear
                :maxLength="30"
                v-model="queryParam.queryWord"
                placeholder="请输入姓名/手机号码"
                autocomplete="off"/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="网点名称" :label-col="{ span:8}" :wrapper-col="{ span:16}">
              <a-select
                style="max-height: 50px;overflow: auto;margin-top: 3px;"
                id="unRecyclableRubbishDetails-stationNo"
                placeholder="请选择网点名称"
                allowClear
                v-model="queryParam.stationNo"
                :showSearch="true"
                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-button type="primary" @click="$refs.table.refresh(true)" id="unRecyclableRubbishDetails-refresh" style="margin-top: 4px">查询</a-button>
            <a-button type="" @click="reset" id="unRecyclableRubbishDetails-reset" style="margin: 4px 0 0 10px">重置</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> {{ orderTotal }} </span>条，<span> {{ total }} </span>kg。
        其中，可回收物总计<span>{{ huishouTotal }} </span>kg，有害垃圾总计<span>{{ harmTotal }} </span>kg，厨余垃圾总计<span>{{ chuyuTotal }} </span>kg，其他垃圾总计<span> {{ otherTotal }} </span>kg，建筑垃圾总计<span> {{ jianzhuTotal }} </span>kg
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table ref="table" :rowKey="record => record.id" :columns="columns" :data="loadData" bordered>
      <!-- 图片 -->
      <template slot="img" slot-scope="text,record">
        <viewer :images="record.imageUrlList? record.imageUrlList:[]" class="viewer" ref="viewer" @inited="inited" rebuild>
          <img v-for="src in record.imageUrlList" :src="src" :key="src" class="image">
        </viewer>
        <P style="margin-bottom: 0;">{{ record.imageUrlList ? record.imageUrlList.length+' 张' :'0 张' }}</P>
      </template>
    </s-table>
    </s-table>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { stationList } from '@/api/releaseRecord.js'
import { getRubbishDetailList, getRubbishDetailTotal } from '@/api/reportForm.js'
import moment from 'moment'
import getDate from '@/libs/getDate.js'
export default {
  components: {
    STable,
    VSelect
  },
  data () {
    return {
      orderTotal: 0, //  总计条数
      total: 0, //  可回收垃圾总计
      huishouTotal: 0, // 可回收物
      harmTotal: 0, // 有害垃圾
      otherTotal: 0, //  其他垃圾总计
      jianzhuTotal: 0, //  建筑垃圾总计
      chuyuTotal: 0, //  厨余垃圾总计
      queryParam: {
        stationNo: undefined, // 网点名称
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        queryWord: ''
      },
      optionData: [], // 网点列表数据
      // 将默认当天时间日期回显在时间选择框中
      time: [
        moment(getDate.getRecentday().starttime, 'YYYY-MM-DD'),
        moment(getDate.getRecentday().endtime, 'YYYY-MM-DD')
      ],
      dateFormat: 'YYYY-MM-DD', // 日期格式
      columns: [
        { title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 150, align: 'center' },
        { title: '网点名称', dataIndex: 'stationName', width: 120, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '采集员姓名', dataIndex: 'gatherName', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '采集员手机', dataIndex: 'gatherPhone', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '可回收物（kg）', dataIndex: 'gatherRecycling', width: 80, align: 'center', customRender: (text) => { return text ? (text / 1000).toFixed(3) / 1 : '0' } },
        { title: '有害垃圾（kg）', dataIndex: 'gatherHarm', width: 80, align: 'center', customRender: (text) => { return text ? (text / 1000).toFixed(3) / 1 : '0' } },
        { title: '厨余垃圾（kg）', dataIndex: 'gatherKitchen', width: 80, align: 'center', customRender: (text) => { return text ? (text / 1000).toFixed(3) / 1 : '0' } },
        { title: '其他垃圾（kg）', dataIndex: 'gatherOther', width: 80, align: 'center', customRender: (text) => { return text ? (text / 1000).toFixed(3) / 1 : '0' } },
        { title: '建筑垃圾（kg）', dataIndex: 'gatherBuilding', width: 80, align: 'center', customRender: (text) => { return text ? (text / 1000).toFixed(3) / 1 : '0' } },
        { title: '总计（kg）', dataIndex: 'gatherTotal', width: 80, align: 'center', customRender: (text) => { return text ? (text / 1000).toFixed(3) / 1 : '0' } },
        { title: '备注', dataIndex: 'remarks', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '图片', width: 100, align: 'center', scopedSlots: { customRender: 'img' } }
      ],
      // 图片
      images: [],
      // 加载数据方法 必须为 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 getRubbishDetailList(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
            }
            this.orderTotal = res.data.count || 0
            this.getTotal()
            return res.data
          } else {
            this.orderTotal = 0
          }
        })
      }
    }
  },
  mounted () {
    this.getStationList()
  },
  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
      )
    },
    // 获取网点数据
    getStationList () {
      stationList().then(res => {
        if (res.status == 200) {
          this.optionData = res.data || []
        } else {
          this.optionData = []
        }
      })
    },
    // 总计
    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
      }
      getRubbishDetailTotal(params).then(res => {
        if (res.status == 200) {
          if (res.data) {
            this.otherTotal = res.data.GATHER_OTHER ? (res.data.GATHER_OTHER / 1000).toFixed(3) / 1 : '0'
            this.jianzhuTotal = res.data.GATHER_BUILDING ? (res.data.GATHER_BUILDING / 1000).toFixed(3) / 1 : '0'
            this.chuyuTotal = res.data.GATHER_KITCHEN ? (res.data.GATHER_KITCHEN / 1000).toFixed(3) / 1 : '0'
            this.total = res.data.GATHER_TOTAL ? (res.data.GATHER_TOTAL / 1000).toFixed(3) / 1 : '0'
            this.harmTotal = res.data.GATHER_HARM ? (res.data.GATHER_HARM / 1000).toFixed(3) / 1 : '0'
            this.huishouTotal = res.data.GATHER_RECYCLING ? (res.data.GATHER_RECYCLING / 1000).toFixed(3) / 1 : '0'
          }
        }
      })
    },
    // 重置
    reset () {
      this.queryParam.stationNo = undefined
      this.queryParam.beginDate = null
      this.queryParam.endDate = null
      this.queryParam.queryWord = ''
      this.time = [
        moment(getDate.getRecentday().starttime, 'YYYY-MM-DD'),
        moment(getDate.getRecentday().endtime, 'YYYY-MM-DD')
      ]
      this.$refs.table.refresh(true)
    },
    inited (viewer) {
      this.$viewer = viewer
    },
    isShow () {
      this.$viewer.update()
    }
  }
}
</script>

<style lang='less' scoped>
	.unRecyclableRubbishDetails-container {
		.unRecyclableRubbishDetails-container-searchForm {
			.ant-form-inline .ant-form-item {
				width: 100%;
				margin-bottom: 15px;
			}
		}
    .image {
    width:25px;
    height: 25px;
    cursor: pointer;
    margin: 5px;
    display: inline-block;
  }
  .image:not(:first-child){
    display: none;
  }
	}
</style>
