<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-select
                id="netWorkCleanRecord-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="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> {{ 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)" id="netWorkCleanRecord-handleDetail">查看详情</a-button> -->
        <a-button type="primary" @click="handleDetail(record)" v-hasPermission="'B_netWorkCleanRecord_detail'" id="netWorkCleanRecord-handleDetail">查看详情</a-button>
        <span v-if="!$hasPermissions('B_netWorkCleanRecord_detail')">--</span>
      </span>
    </s-table>
    <!-- 详情弹窗 -->
    <netWorkCleanModal :stationNo="itemStationNo" :searchDate="time" :visible="isOpenModal" @close="isOpenModal=false"></netWorkCleanModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { stationList } from '@/api/releaseRecord.js'
import { cleanRecordList, getCleanStationTotal } from '@/api/cleanManage.js'
import netWorkCleanModal from './netWorkCleanModal.vue'
import moment from 'moment'
import getDate from '@/libs/getDate.js'
export default {
  components: { STable, VSelect, netWorkCleanModal },
  data () {
    return {
      formItemLayout: {
        labelCol: {
          span: 7
        },
        wrapperCol: {
          span: 17
        }
      },
      isOpenModal: false, // 默认弹窗关闭
      itemStationNo: '', // 每行网点编号
      // 查询参数
      queryParam: {
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        stationNo: undefined // 网点编号
      },
      totalNum: 0, // 总条数
      cleanWeightTotal: 0, // 回收物总计
      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: 'stationName', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '清运次数', dataIndex: 'cleanTimes', width: 100, align: 'center', sorter: true },
        { title: '清运重量(kg)', dataIndex: 'cleanWeight', width: 100, align: 'center', sorter: true, customRender: (text) => { return text || 0 } },
        { title: '最后清运时间', dataIndex: 'cleanTime', width: 100, align: 'center', 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 cleanRecordList(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(2)
              }
            }
            this.totalNum = res.data.count
            return res.data
          } else {
            this.totalNum = 0
          }
        })
      }
    }
  },
  mounted () {
    this.getStationList()
    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
      )
    },
    // 获取网点名称数据
    getStationList () {
      stationList().then(res => {
        if (res.status == 200) {
          this.optionData = 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
      }
      getCleanStationTotal(params).then(res => {
        if (res.status == 200) {
          if (res.data != 0 || null) {
            this.cleanWeightTotal = (res.data.cleanWeight / 1000).toFixed(2)
          }
        } else {
          this.cleanWeightTotal = 0
        }
      })
    },
    // 查询
    refreshSearch () {
      this.$refs.table.refresh(true)
      this.getTotal()
    },
    // 刷新列表
    // refreshTable () {
    //   if (this.itemStationNo && this.time) {
    //     this.$refs.table.refresh()
    //   } else {
    //     this.$refs.table.refresh(true)
    //   }
    // },
    // 重置
    reset () {
      this.queryParam = {
        stationNo: undefined,
        beginDate: null,
        endDate: null
      }
      this.time = [
        moment(getDate.getRecentday().starttime, 'YYYY-MM-DD 00:00:00'),
        moment(getDate.getRecentday().endtime, 'YYYY-MM-DD 23:59:59')
      ]
      this.refreshSearch()
    },
    // 查看清运详情
    handleDetail (record) {
      this.itemStationNo = record.stationNo
      this.searchDate = this.time
      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>
