<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" size="small" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.keyword" size="small" placeholder="责任人" clearable/>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.bridgeId" placeholder="所属桥梁" size="small" clearable>
          <el-option
            v-for="item in bridgeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="timeRange"
          size="small"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </search-item>
    </search-area>
    <normal-table :data="list" :head="tableOption.head" :query="listQuery" :total="total" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" size="small" @change="changePage">
      <template slot="columns">
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align">-->
          <template slot-scope="scope">
            <span v-if="column.text == '所属桥梁 / 道路'">{{ scope.row[column.value]!==''? scope.row[column.value]:scope.row['roadName'] }}</span>
            <span v-else-if="column.text == '开始养护日期'">{{ scope.row[column.value].substr(0,10) }}</span>
            <span v-else>{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </template>
    </normal-table>
  </app-container>
</template>

<script>
import { getRecordList, exportRecords } from '@/api/system/order'
import { downloadFile } from '@/utils/downloadUtils'

export default {
  name: 'ListMaintainRecords',
  data() {
    return {
      listQuery: {
        keyword: '', // 关键字（责任人/养护名称）
        bridgeId: '', // 所属桥梁id
        startDate: '', // 养护开始日期
        endDate: '', // 养护结束日期
        offset: 1,
        limit: 20,
        sort: 'maintainTime',
        order: 'desc'
      }, // 筛选条件
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      timeRange: [],
      columns: [
        {
          text: '日期',
          value: 'maintainTimeStr',
          align: 'center',
          width: 160
        },
        {
          text: '所属桥梁/道路',
          value: 'bridgeName',
          align: 'center',
          width: 160
        },
        {
          text: '责任人',
          value: 'personName',
          align: 'center',
          width: 160
        },
        {
          text: '情况描述',
          value: 'content',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      bridgeList: [],
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false // 是否显示编辑框
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.startDate = val[0]
        this.listQuery.endDate = val[1]
      } else {
        this.listQuery.startDate = ''
        this.listQuery.endDate = ''
      }
    }
  },
  mounted() {
    this.bridgeList = this.baseConfig.bridges
    this.fetchData()// 获取数据
  },
  methods: {
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getRecordList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    add() {
      this.editShow = true
      this.$refs.editorder.initDialog('create', true, null)
    },
    detail(row) {
      this.editShow = true
      this.$refs.editorder.initDialog('detail', true, row)
    },
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '下载中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      exportRecords(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        downloadFile(res.data, '工单列表')
      }).catch((res) => {
        loading.close()
      })
    },
    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.$message.success('clearInput')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .el-table th.gutter{
    display: table-cell !important;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
  .form-container {
    .selectForm-container-item {
      width: 220px;
    }
  }

  .table-container {
    padding: 0px 10px;
  }
</style>
