<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane label="工位PDF" name="station">
          <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-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-tab-pane>

        <el-tab-pane label="整线PDF" name="line">
          <div class="topDiv">
            <el-button type="primary" plain size="small" icon="el-icon-order" @click="getLineDirectFileList">查询</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="lineDirectFileList"
              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="lineCheckDirectFile(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="deleteLineDirectFileSubmit(scope2.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </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>
  </div>
</template>

<script>
import { url } from '@/utils/request'
export default {
  data() {
    return {
      activeTab: 'station',
      imgDialogFormVisible: false,
      imgDialogObj: '',
      imgDialogOpName: '',
      imgCheckDialogFormVisible: false,
      stationNumber: [],
      stationName: '',
      limit: 1,
      DataPDF: {},
      uploadPDF: url,
      imgCheckDirectFileList: [],
      lineDirectFileList: [],
      dialogFormVisible: false
    }
  },
  created() {
    this.getStationNumber()
  },
  methods: {
    handleTabClick() {
      if (this.activeTab === 'line') {
        this.getLineDirectFileList()
      } else if (this.imgDialogOpName) {
        this.getImgCheckDirectFileList()
      }
    },
    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].工序名称
          })
        }
      })
    },
    handelLimitStyle() {
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0] && 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] && obj[0].style.display === '') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'none'
        }
      }
    },
    uploadSuccessPDF() {
      this.dialogFormVisible = false
      this.$refs.uploadPDF.clearFiles()
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0] && obj[0].style.display === 'none') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'block'
        }
      }
      this.$message.success('指导文件上传成功!')
      if (this.activeTab === 'line') {
        this.getLineDirectFileList()
      } else {
        this.getImgCheckDirectFileList()
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisiblePictureCard = true
    },
    beforeUploadPdf(file) {
      let isPDF = /(\.|\/)(pdf)$/i.test(file.type) || /\.pdf$/i.test(file.name)
      if (!isPDF) {
        this.$message.error('只能上传PDF')
        return false
      }
      if (this.activeTab === 'station' && !this.imgDialogOpName) {
        this.$message.error('请选择工位号')
        return false
      }
      return true
    },
    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 => {
        if (response.data.length > 0) {
          this.pdfSrc = this.solvePdfUr1(response.data[0].指导文件)
          window.open(this.pdfSrc, '_blank')
        } else {
          this.$message.error('该工位此点检项目无指导文件，请设置后查看')
        }
      })
    },
    lineCheckDirectFile(row) {
      var param = []
      param[0] = ['ID', row.ID]
      var Data = this.CreateData('11', '基础数据_操作指南整线指导文件_查看文件', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) {
          this.pdfSrc = this.solvePdfUr1(response.data[0].指导文件)
          window.open(this.pdfSrc, '_blank')
        } else {
          this.$message.error('无整线指导文件，请设置后查看')
        }
      })
    },
    solvePdfUr1(content) {
      const bstr = atob(content)
      let n = bstr.length
      const u8arr = new Uint8Array(n)
      while (n--) { u8arr[n] = bstr.charCodeAt(n) }
      const blob = new Blob([u8arr], { type: 'application/pdf;chartset=UTF-8' })
      return window.URL.createObjectURL(blob)
    },
    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] && response.data[0].result === '1') {
            this.$message.success('删除指导文件成功！')
            this.getImgCheckDirectFileList()
          } else if (response.data) {
            this.$message.success('删除指导文件成功！')
            this.getImgCheckDirectFileList()
          } else {
            this.$message.error('删除指导文件失败！')
          }
        })
      }).catch(() => {})
    },
    deleteLineDirectFileSubmit(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] && response.data[0].result === '1') {
            this.$message.success('删除整线指导文件成功！')
            this.getLineDirectFileList()
          } else if (response.data) {
            this.$message.success('删除整线指导文件成功！')
            this.getLineDirectFileList()
          } else {
            this.$message.error('删除整线指导文件失败！')
          }
        })
      }).catch(() => {})
    },
    uploadShowPDF() {
      this.imgDialogFormVisible = true
    },
    submitPicturePDF() {
      var param = []
      var Data1
      if (this.activeTab === 'line') {
        param[0] = ['文件名称', '']
        param[1] = ['文件后缀', '']
        param[2] = ['文件内容', null]
        Data1 = this.CreateData('15', '基础数据_操作指南整线指导文件_保存文件', param)
      } else {
        if (!this.imgDialogOpName) {
          this.$message.error('请先选择工位号后提交!')
          return
        }
        param[0] = ['工位号', this.imgDialogOpName]
        param[1] = ['文件名称', '']
        param[2] = ['文件后缀', '']
        param[3] = ['文件内容', null]
        Data1 = this.CreateData('15', '基础数据_操作指南指导文件_保存文件', param)
      }
      this.DataPDF.param = Data1
      this.$refs.uploadPDF.submit()
      this.imgDialogFormVisible = false
      this.$message.success('指导文件上传成功!')
    },
    getImgCheckDirectFileList() {
      var param = []
      this.imgCheckDirectFileList = []
      param[0] = ['工位号', this.imgDialogOpName]
      var Data = this.CreateData('11', '基础数据_操作指南指导文件_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data && response.data.length > 0) {
          this.imgCheckDirectFileList = response.data.filter(function(item) {
            return item.工位号 !== '整线'
          })
          if (this.imgCheckDirectFileList.length === 0) {
            this.$message.error('该工位无此点检指导文件，请添加后查看')
          }
        } else {
          this.$message.error('该工位无此点检指导文件，请添加后查看')
        }
      })
    },
    getLineDirectFileList() {
      this.lineDirectFileList = []
      var param = []
      var Data = this.CreateData('11', '基础数据_操作指南整线指导文件_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data && response.data.length > 0) {
          this.lineDirectFileList = 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;
 }
}
.el-upload-dragger{
  width: 210px;
}
</style>
