<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="true" type="seperate" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.routeName" size="small" placeholder="路线描述" clearable/>
      </search-item>
      <search-item>
        <el-input v-model="listQuery.startAddress" size="small" placeholder="起始地址" clearable/>
      </search-item>
      <search-item>
        <el-input v-model="listQuery.endAddress" size="small" placeholder="终点地址" clearable/>
      </search-item>
      <!--高级检索-->
      <div slot="searchMore">
        <search-item>
          <el-input v-model="listQuery.mainRoad" placeholder="主要途径道路" clearable/>
        </search-item>
        <search-item>
          <dept-select v-model="listQuery.deptId" :dept-show="deptShow" placeholder="申请单位" clearable value=""/>
        </search-item>
      </div>
    </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/route/waste/add')" size="small" class="edit_btn" @click.stop="add">新增</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="detail(scope.row)">详情</el-button>
            <el-button v-if="hasPerm('/sanitation/route/waste/update')" type="text" size="small" @click.stop="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/sanitation/route/delete/waste')" type="text" size="small" @click.stop="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
  </app-container>
</template>
<script>
import { getRouteInfoList, delRouteInfo } from '@/api/sanitation/route'
import DeptSelect from '@/components/DeptSelect'
export default {
  name: 'ListRoute',
  components: { DeptSelect },
  data() {
    return {
      deptShow: true,
      listQuery: {
        sys: '1',
        type: '',
        routeName: '',
        startAddress: '',
        endAddress: '',
        mainRoad: '',
        deptId: '',
        offset: 1,
        limit: 15
      }, // 查询条件
      columns: [
        {
          text: '路线描述',
          value: 'routeName',
          align: 'center'
        },
        {
          text: '起点地址',
          value: 'startAddress',
          align: 'center',
          width: 200
        },
        {
          text: '终点地址',
          value: 'endAddress',
          align: 'center',
          width: 200
        },
        {
          text: '主要途径道路',
          value: 'mainRoad',
          align: 'center'
        }
      ], // 动态加载的表头
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      deptlist: [], // 类型列表
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      } // 表格属性
    }
  },
  created() {
    const path = this.$route.path
    if (path.includes('route/waste')) {
      this.listQuery.type = 'waste'
      this.fetchData()
    } else {
      this.listQuery.type = 'clean'
      this.fetchData()
    }
  },
  methods: {
    add() {
      this.$router.push({ path: '/route/add/' + this.listQuery.type })
    },
    edit(row) {
      this.$router.push({
        path: '/route/edit/' + this.listQuery.type,
        query: {
          id: row.id,
          routeName: row.routeName,
          alarmType: row.alarmType,
          alarmDistance: row.alarmDistance,
          startAddress: row.startAddress,
          endAddress: row.endAddress,
          mainRoad: row.mainRoad,
          startDate: row.startDate,
          endDate: row.endDate,
          accessTime: row.accessTime,
          remarks: row.remarks
        }
      })
    },
    detail(row) {
      console.log('/route/detail/' + this.listQuery.type)
      this.$router.push({
        path: '/route/detail/' + this.listQuery.type,
        query: {
          id: row.id,
          routeName: row.routeName,
          alarmType: row.alarmType,
          alarmDistance: row.alarmDistance,
          startAddress: row.startAddress,
          endAddress: row.endAddress,
          mainRoad: row.mainRoad,
          startDate: row.startDate,
          endDate: row.endDate,
          accessTime: row.accessTime,
          remarks: row.remarks
        }
      })
    },
    del(row) {
      this.$confirm(
        '确定要删除' + row.routeName + '吗？',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delRouteInfo(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    search() {
      this.fetchData(false)
    },
    fetchData(isNowPage = true) {
      console.log('fetchData')
      this.listLoading = true
      if (!isNowPage) {
        this.listQuery.offset = 1
      }
      getRouteInfoList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        // 如果当前页码数据为空，重新请求末页
        if (this.listQuery.offset > Math.ceil(this.total / this.listQuery.limit)) {
          this.listQuery.offset = Math.ceil(this.total / this.listQuery.limit)
          getRouteInfoList(this.listQuery).then(response => {
            this.list = response.data.rows
            this.total = parseInt(response.data.total)
            this.listLoading = false
          })
        }
        this.listLoading = false
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    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.routeName = ''
      this.listQuery.startAddress = ''
      this.listQuery.endAddress = ''
      this.listQuery.mainRoad = ''
      this.listQuery.deptId = ''
      this.listQuery.offset = '1'
      this.listQuery.limit = '20'
    }
  }
}
</script>

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