<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span>产品编号：</span>
        <el-input v-model="partCode" size="small" clearable style="width: 250px" />
        <el-button type="primary" size="small" icon="el-icon-search" style="margin-left: 15px" @click="getTable2">查询</el-button>
        <div style="margin-top: 5px">
          <el-table
            :data="RepairManagementDataLogTable"
            :header-cell-style="{ background: '#F5F5F5', fontSize: '22px' }"
            style="width: 100%;"
            border
            height="680px"
            tooltip-effect="dark"
          >
            <el-table-column type="index" align="center" label="序号" width="60px" />
            <el-table-column align="center" label="订单号" width="150px">
              <template slot-scope="scope">
                {{ scope.row.订单号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="工位号" width="100px">
              <template slot-scope="scope">
                {{ scope.row.工位号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="物料号" width="150px">
              <template slot-scope="scope">
                {{ scope.row.物料号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="物料名称" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.零件名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="数量" width="70px">
              <template slot-scope="scope">
                {{ scope.row.零件数量 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作方式" width="100px">
              <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 align="center" label="旧条码">
              <template slot-scope="scope">
                {{ scope.row.物料条码 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作人" width="100px">
              <template slot-scope="scope">
                {{ scope.row.更换人 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="处理时间" width="170px">
              <template slot-scope="scope">
                {{ scope.row.更换时间 }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'RepairManagement',
  data() {
    return {
      barcodeType: 1,
      RepairType: 1,
      currentSelectRow: {},
      RepairManagementReplaceDialogVisible: false,
      RepairManagementDestroyAllDialogVisible: false,
      RepairManagementReplaceForm: {
        ID: 0,
        isUsedOpMaterial: true,
        TraceabilityCode: 0,
        materialCount: 0,
        materialCode: '',
        materialBarCode: '',
        materialName: '',
        materialBarCodeNew: ''
      },
      RepairManagementReplaceRules: {
        materialBarCodeNew: [{ required: true, message: '请输入/扫描新物料码！', trigger: 'blur' }]
      },
      RepairManagementType: '1',
      partCode: '',
      barcode: '',
      opCode: '',
      opCodeS: [],
      stationArr: [],
      RepairManagementDataTable: [],
      RepairManagementDataLogTable: [],
      RepairManagementDataTableOrigin: [],
      RepairManagementMaterialDataTable: [],
      engineValue: {
        OpName: null,
        EngineID: null, // 产品编号
        EngineType: null, // 产品型号
        EngineTypeID: null, // 机型代码
        PalletCode: null,
        ProcessCode: null, // 程序号
        PartPalletCode: null, // 托盘号
        PalletInfo: null, // 托盘信息
        RepairPartStatusPLC: null,
        QualityMask: null,
        OrderForm: null, // 订单号
        EngineTypeString: null // 产品型号字符串北汽专用
      }
    }
  },
  created() {
    this.getStation()
  },
  mounted() {
  },
  methods: {
    receiveBarCode(barCode) {
      if (barCode.includes('CG1A6220') && barCode.length === 19) {
        this.barcode = barCode
        this.getTable()
        return
      } else {
        if (this.barcodeType === 2) {
          this.RepairManagementReplaceForm.materialBarCodeNew = barCode
        }
      }
    },
    getStation() {
      this.stationArr = []
      const Data = this.CreateData('11', '工位与名称_视图_查询')
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationArr.push({
            label: `【${response.data[i].工位号}】${response.data[i].工位名称}`,
            value: response.data[i].工位号,
            text: `【${response.data[i].工位号}】${response.data[i].工位名称}`,
            MesWebCode: response.data[i].MesWebCode
          })
        }
        // this.stationArr.unshift({
        //   label: `全部工位`,
        //   value: 'ALL'
        // })
        // this.opCode = 'ALL'
      })
    },
    getTable() {
      this.RepairManagementDataTable = []
      this.RepairManagementMaterialDataTable = []
      this.currentSelectRow = {}
      this.barcodeType = 1
      this.RepairManagementReplaceDialogVisible = false
      if (this.barcode === '') {
        this.$message.error('请输入产品编号！')
        return
      }
      const param = []
      param[0] = ['发动机号', this.barcode]
      const Data = this.CreateData('11', '质量数据查询_测量数据发动机在线状态_返修工位', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) {
          this.RepairManagementDataTable = response.data
          this.engineValue.OrderForm = response.data[0]['订单号']
          this.engineValue.EngineType = response.data[0]['机型']
          // this.engineValue.EngineTypeID = response.data[0]['机型ID']
          this.engineValue.EngineID = response.data[0]['发动机号']
          this.engineValue.PalletInfo = response.data[0]['车体二维码']
          // this.engineValue.ProcessCode = response.data[0]['上线总排序']
        } else {
          this.RepairManagementDataTable = []
          this.engineValue.OrderForm = ''
          this.engineValue.EngineType = ''
          // this.engineValue.EngineTypeID = response.data[0]['机型ID']
          this.engineValue.EngineID = ''
          this.engineValue.PalletInfo = ''
          // this.engineValue.ProcessCode = response.data[0]['上线总排序']
        }
        this.$emit('initEngineValue', this.engineValue)
      })
    },
    getTable1(row) {
      this.currentSelectRow = row
      this.RepairManagementMaterialDataTable = []
      const param = []
      param[0] = ['工位号', row.工位号]
      param[1] = ['发动机号', row.发动机号]
      param[2] = ['发动机号_ischeck', 1]
      param[3] = ['工位号_ischeck', 1]
      param[4] = ['是否禁用', 0]
      param[5] = ['PageCurrent', 1]
      param[6] = ['PageSize', 100]
      param[7] = ['PageCount', '1111', 'int', '1']
      param[8] = ['ItemCount', '1111', 'int', '1']
      const Data = this.CreateData('11', '物料数据_视图_查询_返修', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result.length > 0) {
          this.RepairManagementMaterialDataTable = response.data.result
        } else {
          this.RepairManagementMaterialDataTable = []
        }
      })
    },
    getTable2() {
      if (this.partCode === '') {
        this.$message.error('请输入产品编号！')
        return
      }
      this.RepairManagementDataLogTable = []
      const param = []
      param[0] = ['发动机号', this.partCode]
      const Data = this.CreateData('11', '电子看板_装配零件_返修件_返修记录_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) {
          console.log(response.data)
          this.RepairManagementDataLogTable = response.data
        } else {
          this.RepairManagementDataLogTable = []
        }
      })
    },
    filterHandler(value, row, column) {
      const property = column['property']
      this.RepairManagementMaterialDataTable = []
      return row[property] === value
    },
    RepairManagementReplace(row) {
      this.RepairManagementReplaceForm.materialCode = row.物料号
      this.RepairManagementReplaceForm.materialName = row.零件名称
      this.RepairManagementReplaceForm.TraceabilityCode = row.追溯代码
      this.RepairManagementReplaceForm.ID = row.ID
      this.RepairManagementReplaceForm.materialCount = row.零件数量
      this.RepairManagementReplaceForm.materialBarCode = row.物料条码
      this.RepairManagementReplaceForm.materialBarCodeNew = ''

      this.RepairManagementReplaceDialogVisible = true
      this.barcodeType = 2
    },
    // 返修替换提交
    RepairManagementReplaceSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = []
          param[0] = ['工位号', this.$store.state.station.stationName]
          param[1] = ['ID', this.RepairManagementReplaceForm.ID]
          param[2] = ['追溯代码', this.RepairManagementReplaceForm.TraceabilityCode]
          param[3] = ['操作者', this.$store.state.user.name]
          param[4] = ['新物料条码', this.RepairManagementReplaceForm.materialBarCodeNew]
          param[5] = ['是否消耗工位物料', this.RepairManagementReplaceForm.isUsedOpMaterial ? 1 : 0]
          const Data = this.CreateData('12', '电子看板_装配零件_返修件_更换物料_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data.length > 0) {
              if (response.data[0].result === '1') {
                this.$message.success('物料更换成功！')
                this.RepairManagementReplaceDialogVisible = false
                this.barcodeType = 1
                this.getTable1(this.currentSelectRow)
              } else {
                this.$message.error('物料更换失败！')
              }
            } else {
              this.$message.error('物料更换失败！')
            }
          })
        }
      })
    },
    // 返修替换取消
    RepairManagementReplaceCancel(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.RepairManagementReplaceDialogVisible = false
      this.barcodeType = 1
    },
    RepairManagementDestroy(row) {
      this.$confirm(`是否报废当前物料【${row.物料号}】【${row.零件名称}】?`, '物料报废', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = []
        param[0] = ['工位号', this.$store.state.station.stationName]
        param[1] = ['ID', row.ID]
        param[2] = ['追溯代码', row.追溯代码]
        param[3] = ['操作者', this.$store.state.user.name]
        param[4] = ['新物料条码', '']
        const Data = this.CreateData('12', '电子看板_装配零件_返修件_报废物料_编辑', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data.length > 0) {
            if (response.data[0].result === '1') {
              this.$message.success('物料报废成功！')
              this.getTable1(this.currentSelectRow)
            } else {
              this.$message.error('物料报废失败！')
            }
          } else {
            this.$message.error('物料报废失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作！'
        })
      })
    },
    RepairManagementReturnOp() {
      if (this.barcode === '') {
        this.$message.error('请先扫描条码！')
        return
      }
      this.$confirm(`剩余物料将返回线边库位?`, '物料返回', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = []
        param[0] = ['发动机号', this.barcode]
        param[1] = ['操作者', this.$store.state.user.name]
        const Data = this.CreateData('12', '电子看板_装配零件_返修件_报废物料_返回', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data.length > 0) {
            if (response.data[0].result === '1') {
              this.$message.success('物料回仓成功！')
              this.getTable1(this.currentSelectRow)
            } else {
              this.$message.error('物料回仓失败！')
            }
          } else {
            this.$message.error('物料回仓失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作！'
        })
      })
    }
  }
}
</script>

<style scoped>
.topCard{
  margin: 5px;
}
.topDiv{
  margin-bottom: 10px;
}
</style>
