<template>
  <app-container>
    <search-area :need-clear="false" :need-search-more="false" type="default" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.carCode" size="small" placeholder="车牌号" clearable/>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.routeName" size="small" placeholder="路线" clearable/>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="timeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          size="small"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable/>
      </search-item>
    </search-area>
    <normal-table :data="list" :head="tableOption.head" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" @change="changePage">
      <template slot="btns">
        <el-button v-if="hasPerm('/sanitation/job/export/transfer')" size="small" class="edit_btn" @click="batchExport">批量导出</el-button>
      </template>
    </normal-table>
  </app-container>
</template>

<script>
import { getCarJobRecords, exportCarJobRecords } from '@/api/sanitation/car'
import { exportFile } from '@/utils/exportutils'

export default {
  name: 'JobRecords',
  data() {
    return {
      listQuery: {
        type: '', // waste垃圾转运，clean路面清扫
        carCode: '', // 车牌号
        routeName: '', // 路线描述
        startTime: '', // 开始日期
        endTime: '', // 结束日期
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '车牌号',
          value: 'carCode',
          align: 'center'
        },
        {
          text: '路线',
          value: 'routeName',
          align: 'center'
        },
        {
          text: '车辆使用人',
          value: 'userPerson',
          align: 'center'
        },
        {
          text: '车辆使用人电话',
          value: 'userPersonPhone',
          align: 'center'
        },
        {
          text: '开始时间',
          value: 'startTime',
          align: 'center'
        },
        {
          text: '结束时间',
          value: 'endTime',
          align: 'center'
        }
      ], // 显示列
      timeRange: [], // 时间范围
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      typeList: [],
      fileList: [],
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      editShow: false // 编辑页面是否显示
    }
  },
  created() {
    const path = this.$route.path
    if (path.includes('waste')) {
      this.listQuery.type = 'waste'
      this.fetchData()
    } else {
      this.listQuery.type = 'clean'
      this.fetchData()
    }
  },
  methods: {
    fetchData() {
      this.listLoading = true
      if (this.timeRange && this.timeRange.length > 0) {
        this.listQuery.startTime = this.timeRange[0]
        this.listQuery.endTime = this.timeRange[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      getCarJobRecords(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 点击详情
    goDetail(row) {
      this.editShow = true
      this.$refs.edittoilet.initDialog('detail', true, row)
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      exportCarJobRecords(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        const blob = new Blob([res.data])
        const fileName = this.listQuery.type + `-作业记录.xlsx` // 下载后文件名
        exportFile(blob, fileName)
      }).catch((res) => {
        loading.close()
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作， 若不需要显示重置按钮则不需要写
    clearInput() {
      this.listQuery.carCode = ''
      this.listQuery.routName = ''
      this.listQuery.startTime = ''
      this.listQuery.endTime = ''
      this.listQuery.offset = 1
      this.timeRange = []
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    .edit_btn{
      float:right;
      margin-left:5px;
    }
  }
</style>
