<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="imgDialogOpName" placeholder="请选择工位号" size="small" style="width: 150px" @change="getImgCheckDirectFileList">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <!--<el-select v-model="imgDialogBigType" placeholder="请选择机型" size="small" style="width: 150px">-->
        <!--  <el-option-->
        <!--    label="5S"-->
        <!--    value="1"-->
        <!--    size="mini"/>-->
        <!--  <el-option-->
        <!--    label="6S"-->
        <!--    value="4"-->
        <!--    size="mini"/>-->
        <!--</el-select>-->
        <el-button type="primary" plain size="small" icon="el-icon-order" @click="getImgCheckDirectFileList">查询</el-button>
        <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="uploadShowPDF()">上传</el-button>
      </div>
      <div class="main">
        <el-table
          :data="imgCheckDirectFileList"
          size="small"
          style="width: 100%; height: 680px; font-size: 20px; color: #000; border: 1px solid #9e9b9b"
        >
          <el-table-column
            label="排序"
            type="index"
            align="center"
            width="100"/>
          <el-table-column prop="文件名称" label="文件名称" align="center" />
          <el-table-column prop="操作时间" label="导入时间" align="center" />
          <el-table-column label="查看文件" align="center" width="150">
            <template slot-scope="scope2">
              <el-button type="warning" @click="imgCheckDirectFile(scope2.row)" >打开指导文件</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150">
            <template slot-scope="scope2">
              <el-button type="warning" @click="deleteImgCheckDirectFileSubmit(scope2.row)" >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <!--上传文件-->
    <el-dialog v-el-drag-dialog :visible.sync="imgDialogFormVisible" title="上传指导文件" center style="height: 800px" width="460px">
      <el-upload
        id="invoiceScan"
        ref="uploadPDF"
        :on-remove="handelLimitStyle"
        :on-change="handelLimit"
        :multiple="true"
        :auto-upload="false"
        :on-success="uploadSuccessPDF"
        :on-preview="handlePictureCardPreview"
        :before-upload="beforeUploadPdf"
        :on-exceed="warningExceedPdf"
        :limit="limit"
        :data="DataPDF"
        :action="uploadPDF"
        style="margin-left: 30px"
        drag>
        <i class="el-icon-upload"/>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip" class="el-upload__tip">只能上传一个pdf文件，且不超过100mb</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="imgDialogFormVisible=false">取消</el-button>
        <el-button type="primary" style="width: 70px" @click="submitPicturePDF()">确定</el-button>
      </div>
    </el-dialog>
    <!--展示文件列表-->
    <el-dialog v-el-drag-dialog :visible.sync="imgCheckDialogFormVisible" title="查看指导文件" center style="height: 100%;overflow: auto" width="80%">
      <span style="margin-left: 10px">工位号：</span>
      <el-button type="text" size="mini" icon="el-icon-circle-check-outline" @click="getImgCheckDirectFileList">查看</el-button>
      <el-table
        :data="imgCheckDirectFileList"
        size="small"
        max-height="365px"
        style="width: 100%; height: 280px; font-size: 20px; color: #000; border: 1px solid #9e9b9b"
      >
        <el-table-column
          label="排序"
          type="index"
          align="center"
          width="100"/>
        <el-table-column prop="文件名称" label="文件名称" align="center" />
        <el-table-column prop="操作时间" label="导入时间" align="center" />
        <el-table-column label="查看文件" align="center" width="150">
          <template slot-scope="scope2">
            <el-button type="warning" @click="imgCheckDirectFile(scope2.row)" >打开指导文件</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="scope2">
            <el-button type="warning" @click="deleteImgCheckDirectFileSubmit(scope2.row)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="imgCheckDialogFormVisible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { url } from '@/utils/request'
import $ from 'jquery'
import { getTableData1, getTableData2, getTableData3, editType, confirmEdit1, confirmEdit2, selectCount, openDelete1, openDelete2, submit1, submit2, exportOut } from '@/api/ManufacturingCenter/maintainInformation'
import { MESUploadFileProject } from '@/utils/request'
export default {
  data() {
    return {
      imgDialogFormVisible: false,
      imgDialogObj: '', // 点检类型对象
      imgDialogOpName: '', // 点检指导文件选择的工位号
      // imgDialogBigType: '1', // 点检机型类型
      imgCheckDialogFormVisible: false, // 查看点检文件
      stationNumber: [],
      stationName: '',
      limit: 1,
      DataPDF: {},
      uploadPDF: url,
      imgCheckDirectFileList: [],
      dialogFormVisible: false
    }
  },
  computed: {
  },
  created() {
    this.getStationNumber()
  },
  methods: {
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号,
            labelName: response.data[i].工序名称
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 给样式的
    handelLimitStyle() {
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0].style.display === 'none') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'block'
        }
      }
    },
    handelLimit() {
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0].style.display === '') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'none'
        }
      }
    },
    // 上传成功回调
    uploadSuccessPDF(res) {
      this.dialogFormVisible = false
      this.$refs.uploadPDF.clearFiles()
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0].style.display === 'none') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'block'
        }
      }
      this.$message.success('指导文件上传成功!')
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisiblePictureCard = true
    },
    // 上传前检测
    beforeUploadPdf(file) {
      let isJPG = /(\.|\/)(pdf)$/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传PDF')
      }
      if (!this.imgDialogOpName) {
        this.$message.error('请选择工位号')
        isJPG = false
      }
      // if (!this.imgDialogBigType) {
      //   this.$message.error('请选择机型后提交')
      //   isJPG = false
      // }
      return isJPG
    },
    warningExceedPdf() {
      this.$message.error('仅可上传一份PDF指导文件')
    },
    // 预览指导文件按钮
    imgCheckDirectFile(row) {
      var param = []
      param[0] = ['ID', row.ID]
      var Data = this.CreateData('11', '基础数据_操作指南指导文件_查看文件', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response)
        if (response.data.length > 0) {
          this.pdfSrc = this.solvePdfUr1(response.data[0].指导文件)
          window.open(this.pdfSrc, '_blank')
          // this.imgCheckDialogFormVisible = true // 查看点检文件
          console.log(this.pdfSrc)
        } else {
          this.$message.error('该工位此点检项目无指导文件，请设置后查看')
        }
      })
    },
    // 处理pdf 二进制流
    solvePdfUr1(content, type, fileName) {
      const bstr = atob(content)
      let n = bstr.length
      const u8arr = new Uint8Array(n)
      while (n--) { u8arr[n] = bstr.charCodeAt(n) }
      // 确定解析格式，可能可以变成img，没有深入研究
      const blob = new Blob([u8arr], { type: 'application/pdf;chartset=UTF-8' })
      return window.URL.createObjectURL(blob)
    },
    callOff() {
      this.dialogFormVisible = false
      this.$refs['form'].resetFields()
    },
    // 删除指导文件
    deleteImgCheckDirectFileSubmit(row) {
      this.$confirm('此操作将永久删除该指导文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', '基础数据_操作指南指导文件_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除指导文件成功！')
            this.getImgCheckDirectFileList()
          } else {
            this.$message.error('删除指导文件失败！')
          }
        })
      }).catch(() => {
      })
    },
    // 添加指导文件
    uploadShowPDF(row) {
      this.imgDialogFormVisible = true
    },
    // 保存文件
    submitPicturePDF() {
      if (!this.imgDialogOpName) {
        this.$message.error('请先选择工位号后提交!')
        return
      }
      // if (!this.imgDialogBigType) {
      //   this.$message.error('请先选择机型后提交!')
      //   return
      // }
      var param = []
      param[0] = ['工位号', this.imgDialogOpName]
      // param[1] = ['机型', this.imgDialogBigType]
      param[1] = ['文件名称', '']
      param[2] = ['文件后缀', '']
      param[3] = ['文件内容', null]
      var Data1 = this.CreateData('15', '基础数据_操作指南指导文件_保存文件', param)
      this.DataPDF.param = Data1
      this.$refs.uploadPDF.submit()
      this.imgDialogFormVisible = false
      this.$message.success('指导文件上传成功!')
    },
    // 查询指导文件列表
    getImgCheckDirectFileList() {
      console.log('触发了查询')
      var param = []
      this.imgCheckDirectFileList = []
      param[0] = ['工位号', this.imgDialogOpName]
      // param[1] = ['机型', this.imgDialogBigType]
      var Data = this.CreateData('11', '基础数据_操作指南指导文件_查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response)
        if (response.data.length > 0) {
          this.imgCheckDirectFileList = response.data
        } else {
          this.$message.error('该工位无此点检指导文件，请添加后查看')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.NewlyAdded{
  background:rgb(253,246,236);
  border-color: #ff9759;
  color: #ff9759;
&:hover{
   background: #ff9759;
   border-color: #ff9759;
   color: white;
 }
&:focus{
   background: #ff9759;
   border-color: #ff9759;
   color: white;
 }
}
#dianjianbiao {
  /*.el-table td, .el-table th.is-leaf,.el-table--border, .el-table--group{*/
  /*border-color: black !important;*/
  /*}*/
  /*.el-table--border::after, .el-table--group::after, .el-table::before{*/
  /*background-color: black !important;*/
  /*}*/
  /*th {*/
  /*background: white !important;*/
  /*color: black !important;*/
  /*}*/
}
.el-upload-dragger{
  width: 210px;
//right: 30px;
}
</style>

