<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-input v-model.trim="listQuery.devcode" placeholder="设备编号" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-select v-model="listQuery.devtype" filterable placeholder="设备类型" clearable value="" >
              <el-option
                v-for="item in deviceTypeList"
                :key="item.productName"
                :label="item.productName"
                :value="item.productName"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-select v-model="listQuery.project" filterable placeholder="所属项目" clearable value="">
              <el-option
                v-for="item in projectList"
                :key="item.projectName"
                :label="item.projectName"
                :value="item.projectName"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-select v-model="listQuery.installPerson" filterable placeholder="负责人" clearable value="">
              <el-option
                v-for="item in principalList"
                :key="item.name"
                :label="item.name"
                :value="item.name"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-date-picker
              v-model="timeRange"
              :picker-options="pickerOptions"
              type="daterange"
              value-format="yyyy-MM-dd"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"/>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="batchExport">导出日志列表</el-button>
          <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="batchExport1">导出设备安装基础信息</el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <div>
      <el-row class="table-title">
        <el-col :span="15"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <el-table-column :index="indexMethod" align="center" type="index" label="序号" width="55"/>
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span :class="column.class">{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.offset"
        :page-sizes="[10,20,30]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
    <!-- 导出弹窗 -->
    <ExportDialog :export-dialog-visible="exportDialogVisible" :is-exporting="isExporting" @startExport="startExport" @closeDialog="closeDialog"/>
  </div>
</template>

<script>

import { getLogList, batchExportDeviceAdd, batchExportLog, getDeviceList, batchExportDeviceImage } from '@/api/factory'
import { getPrincipalList } from '@/api/devicetype'
import { getProjectList } from '@/api/batch'
import DownloadTemplate from '@/components/DownloadTemplate/index'
import ExportDialog from './components/exportDialog.vue'
export default {
  name: 'ListQuality',
  components: { DownloadTemplate, ExportDialog },
  data() {
    return {
      fileList: [], // 批量导入
      timeRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      qualityfilename: 'repairsImp.xlsx',
      listQuery: {
        devcode: '',
        devtype: '',
        project: '',
        installPerson: '',
        begTime: '',
        endTime: '',
        offset: 1,
        limit: 10
      }, // 筛选条件
      deviceTypeList: null, // 设备类型列表
      projectList: null, // 项目列表
      principalList: null, // 负责人列表
      columns: [
        {
          text: '设备编号',
          value: 'devcode',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'devicetype',
          align: 'center'
        },
        {
          text: '井编号',
          value: 'wellcode',
          align: 'center'
        },
        {
          text: '井类型',
          value: 'welltype',
          align: 'center'
        },
        {
          text: '井深',
          value: 'welldepth',
          align: 'center'
        },
        {
          text: '井名称',
          value: 'wellname',
          align: 'center',
          width: 150
        },
        {
          text: '安装高度',
          value: 'installheigt',
          align: 'center'
        }, {
          text: '负责人',
          value: 'installperson',
          align: 'center'
        }, {
          text: '记录时间',
          value: 'installtime',
          align: 'center'
        }, {
          text: '所属项目',
          value: 'project',
          align: 'center'
        }, {
          text: '安装位置',
          value: 'position',
          align: 'center'
        }, {
          text: '描述',
          value: 'description',
          align: 'center',
          width: 250
        }, {
          text: '(gcj02)经度',
          value: 'longitude',
          align: 'center'
        }, {
          text: '(gcj02)纬度',
          value: 'latitude',
          align: 'center'
        }, {
          text: '(wgs84)经度',
          value: 'longitude84',
          align: 'center'
        }, {
          text: '(wgs84)纬度',
          value: 'latitude84',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false, // 是否显示编辑框
      dialogFormVisible: false, // 是否显示编辑框
      exportDialogVisible: false, // 导出弹窗显隐
      isExporting: false // 是都导出中
    }
  },
  computed: {
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.begTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.begTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    getDeviceList().then(response => {
      this.deviceTypeList = response.data
      console.log(this.deviceTypeList)
    })
    getProjectList().then(response => {
      this.projectList = response.data
    })
    getPrincipalList('repair').then(response => {
      this.principalList = response.data
    })
    this.fetchData()// 获取数据
    var lett = this
    document.onkeydown = function(e) {
      var key = window.event.keyCode
      if (key === 13) {
        lett.fetchData()
      }
    }
  },
  activated() {
    this.fetchData()// 获取数据
  },
  methods: {
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '下载中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportLog(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        console.log('download===', res)
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `日志列表.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      }).catch((res) => {
        loading.close()
        this.$message.error(res.message)
      })
    },
    // 点击开始导出
    startExport(includeImage) {
      if (includeImage === true) { // 包含图片
        this.isExporting = true
        batchExportDeviceImage(this.listQuery).then(res => {
          this.isExporting = false
          // this.$message.success(res.data)
          this.$message({
            message: `${res.data}`,
            type: 'success',
            duration: '4000'
          })
        }).catch((res) => {
          this.isExporting = false
          // this.$message.error(res.message)
        })
      } else { // 不包含图片
        // 全屏加载动画
        const loading = this.$loading({
          lock: true,
          text: '下载中,请稍后...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        batchExportDeviceAdd(this.listQuery).then(res => {
          const blob = new Blob([res.data])
          const downloadElement = document.createElement('a')
          loading.close()
          const href = window.URL.createObjectURL(blob) // 创建下载的链接
          downloadElement.href = href
          downloadElement.download = `设备安装基础信息列表.xls` // 下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() // 点击下载
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放blob对象
        }).catch((res) => {
          loading.close()
          this.$message.error(res.message)
        })
      }
    },
    // 点击导出设备安装基础信息
    batchExport1() {
      this.exportDialogVisible = true
      // 全屏加载动画
      // const loading = this.$loading({
      //   lock: true,
      //   text: '下载中,请稍后...',
      //   spinner: 'el-icon-loading',
      //   background: 'rgba(0, 0, 0, 0.7)'
      // })
      // batchExportDeviceAdd(this.listQuery).then(res => {
      //   loading.close() // 关闭加载动画
      //   console.log('download===', res)
      //   const blob = new Blob([res.data])
      //   const downloadElement = document.createElement('a')
      //   const href = window.URL.createObjectURL(blob) // 创建下载的链接
      //   downloadElement.href = href
      //   downloadElement.download = `设备安装基础信息列表.xlsx` // 下载后文件名
      //   document.body.appendChild(downloadElement)
      //   downloadElement.click() // 点击下载
      //   document.body.removeChild(downloadElement) // 下载完成移除元素
      //   window.URL.revokeObjectURL(href) // 释放blob对象
      // }).catch((res) => {
      //   loading.close()
      //   this.$message.error(res.message)
      // })
    },
    closeDialog() {
      this.exportDialogVisible = false
    },

    // 查询数据
    search() {
      console.log(this.listQuery)
      this.fetchData(false)
    },
    // 获取字典数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getLogList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        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
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .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
    }
  }
  .upload-btn{
    display: inline;
  }
</style>
