<!-- 数据预录入列表 -->
<template>
  <div class="data-preentry">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item">
            <el-input v-model.trim="listQuery.wellCode" placeholder="井编号" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.wellType" filterable placeholder="井类型" clearable value="" >
              <el-option
                v-for="item in wellTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.projectId" filterable placeholder="所属项目" clearable value="" >
              <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.projectName"
                :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-input v-model.trim="listQuery.position" placeholder="位置" clearable/>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">查询</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-upload
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          class="upload-btn"
          action="string"
          accept=".xls,.xlsx">
          <el-button style=" float:right;margin:7px 3px;" size="small" icon="el-icon-edit">批量导入</el-button>
        </el-upload>
        <el-button style=" float:right;margin:7px 3px;" size="small" @click="add">新增数据</el-button>
      </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-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="text" @click="detail(scope.row)">详情</el-button>
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="del(scope.row)">删除</el-button>
          </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>
    <AddWellDialog v-show="dialogFormVisible" ref="addWellDialogRef" @watchChild="fetchData"/>
  </div>
</template>

<script>
import DownloadTemplate from '@/components/DownloadTemplate/index'
import AddWellDialog from '../components/AddWellDialog.vue'
import { getFactoryManagerList, delFactoryManagerList, importFactoryManagerList } from '@/api/factoryManager/dataPreentry'
import { getProjectListUnpageSegment } from '@/api/factoryManager/project'
export default {
  name: 'ListQuality',
  components: { DownloadTemplate, AddWellDialog },
  data() {
    return {
      fileList: [], // 批量导入
      qualityfilename: 'repairsImp.xlsx',
      projectList: [],
      wellTypeList: [
        {
          id: '1',
          name: '燃气井'
        },
        {
          id: '2',
          name: '液位井'
        }
      ],
      listQuery: {
        wellNo: '', // wellCode
        wellType: '', // 井类型
        position: '', // 位置
        projectId: '', // 所在项目
        offset: 1,
        limit: 10
      }, // 筛选条件
      columns: [
        {
          text: '井编号',
          value: 'wellCode',
          align: 'center'
        },
        {
          text: '井名称',
          value: 'wellName',
          align: 'center'
        },
        {
          text: '井类型',
          value: 'wellType',
          align: 'center'
        },
        {
          text: '位置',
          value: 'position',
          align: 'center'
        },
        {
          text: '所属项目',
          value: 'projectName',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      multipleSelection: [], // 多选
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false, // 是否显示编辑框
      dialogFormVisible: false, // 是否显示编辑框
      exportDialogVisible: false // 导出弹窗显隐
    }
  },
  created() {
    this.fetchProjectListUnpageSegment() // 获取项目列表
    this.fetchData(false)// 获取数据
  },
  activated() {
    this.fetchData(false)// 获取数据
  },
  methods: {
    closeDialog() {
      this.exportDialogVisible = false
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData(isNowPage = false) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getFactoryManagerList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 获取项目列表
    fetchProjectListUnpageSegment() {
      getProjectListUnpageSegment().then(response => {
        this.projectList = response.data
      })
    },
    // 制作表格序号
    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
    },
    // 点击新增
    add() {
      this.dialogFormVisible = true
      this.$refs.addWellDialogRef.initDialog('create', this.dialogFormVisible)
    },
    // 点击编辑
    edit(row) {
      this.dialogFormVisible = true
      this.$refs.addWellDialogRef.initDialog('update', this.dialogFormVisible, row)
    },
    // 点击详情
    detail(row) {
      this.dialogFormVisible = true
      this.$refs.addWellDialogRef.initDialog('detail', this.dialogFormVisible, row)
    },
    // 点击删除
    del(row) {
      delFactoryManagerList({ id: row.id }).then(res => {
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message.error('删除失败')
      })
    },
    // 批量导入
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 批量导入上传请求
      importFactoryManagerList(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$alert('已导入成功' + res.message, '', {
            confirmButtonText: '确定',
            type: 'success'
          })
          // this.$message.success('导入成功')
          this.fileList = []
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(err => {
        loading.close()
        this.$message.error(err.message)
      })
      this.fileList = []
    }
  }
}
</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>
<style rel="stylesheet/scss" lang="scss">
  .data-preentry {
    .el-upload {
      display: block;
    }
  }
</style>
