<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>关联状态：</span>
        <el-checkbox-group v-model="connectStatus" :min="1" size="small" style="display: inline-block">
          <el-checkbox label="0">未关联</el-checkbox>
          <el-checkbox label="1">已关联</el-checkbox>
        </el-checkbox-group>
        <el-button size="small" type="success" icon="el-icon-search" style="margin: 0 0 0 10px" @click="pageCurrent1=1;pageSize1=10;total1=0;searchTable1();">查询</el-button>
      </div>
      <h4 style="margin-top: 0">滞货使用情况表</h4>
      <el-table :data="tableData1" size="mini" stripe border style="width: 100%;" highlight-current-row height="440" @row-click="searchConnect">
        <el-table-column label="关联状态" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.是否关联)===1" type="success" size="small">已关联</el-tag>
            <el-tag v-else type="info" size="small">未关联</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="图号或型号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column label="规格" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column label="机床图号" align="center" min-width="100">
          <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="100">
          <template slot-scope="scope">
            {{ scope.row.使用滞货数量 }}
          </template>
        </el-table-column>
        <el-table-column label="使用人姓名" align="center" min-width="90">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="使用时间" align="center" width="140">
          <template slot-scope="scope">
            {{ scope.row.操作时间 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template slot-scope="scope">
            <el-tooltip :open-delay="1000" effect="dark" content="将滞货与项目关联并使用" placement="top">
              <el-button :disabled="Number(scope.row.是否关联)===1" size="mini" type="warning" plain @click="connectBacklog(scope.row)">关联物料</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin: 10px 0;">
        <el-pagination
          :current-page="PageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="PageSize1"
          :total="total1"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
      <h4>滞货关联明细</h4>
      <el-table :data="tableData2" size="mini" border style="width: 100%;" height="220">
        <el-table-column label="序号" align="center" type="index" width="80"/>
        <el-table-column label="名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="图号或型号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column label="规格" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column props="仓库名称" 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="关联数量" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.关联数量 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-el-drag-dialog :visible.sync="dialogVisible1" title="关联物料" center style="min-height: 300px;" width="900px">
      <el-table :data="tableData01" size="mini" border style="width: 100%;" height="220" show-summary @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center" width="55"/>
        <el-table-column label="名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="图号或型号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column label="规格" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column props="仓库名称" 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="货位存量" align="center" width="80">
          <template slot-scope="scope">
            {{ scope.row.货位存量 }}
          </template>
        </el-table-column>
        <el-table-column label="关联数量" align="center" width="120" prop="关联数量">
          <template slot-scope="scope">
            <el-input-number :min="0" v-model="scope.row.关联数量" size="mini" controls-position="right" style="width: 100%"/>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible1=false">取消</el-button>
        <el-button type="primary" size="small" @click="submitConnectBacklog">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchBacklogUseRecord, searchBacklogLocation, submitConnectBacklog, searchBacklogConnectDetail } from '@/api/PurchasingCenter/BacklogGoodsPrepare'
import { mapGetters } from 'vuex'
export default {
  name: 'BacklogGoodsPrepare',
  data() {
    return {
      connectStatus: ['0'],
      tableData1: [],
      PageCurrent1: 1,
      PageSize1: 10,
      total1: 0,
      tableData2: [],
      dialogVisible1: false,
      tableData01: [],
      multipleSelection: [],
      useNumber: '', // 使用数量
      useNum: '' // 滞货使用流水号
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  mounted() {
    // this.initData()
    this.searchTable1()
  },
  methods: {
    // 初始化数据
    initData() {},
    // 查询滞货使用记录
    searchTable1() {
      let status
      if (Number(this.connectStatus) === 0) {
        status = 0
      } else if (Number(this.connectStatus) === 1) {
        status = 1
      } else {
        status = 2
      }
      searchBacklogUseRecord(this.PageCurrent1, this.PageSize1, status).then(response => {
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.pageCurrent1 = 1
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    // 查询滞货关联
    searchConnect(row) {
      searchBacklogConnectDetail(row.滞货使用流水号).then(response => {
        this.tableData2 = response.data
      })
    },
    // 关联物料
    connectBacklog(row) {
      this.dialogVisible1 = true
      this.useNumber = row.使用滞货数量
      this.useNum = row.滞货使用流水号
      searchBacklogLocation(row.物料流水号).then(response => {
        this.tableData01 = response.data
        this.tableData01.map(v => {
          this.$set(v, '关联数量', 0)
          return v
        })
      })
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 提交关联物料
    submitConnectBacklog() {
      let sum = 0
      const numGroup = []
      const materialLocalGroup = []
      for (let i = 0; i < this.multipleSelection.length; i++) {
        sum += this.multipleSelection[i].关联数量
        numGroup.push(this.multipleSelection[i].关联数量)
        materialLocalGroup.push(this.multipleSelection[i].物料与货位对照流水号)
      }
      if (sum === Number(this.useNumber)) {
        submitConnectBacklog(this.useNum, materialLocalGroup, numGroup).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('关联成功')
            this.searchTable1()
            this.dialogVisible1 = false
          } else {
            this.$message.error('关联失败')
          }
        })
      } else {
        this.$message.error('关联数量有误')
      }
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .el-tag{
    margin: 0
  }
</style>
