<template>
  <div>
    <el-card>
      <span>零件图号:</span>
      <el-input v-model="partName" placeholder="零件图号" size="small" clearable style="width:200px"/>
      <el-button size="small" icon="el-icon-search" type="success" style="margin-left: 10px" @click="pageSize=10; pageCurrent=1;searchTable()">查询</el-button>
      <el-button size="small" icon="el-icon-success" type="primary" style="margin-left: 10px" @click="edit2()">自家备料确认</el-button>
      <el-button size="small" icon="el-icon-success" type="primary" style="margin-left: 10px" @click="back()">打回</el-button>
    </el-card>
    <el-card>
      <el-table v-loading="loading" :data="tableData" height="580" style="width: 100%;" border element-loading-text="拼命加载中" @selection-change="handleSelectionChange2">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="零件名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" width="280px">
          <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>
      <div class="block">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <el-card>
      <span>零件图号:</span>
      <el-input v-model="partName2" placeholder="零件图号" size="small" clearable style="width:200px"/>
      <el-button size="small" icon="el-icon-search" type="success" style="margin-left: 10px" @click="total2=0;pageSize2=10; pageCurrent2=1;searchTable2()">查询</el-button>
      <el-button size="small" icon="el-icon-success" type="primary" style="margin-left: 10px" @click="edit()">外购备料确认</el-button>
    </el-card>
    <el-card>
      <el-table v-loading="loading2" :data="tableData2" height="580" style="width: 100%;" border element-loading-text="拼命加载中" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="零件名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" width="240px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="重量" align="center" width="140px">
          <template slot-scope="scope">
            {{ scope.row.重量1 }}
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" width="140px">
          <template slot-scope="scope">
            {{ scope.row.单价 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">
            {{ scope.row.备注1 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          v-if="!loading2"
          :current-page="pageCurrent2"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize2"
          :total="total2"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"/>
      </div>
    </el-card>

  </div>
</template>

<script>
import { searchtable, searchtable2, searchtable3, change2, change3, edit } from '@/api/ManufacturingCenter/MaterialArrivalManagement'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      arrest2: [],
      editTime: '',
      bianliang1: [],
      bianliang2: [],
      bianliang3: [],
      num: 0,
      arrest: [],
      all: false,
      partName: '',
      loading: false,
      tableData: [], // 表格数据
      total: null, // 总条数
      pageSize: 10, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      loading2: false,
      partName2: '',
      tableData2: [], // 表格数据
      total2: null, // 总条数
      pageSize2: 10, // 每页显示条数
      pageCurrent2: 1 // 后台获取页
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id',
      'userId'
    ])
  },
  created() {
    this.searchTable()
    this.searchTable2()
  },
  methods: {
    handleSelectionChange2(val) {
      this.arrest2 = val
    },
    edit2() {
      this.bianliang1 = []
      this.bianliang2 = []
      if (this.arrest2.length === 0) {
        this.$message.warning('请选择零件')
      } else {
        for (let i = 0; i < this.arrest2.length; i++) {
          this.bianliang1.push(this.arrest2[i].工艺计划流水号)
          this.bianliang2.push(this.arrest2[i].工序流水号)
        }
        change3(this.bianliang2, this.userId, this.bianliang1).then(response => {
          this.searchTable()
        })
      }
    },
    // 查询表格数据
    searchTable() {
      this.loading = true
      this.tableData = []
      this.Data = []
      if (this.partName !== '' && this.partName !== null) {
        this.partNamecheck = true
      } else {
        this.partNamecheck = false
      }
      searchtable(this.partNamecheck, this.partName, 0, this.pageCurrent, this.pageSize).then(response => {
        if (response.data.total === 0) {
          this.loading = false
        } else {
          this.Data = response.data.rows
          this.total = response.data.total
        }
      }).then(() => {
        for (let i = 0; i < this.Data.length; i++) {
          searchtable2(this.Data[i].工艺计划流水号).then(response => {
            this.tableData.push({
              工艺计划流水号: this.Data[i].工艺计划流水号,
              零件名称: this.Data[i].零件名称,
              零件图号: this.Data[i].零件图号,
              备料到货: this.Data[i].备料到货,
              工艺要求: response.data[0].工艺要求,
              工序流水号: response.data[0].工序流水号,
              重量: response.data[0].重量,
              单价: 0
            })
          })
          this.loading = false
        }
      })
    },
    back() {
      if (this.arrest2.length === 0) {
        this.$message.warning('请选择零件')
      } else {
        this.a = 0
        for (let i = 0; i < this.arrest2.length; i++) {
          edit(this.arrest2[i].工艺计划流水号).then(response => {
            this.a += parseInt(response.data[0].result)
            if (this.a === this.arrest2.length) {
              this.$message.success('打回成功')
              this.searchTable()
            } else {
              this.$message.error('打回失败')
            }
          })
        }
      }
    },
    zeroFill(i) {
      if (i >= 0 && i <= 9) {
        return '0' + i
      } else {
        return i
      }
    },
    // // 自家备料确认
    // handleChange1(row) {
    //   var date = new Date()
    //   var month = this.zeroFill(date.getMonth() + 1)
    //   var day = this.zeroFill(date.getDate())
    //   this.editTime = date.getFullYear() + '-' + month + '-' + day
    //   change(row.工艺计划流水号, row.重量, row.单价).then(() => {
    //     handlechange(row.工序流水号, this.userId).then(response => {
    //       handlechange2(row.工序流水号, this.editTime).then(response => {
    //         this.searchTable()
    //       })
    //     })
    //   })
    // },
    handleSelectionChange(val) {
      this.arrest = val
    },
    searchTable2() {
      this.loading2 = true
      this.tableData2 = []
      this.Data2 = []
      if (this.partName2 !== '' && this.partName2 !== null) {
        this.partNamecheck2 = 1
      } else {
        this.partNamecheck2 = 0
      }
      searchtable3(this.partNamecheck2, this.partName2, this.pageCurrent2, this.pageSize2).then(response => {
        if (response.data.total === 0) {
          this.loading2 = false
        } else {
          this.Data2 = response.data.rows
          this.total2 = response.data.total
        }
      }).then(() => {
        for (let i = 0; i < this.Data2.length; i++) {
          searchtable2(this.Data2[i].工艺计划流水号).then(response => {
            this.tableData2.push({
              工艺计划流水号: this.Data2[i].工艺计划流水号,
              零件名称: this.Data2[i].零件名称,
              零件图号: this.Data2[i].零件图号,
              重量1: this.Data2[i].重量1,
              单价: this.Data2[i].单价,
              备注1: this.Data2[i].备注1,
              工序流水号: response.data[0].工序流水号
            })
          })
          this.loading2 = false
        }
      })
    },
    edit() {
      this.bianliang1 = []
      this.bianliang2 = []
      if (this.arrest.length === 0) {
        this.$message.warning('请选择零件')
      } else {
        for (let i = 0; i < this.arrest.length; i++) {
          this.bianliang1.push(this.arrest[i].工艺计划流水号)
          this.bianliang2.push(this.arrest[i].工序流水号)
        }
        change2(this.bianliang2, this.userId, this.bianliang1).then(response => {
          this.searchTable2()
        })
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    },
    handleSizeChange2(val) {
      this.pageCurrent2 = 1
      this.pageSize2 = val
      this.searchTable2()
    },
    handleCurrentChange2(val) {
      this.pageCurrent2 = val
      this.searchTable2()
    }
  }
}
</script>

<style scoped>

</style>
