<template>
  <div>
    <el-card>
      <span>机床号:</span>
      <el-select v-model="Machine" filterable clearable size="small" style="margin-top:10px;width: 220px" placeholder="机床号" @change="getGroup()">
        <el-option
          v-for="item in Machines"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.value2 }}</div>
        </el-option>
      </el-select>
      <span>组号:</span>
      <el-select v-model="Group" size="small" clearable placeholder="组号" style="margin-top:10px;width: 180px;">
        <el-option
          v-for="item in Groups"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>零件图号:</span>
      <el-input v-model="partName" placeholder="零件图号" size="small" clearable style="width:200px;margin: 3px 0"/>
      <el-tooltip :open-delay="1200" effect="dark" content="自家备料查询" placement="top">
        <el-button size="small" icon="el-icon-search" type="success" style="margin-left: 10px" @click="pageSize=10; pageCurrent=1;searchTable()">查询</el-button>
      </el-tooltip>
      <el-tooltip :open-delay="1200" effect="dark" content="自家备料完成" placement="top">
        <el-button size="small" icon="el-icon-success" type="warning" plain style="margin-left: 10px" @click="edit2()">自家备料确认</el-button>
      </el-tooltip>
      <el-tooltip :open-delay="1200" effect="dark" content="将备料件打回重新分配" placement="top">
        <el-button size="small" icon="el-icon-success" type="danger" style="margin-left: 10px" @click="back()">打回</el-button>
      </el-tooltip>
    </el-card>
    <el-card>
      <el-table v-loading="loading" :data="tableData" height="350" style="width: 100%;margin: 3px 0" size="mini" border stripe highlight-current-row element-loading-text="拼命加载中" @selection-change="handleSelectionChange2">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="零件名称" prop="零件名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" prop="零件图号" 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"
          style="margin: 3px 0"
          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;margin: 3px 0"/>
      <el-tooltip :open-delay="1200" effect="dark" content="外购备料查询" placement="top">
        <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-tooltip>
      <el-tooltip :open-delay="1200" effect="dark" content="外购备料完成" placement="top">
        <el-button size="small" icon="el-icon-success" type="warning" style="margin-left: 10px" @click="edit()">外购备料确认</el-button>
      </el-tooltip>
    </el-card>
    <el-card>
      <el-table v-loading="loading2" :data="tableData2" height="350" style="width: 100%;margin: 3px 0" size="mini" border stripe highlight-current-row element-loading-text="拼命加载中" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="零件名称" prop="零件名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" prop="零件图号" 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 { getMachines, getGroups, searchtable, searchtable2, searchtable3, change2, change3, edit } from '@/api/ManufacturingCenter/MaterialArrivalManagement'
import { mapGetters } from 'vuex'
import Cookie from 'js-cookie'
export default {
  data() {
    return {
      Machine: '',
      Machines: [],
      Group: '',
      Groups: [],
      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'
    ])
  },
  watch: {
    // 如果路由有变化，会再次执行该方法
    '$route': {
      handler(route) {
        const _this = this
        if (route.name === 'MaterialArrivalManagement') {
          _this.getMachine()
        }
      }
    }
  },
  created() {
    this.getMachine()
    // 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()
        })
      }
    },
    getMachine() {
      if (Cookie.get('machineValue') !== undefined) {
        this.Machine = ''
        this.Machines = []
        this.Group = ''
        this.Groups = []
        getMachines().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号,
              value2: response.data[i].项目名称
            })
          }
        }).then(() => {
          this.Machine = parseInt(Cookie.get('machineValue'))
          this.getGroup()
          this.Group = parseInt(Cookie.get('group'))
          this.searchTable()
          Cookie.remove('machineValue')
          Cookie.remove('group')
          this.searchTable2()
        })
      } else {
        this.Machine = ''
        this.Machines = []
        this.Group = ''
        this.Groups = []
        getMachines().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号,
              value2: response.data[i].项目名称
            })
          }
        })
        this.searchTable()
      }
    },
    getGroup() {
      this.Group = ''
      this.Groups = []
      getGroups(this.Machine).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Groups.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    // 查询表格数据
    searchTable() {
      this.loading = true
      this.tableData = []
      this.Data = []
      if (this.Machine) { this.Machinecheck = true } else { this.Machinecheck = false }
      if (this.Group) { this.Groupcheck = true } else { this.Groupcheck = false }
      if (this.partName) { this.partNamecheck = true } else { this.partNamecheck = false }
      searchtable(this.Machinecheck, this.Machine, this.Groupcheck, this.Group, 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>
