<template>
  <div>
    <el-card>
      <span>项目名称</span>
      <el-select v-model="projectValue" placeholder="请选择项目名称" filterable size="small">
        <el-option
          v-for="item in project"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>机床图号</span>
      <el-select v-model="machineValue" placeholder="请选择机床图号" filterable size="small">
        <el-option
          v-for="item in machine"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>组号</span>
      <el-select v-model="groupValue" placeholder="请选择组号" filterable size="small">
        <el-option
          v-for="item in group"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>

      <el-button icon="el-icon-search" type="success" size="small" @click="pageCurrentWB = 1;pageSizeWB = 10;pageCurrent = 1;pageSize = 10;getTableData()">查询</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        size="mini"
        style="width: 100%;max-height: 520px"
        height="520">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column
          label="机床图号"
          align="center"
          min-width="100px">
          <template slot-scope="scope">{{ scope.row.机床图号 }}</template>
        </el-table-column>
        <el-table-column
          label="零件图号"
          align="center"
          min-width="100px">
          <template slot-scope="scope">{{ scope.row.零件图号 }}</template>
        </el-table-column>
        <el-table-column
          label="零件名称"
          align="center"
          min-width="100px">
          <template slot-scope="scope">{{ scope.row.零件名称 }}</template>
        </el-table-column>
        <el-table-column
          label="组号"
          align="center"
          min-width="100px">
          <template slot-scope="scope">{{ scope.row.组号 }}</template>
        </el-table-column>
        <el-table-column
          label="材料"
          align="center">
          <template slot-scope="scope">{{ scope.row.材料 }}</template>
        </el-table-column>
        <el-table-column
          label="备注"
          align="center">
          <template slot-scope="scope">{{ scope.row.零件备注 }}</template>
        </el-table-column>
        <el-table-column
          label="零件类型"
          align="center">
          <template slot-scope="scope">{{ scope.row.零件类型名称 }}</template>
        </el-table-column>
        <el-table-column
          min-width="50px"
          label="零件图纸"
          align="center">
          <template slot-scope="scope">

            <el-button circle icon="el-icon-search" size="small" @click="getNum(scope.row)"/>
            <!--<el-button slot="reference" type="success" icon="el-icon-search" circle size="mini"></el-button>-->
            <!--<el-button type="success" icon="el-icon-search" circle size="mini"></el-button>-->
          </template>
        </el-table-column>

        <el-table-column
          min-width="80px"
          label="操作"
          align="center">
          <template slot-scope="scope">
            <!--<FileUpload ref="FileUpload" :num="scope.row.基本件流水号" table-name="机加工MES_零件图PDF表" @listenMsg ="listenMsgFromChid"/>-->
            <el-upload
              :disabled="disabled"
              :action="action"
              :on-success="uploadSuccess"
              :on-error="uploadFailure"
              :show-file-list="showFileList"
              :file-list="fileList"
              class="upload-demo"
              multiple>
              <el-button :disabled="disabled" size="mini" type="primary" round @click="getAction(scope.row)">点击上传</el-button>
            </el-upload>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChanges"
          @current-change="handleCurrentChanges"/>
      </div>

    </el-card>
    <el-dialog
      :show-close="close"
      :visible.sync="dialogVisible"
      width="15%">
      <div v-for="(file, key) in hadFile" :key="key">
        <div class="wrapper">
          <div class="content">
            <div class="action">
              <a v-show="isDeleteShow" class="delete" @click="deleteFileData(file.id)">删除</a>
            </div>
            <div v-if="file.suffix==='pdf'" class="icon"><svg-icon icon-class="pdf" /></div>
            <div v-else-if="file.suffix==='docx'||file.suffix === 'doc'" class="icon"><svg-icon icon-class="doc" /></div>
            <div v-else-if="file.suffix==='xlsx' || file.suffix === 'xls'" class="icon"><svg-icon icon-class="excel" /></div>
            <div v-else-if="file.suffix==='pptx' || file.suffix === 'ppt'" class="icon"><svg-icon icon-class="ppt" /></div>
            <div v-else-if="file.suffix==='jpg' || file.suffix === 'jpeg'|| file.suffix === 'png'" class="icon"><svg-icon icon-class="pic" /></div>
            <div v-else class="icon"><svg-icon icon-class="file" /></div>
            <a id="appUrl" :href="file.url" target="view_window"><div class="info">{{ file.name }}</div></a>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import { projectSelect, machineSelect, groupSelect, basicPartsData } from '@/api/TechnologyCenter/PartDrawingMaintenance'
import { MESUploadFile, MESDownloadFile } from '@/utils/request'
export default {
  data() {
    return {
      close: false,
      dialogVisible: false,
      partID: '',
      projectValue: '',
      project: [],
      machineValue: '',
      machine: [],
      groupValue: '',
      group: [],
      pageCurrent: 1,
      pageSize: 10,
      total: null,
      loading: false,
      tableData: [],
      fileList: [],
      showFileList: true,
      action: '',
      disabled: false,
      tableName: '机加工MES_零件图PDF表',
      num: '',
      hadFile: [],
      isDeleteShow: false,
      deleteShow: '1',
      operationType: '0'
    }
  },
  computed: {
    groupName() {
      for (let i = 0; i < this.group.length; i++) {
        if (this.group[i].value === this.groupValue) {
          return this.group[i].label
        }
      }
    }
  },
  watch: {
    project(val) {
      if (val.length > 0) {
        this.projectValue = val[0].value
      }
    },
    projectValue(val) {
      this.machineValue = ''
      this.machine = []
      this.groupValue = ''
      this.group = []
      if (val) {
        this.getMachineSelect(val)
      }
    },
    machine(val) {
      if (val.length > 0) {
        this.machineValue = val[0].value
      }
    },
    machineValue(val) {
      this.groupValue = ''
      this.group = []
      if (val) {
        this.getGroupSelect(val)
      }
    },
    group(val) {
      if (val.length > 0) {
        this.groupValue = val[0].value
      }
    },
    groupValue(val) {
      if (val) {
        this.pageCurrent = 1
        this.pageSize = 10
        this.getTableData()
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    getAction(row) {
      this.num = row.基本件流水号
      this.action = `${MESUploadFile}?&num=${this.num}&tableName=${this.tableName}`
    },
    getNum(row) {
      this.dialogVisible = true
      this.num = row.基本件流水号
      this.getFileList(this.num)
    },
    uploadSuccess(res, file, fileList) {
      if (res[0].result === '1') {
        const index = fileList.indexOf(file)
        fileList.splice(index, 1)
      } else {
        this.$message.error(`${file.name}文件上传失败，请检查上传文件是否正确！`)
      }
      if (fileList.length === 0) {
        this.$message.success('文件上传成功')
        // this.$refs.FileDown.getFileList(this.num)
      }
    },
    uploadFailure(a, file, fileList) {
      fileList.map(file => {
        this.$message.error(`${file.name}文件上传失败，请检查上传文件是否正确！`)
      })
    },
    fetchData() {
      this.getSelect(projectSelect, ['项目名称', '项目流水号'], 'project')
    },
    getMachineSelect(val) {
      this.getSelect(machineSelect, ['机床图号', '机床流水号'], 'machine', val)
    },
    getGroupSelect(val) {
      this.getSelect(groupSelect, ['组号', '组件流水号'], 'group', val)
    },
    getTableData() {
      const project_check = this.projectValue ? 1 : 0
      const machine_check = this.machineValue ? 1 : 0
      const group_check = this.groupValue ? 1 : 0
      this.loading = true
      basicPartsData(project_check, this.projectValue, machine_check, this.machineValue, group_check, this.groupValue, this.pageSize, this.pageCurrent).then(response => {
        this.loading = false
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.getTableData()
    },
    async getFileList(value) {
      this.hadFile = []
      this.deleteShow === '1' ? this.isDeleteShow = true : this.isDeleteShow = false
      if (value) {
        await request({
          url: '',
          method: 'post',
          data: {
            type: '3',
            name: `select * from ${this.tableName} where 是否启用 = 1 and num = ${value}`
          }
        }).then(data => {
          this.suffix = data.data
          if (data.data.length > 0) {
            if (this.operationType === '1') {
              data.data.map(item => {
                this.hadFile.push({
                  url: `${MESDownloadFile}?id=${item.uid}.${item.suffix}&name=${item.name}.${item.suffix}`,
                  name: `${item.name}.${item.suffix}`,
                  suffix: item.suffix,
                  id: item.uid
                })
              })
            } else if (this.operationType === '0') {
              const server = `${MESDownloadFile}`.split('/')
              data.data.map(item => {
                this.hadFile.push({
                  url: `http://${server[2]}/webpage/part/${item.uid}.${item.suffix}`,
                  name: `${item.name}.${item.suffix}`,
                  suffix: item.suffix,
                  id: item.uid
                })
              })
            }
          }
        })
      }
    },
    async deleteFileData(id) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: '',
          method: 'post',
          data: {
            type: '3',
            name: `delete from ${this.tableName} where uid = N'${id}'`
          }
        }).then(response => {
          if (response.data.length === 0) {
            this.$message.success('删除成功')
            this.getFileList(this.num)
          } else {
            this.$message.error('删除失败')
            this.getFileList(this.num)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
  .wrapper{
    width: 90%;
    height: 50px;
    overflow: hidden;
    border: 1px solid #b8c7d9;
    /*float: left;*/
    margin: 10px;
  }
  .content{
    position: relative;
    padding: 8px 8px 0 8px;
  }
  .action{
    position: absolute;
    top: 2px;
    right: 8px;
  }
  .delete{
    padding: 0 5px;
    border-radius: 3px;
    color: #0f84b0;
    font-size: 12px;
  }
  .delete:hover{
    background: -webkit-linear-gradient(top,#0ba9e3,#0099d3);
    color: #fff;
  }
  a{
    text-decoration: none;
  }
  .icon{
    width: 30px;
    height: 30px;
    display: inline-block;
    margin-right: 7px;
    margin-top: 2px;
    font-size: 26px;
  }
  .info{
    width:100px;
    height: 50px;
    position: absolute;
    left: 44px;
    line-height: 30px;
    display: inline-block;
    color: #999;
  }
</style>
