<template>
  <div class="app-container">
    <el-card>
      <el-select v-model="machineValue" placeholder="机床图号" style="width: 150px" filterable size="small">
        <el-option
          v-for="item in machine"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-select v-model="groupValue" placeholder="组号" style="width: 150px;margin-top: 10px;margin-bottom: 5px" 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="primary" plain size="small" @click="pageCurrentWB = 1;pageSizeWB = 1000;pageCurrent = 1;pageSize = 1000;getTableData()">查询</el-button>
    </el-card>
    <el-card>
      <div v-for="(file, key) in hadFile" :key="key">
        <div class="wrapper">
          <div class="content">
            <div class="action"/>
            <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 :href="file.url" target="view_window"><div class="info">{{ file.name }}</div></a>
          </div>
        </div>
      </div>
    </el-card>
    <el-card>
      <h3>自制件</h3>
      <el-table
        v-loading="loading"
        :data="tableData"
        :row-class-name="tableRowClassName1"
        highlight-current-row
        border
        stripe
        size="mini"
        style="width: 100%;max-height: 520px"
        height="460">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column
          label="机床图号"
          align="center"
          prop="机床图号"
          min-width="150px">
          <template slot-scope="scope">{{ scope.row.机床图号 }}</template>
        </el-table-column>
        <el-table-column
          label="零件图号"
          align="center"
          prop="零件图号"
          min-width="200px">
          <template slot-scope="scope">{{ scope.row.零件图号 }}</template>
        </el-table-column>
        <el-table-column
          label="零件名称"
          align="center"
          prop="零件名称"
          min-width="140px"
          show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.零件名称 }}</template>
        </el-table-column>
        <el-table-column
          label="组号"
          align="center"
          prop="组号"
          min-width="80px">
          <template slot-scope="scope">{{ scope.row.组号 }}</template>
        </el-table-column>
        <el-table-column
          label="零件数量"
          width="80"
          align="center">
          <template slot-scope="scope">{{ scope.row.零件数量 }}</template>
        </el-table-column>
        <el-table-column label="部件数量" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.部件数量 }}
          </template>
        </el-table-column>
        <el-table-column label="机床数量" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.机床数量 }}
          </template>
        </el-table-column>
        <el-table-column
          label="材料"
          prop="材料"
          align="center"
          show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.材料 }}</template>
        </el-table-column>
        <el-table-column
          label="规格"
          prop="规格"
          align="center"
          show-overflow-tooltip>
          <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 align="center" label="分类码" width="150">
          <template slot-scope="scope">
            {{ scope.row.分类码 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="旧零件图号" width="150">
          <template slot-scope="scope">
            {{ scope.row.前零件图号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="整改类型" width="150">
          <template slot-scope="scope">
            {{ scope.row.整改类型 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="导入人" width="100">
          <template slot-scope="scope">
            {{ scope.row.导入人员 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="导入时间" width="100">
          <template slot-scope="scope">
            {{ scope.row.操作日期 | formatTime }}
          </template>
        </el-table-column>
        <!--<el-table-column label="操作" fixed="right" align="center" width="100">-->
        <!--<template slot-scope="scope">-->
        <!--<el-button-->
        <!--:disabled="Number(id)!==0"-->
        <!--size="mini"-->
        <!--type="text"-->
        <!--icon="el-icon-delete"-->
        <!--@click="handleDelete(scope.row)"/>-->
        <!--</template>-->
        <!--</el-table-column>-->
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total"
          @size-change="handleSizeChanges"
          @current-change="handleCurrentChanges"/>
      </div>
    </el-card>
    <el-card>
      <h3>外购件</h3>
      <el-table
        v-loading="loadingWB"
        :data="tableDataWB"
        style="width: 100%;min-height: 600px;"
        size="mini"
        stripe
        height="500"
        highlight-current-row
        border>
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column align="center" prop="物料名称" label="物料名称">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="物料规格" label="物料规格">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="物料型号" label="物料型号">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件数量">
          <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 align="center" prop="零件类型" label="零件类型">
          <template slot-scope="scope">
            {{ scope.row.零件类型 }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="供货商" label="供货商">
          <template slot-scope="scope">
            {{ scope.row.供货商 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" width="100">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="标记" width="100">
          <template slot-scope="scope">
            {{ scope.row.标记 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="分类码" width="150">
          <template slot-scope="scope">
            {{ scope.row.分类码 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="整改类型" width="150">
          <template slot-scope="scope">
            {{ scope.row.整改类型 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="导入人" width="100">
          <template slot-scope="scope">
            {{ scope.row.导入人员 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="导入时间" width="100">
          <template slot-scope="scope">
            {{ scope.row.操作日期 | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="150">
          <template slot-scope="scope">
            <el-button
              :disabled="Number(id)!==0"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px">
        <el-pagination
          :current-page="pageCurrentWB"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSizeWB"
          :total="totalWB"
          layout="total"
          @size-change="handleSizeChangeWB"
          @current-change="handleCurrentChangeWB"/>
      </div>
    </el-card>
  </div>
</template>

<script>
import { machineSelect, groupSelect, basicPartsData, deleteBasicParts, purchasePartsData, deletePurchaseData, getFileData } from '@/api/TechnologyCenter/QueryParts'
import { mapGetters } from 'vuex'
import { ServerIP } from '@/utils/request'
export default {
  name: 'Index',
  data() {
    return {
      machineValue: '',
      machine: [],
      groupValue: '',
      group: [],
      group1: [],
      loading: false,
      loadingWB: false,
      dialogFormVisible: false,
      total: 0,
      tableData: [],
      tableDataWB: [],
      pageCurrent: 1,
      pageSize: 1000,
      pageCurrentWB: 1,
      pageSizeWB: 1000,
      totalWB: 0,
      hadFile: []
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'id'// 人员编号
    ]),
    groupName() {
      for (let i = 0; i < this.group.length; i++) {
        if (this.group[i].value === this.groupValue) {
          return this.group[i].label
        }
      }
    }
  },
  watch: {
    // 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.pageCurrentWB = 1
        this.pageSizeWB = 1000
        this.pageCurrent = 1
        this.pageSize = 1000
        this.getTableData()
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    // 在什么情况下需要查询上传的附件，调用this.getFileData()即可根据
    getFileData(machineValue) {
      this.hadFile = []
      getFileData(machineValue).then(res => {
        if (res.data.length > 0) {
          res.data.map(item => {
            this.hadFile.push({
              url: `${ServerIP}${item.文件标识}.${item.文件后缀}`,
              name: `${item.文件名称}.${item.文件后缀}`,
              suffix: item.文件后缀,
              id: item.文件标识
            })
          })
        }
      })
    },
    fetchData() {
      this.getSelect(machineSelect, ['机床图号', '机床流水号'], 'machine')
    },
    getGroupSelect(val) {
      this.getSelect(groupSelect, ['组号', '组件流水号'], 'group', val)
    },
    getGroupSelect1() {
      this.form.组号 = ''
      this.getSelect(groupSelect, ['组号', '组件流水号'], 'group1', this.form.机床号)
    },
    getTableData() {
      this.getFileData(this.machineValue)
      console.log(this.hadFile, 88899)
      if (this.machineValue) {
        const machine_check = this.machineValue ? 1 : 0
        const group_check = this.groupValue ? 1 : 0
        this.loading = true
        basicPartsData(machine_check, this.machineValue, group_check, this.groupValue, this.pageSize, this.pageCurrent).then(response => {
          this.loading = false
          const res = response.data
          this.total = res.total
          console.log(this.tableData)
          this.tableData = res.rows.map(item => {
            if (item.整改类型 === '1') {
              item.整改类型 = '新增'
            } else if (item.整改类型 === '2') {
              item.整改类型 = '替换'
            } else if (item.整改类型 === '3') {
              item.整改类型 = '补充加工'
            }
            return item
          })
        })
      } else {
        this.$message.warning('请输入查询条件')
      }
      this.loadingWB = true
      purchasePartsData(this.groupValue, this.pageCurrentWB, this.pageSizeWB).then(response => {
        this.loadingWB = false
        const data = response.data
        this.totalWB = data.total
        this.tableDataWB = data.rows.map(item => {
          console.log(item)
          if (item.整改类型 === '1') {
            item.整改类型 = '新增'
          } else if (item.整改类型 === '2') {
            item.整改类型 = '替换'
          } else if (item.整改类型 === '3') {
            item.整改类型 = '补充加工'
          }
          item.零件类型 = item.零件类型 === '2' ? '外购件' : '标准件'
          return item
        })
      })
    },
    openUrgentItem() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible = true
    },
    tableRowClassName1({ row }) {
      if (row.是否加急件 === '1') {
        return 'isUrgentItem1'
      }
    },
    handleDelete(row) {
      if (row.基本件流水号) {
        this.deleteRow(deleteBasicParts, row.基本件流水号, function() {
          this.getTableData()
        })
      } else {
        this.deleteRow(deletePurchaseData, row.标准件和外购件流水号, function() {
          this.getTableData()
        })
      }
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.getTableData()
    },
    handleSizeChangeWB(val) {
      this.pageCurrentWB = 1
      this.pageSizeWB = val
      this.getTableData()
    },
    handleCurrentChangeWB(val) {
      this.pageCurrentWB = val
      this.getTableData()
    }
  }
}
</script>

<style scoped>
span{
  margin: 0px;
}
.wrapper{
  width: 225px;
  height: 50px;
  overflow: hidden;
  border: 1px solid #b8c7d9;
  float: left;
  margin: 0px;
}
.content{
  position: relative;
  padding: 8px 8px 0 8px;
}
.action{
  position: absolute;
  top: 4px;
  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{
  position: absolute;
  left: 44px;
  line-height: 30px;
  display: inline-block;
  color: #999;
}
.el-card {
   padding: 0px !important;
   margin: 0px !important;
}
</style>
<style>
  .el-table .isUrgentItem1{
    background: #ff7575
  }
</style>
