<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.keyword1" placeholder="所属项目关键字" clearable style="width:180px"/>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-select v-model="listQuery.keyword2" filterable placeholder="任务执行状态" clearable value="" style="width:153px">
              <el-option
                v-for="item in stateList"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-input v-model.trim="listQuery.keyword3" placeholder="关联合同编号关键字" clearable style="width:180px"/>
          </el-form-item>
          <!--<el-form-item class="selectForm-container-item" prop="keywords">-->
          <!--<el-select v-model="listQuery.keyword4" filterable placeholder="项目经理" clearable value="" style="width:153px">-->
          <!--<el-option-->
          <!--v-for="item in principalList"-->
          <!--:key="item.id"-->
          <!--:label="item.name"-->
          <!--:value="item.name"/>-->
          <!--</el-select>-->
          <!--</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" icon="el-icon-edit" @click="add">新增</el-button>
          <el-upload
            :limit="1"
            :show-file-list="false"
            :http-request="uploadFile"
            :file-list="fileList"
            class="upload-btn"
            action="string"
            accept=".xls,.xlsx">
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit">批量导入</el-button>
          </el-upload>
          <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="batchExport">导出任务列表</el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <div>
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
        <download-template :filename="factoryfilename" btnname="任务导入模板"/>
      </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="120">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <!--<el-button :disabled="checkStored(scope.row)" type="text" @click="check(scope.row)">生成设备</el-button>-->
            <el-button :disabled="scope.row.status === '1'" type="text" @click="check(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="[5,10,20]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <edit-task v-show="dialogFormVisible" ref="edittask" @watchChild="fetchData"/>
    <create-device v-show="dialogFormVisible1" ref="createdevice" @watchChild="fetchData"/>
  </div>
</template>

<script>

import editTask from '@/views/batchManager/editTask'
import createDevice from '@/views/batchManager/createDevice'
import { getTaskList, batchImportTask, batchExportTask, delTask } from '@/api/batch'
import DownloadTemplate from '@/components/DownloadTemplate/index'
import { getPrincipalList } from '@/api/devicetype'

export default {
  name: 'ListTask',
  components: { editTask, createDevice, DownloadTemplate },
  data() {
    return {
      principalList: null, // 项目经理列表
      fileList: [], // 批量导入
      stateList: [{
        value: 0,
        label: '生产中'
      }, {
        value: 1,
        label: '已发货'
      }, {
        value: 2,
        label: '待交付'
      }],
      factoryfilename: 'taskImp.xlsx',
      listQuery: {
        keyword1: '',
        keyword2: '',
        keyword3: '',
        // keyword4: '',
        offset: 1,
        limit: 5
      }, // 筛选条件
      columns: [
        {
          text: '项目名称',
          value: 'projectName',
          align: 'center'
        },
        {
          text: '项目简称',
          value: 'projectSimpleName',
          align: 'center'
        }, {
          text: '运营商',
          value: 'operator',
          align: 'center'
        }, {
          text: '卡类型',
          value: 'cardType',
          align: 'center'
        },
        {
          text: '执行状态',
          value: 'isSend',
          align: 'center',
          width: '80'
        },
        {
          text: '关联合同编号',
          value: 'contractNumber',
          align: 'center'
        },
        {
          text: '设备名称',
          value: 'devTypeName',
          align: 'center'
        },
        {
          text: '设备型号',
          value: 'devmodeId',
          align: 'center'
        },
        {
          text: '设备数量',
          value: 'devcount',
          align: 'center',
          width: '80'
        },
        {
          text: '交付时间',
          value: 'deliveryTime',
          align: 'center'
        },
        {
          text: '设备要求',
          value: 'demand',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false, // 是否显示编辑框
      dialogFormVisible: false, // 是否显示编辑框
      dialogFormVisible1: false // 是否显示编辑框
    }
  },
  computed: {
  },
  created() {
    this.fetchPrincipalList()
    this.fetchData()// 获取数据
    var lett = this
    document.onkeydown = function(e) {
      var key = window.event.keyCode
      if (key === 13) {
        lett.fetchData()
      }
    }
  },
  activated() {
    this.fetchData()// 获取数据
  },
  methods: {
    // 判断是否生成设备
    checkStored(row) {
      if (row.isstored === '1') {
        return true
      } else {
        return false
      }
    },
    fetchPrincipalList() {
      getPrincipalList('operator,producer').then(response => {
        this.principalList = response.data
      })
    },
    // 批量导入
    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)'
      })
      // 批量导入上传请求
      batchImportTask(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fileList = []
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(err => {
        loading.close()
        this.$message.error(err.message)
      })
      this.fileList = []
    },
    // 删除
    del(row) {
      this.$confirm(
        '确定要删除该任务吗？',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delTask(row.taskId).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '下载中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportTask(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)
      })
    },
    // 新增厂家
    add() {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.edittask.initDialog('create', this.dialogFormVisible)
    },
    // 编辑字典信息
    edit(row) {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.edittask.initDialog('update', this.dialogFormVisible, row)
    },
    // 查看详情
    check(row) {
      this.dialogFormVisible1 = true
      this.$refs.createdevice.initDialog('update', this.dialogFormVisible1, row)
      // this.$router.push({ path: '/contractcheck/' + row.id })
    },
    // check() {
    //   this.$router.push({ path: '/contractcheck' })
    // },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取字典数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getTaskList(this.listQuery).then(response => {
        this.list = response.data.rows
        console.log(this.list)
        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>
