<template>
  <div>
    <el-card>
      <el-row>
        <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-checkbox v-model="all" size="mini" @change="searchTable">查询全部</el-checkbox>-->
        <!--<el-button :disabled="all" size="small" icon="el-icon-success" type="primary" style="margin-left: 10px" @click="edit()">自家备料出库</el-button>-->
        <!--<el-button size="small" icon="el-icon-refresh" type="success" style="margin-left: 10px" @click="pageSize=10; pageCurrent=1;searchTable()">刷新</el-button>-->
      </el-row>
    </el-card>

    <el-card>
      <el-table v-loading="loading" :data="tableData" height="580" style="width: 100%;" border element-loading-text="拼命加载中">
        <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">
          <template slot-scope="scope">
            {{ scope.row.工艺要求 }}
          </template>
        </el-table-column>
        <el-table-column prop="request" label="重量(kg)" align="center" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.重量" size="mini"/>
          </template>
        </el-table-column>
        <el-table-column prop="request" label="单价(元)" align="center" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.单价" size="mini"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150px" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
              @click="handleChange1(scope.row)">出库</el-button>
          </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, change, handlechange, searchtable3, change2 } from '@/api/ManufacturingCenter/MaterialArrivalManagement'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      bianliang1: [],
      bianliang2: [],
      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'
    ])
  },
  created() {
    this.searchTable()
    this.searchTable2()
  },
  methods: {
    // 查询表格数据
    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, !this.all, 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].重量
            })
          })
          this.loading = false
        }
      })
    },
    // 自家备料出库
    handleChange1(row) {
      change(row.工艺计划流水号, row.重量, row.单价).then(() => {
        handlechange(row.工序流水号, '0000')
      }).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, '0000', 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>
