<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-row>
        <el-col style="width: 350px">
          <div class="topDiv">
            <el-row style="margin-top: 10px">
              <span style="margin-left: 10px">工位号：</span>
              <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" style="width: 240px;margin-left: 13px" @change="searchTable();searchTable1()">
                <el-option
                  v-for="item in stationNumber"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  size="mini"/>
              </el-select>
            </el-row>
            <el-row style="margin-top: 10px">
              <span style="margin-left: 10px">产品型号：</span>
              <el-select v-model="engineTypeValue" placeholder="请选择产品型号" size="small" style="width: 240px" @change="searchTable();searchTable1()">
                <el-option
                  v-for="item in engineType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  size="mini"/>
              </el-select>
            </el-row>
            <el-row style="margin-top: 10px">
              <span style="margin-left: 10px">工位名称：</span>
              <el-input v-model="stationName" disabled style="width:240px;" placeholder="" size="small"/>
            </el-row>
            <el-row style="margin:10px 0 0 10px">
              <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="submitAdd('form')">增加</el-button>
              <el-button type="success" plain icon="el-icon-edit" size="small" style="margin-left: 35px" @click="submitUpdate">修改</el-button>
              <el-button type="danger" plain icon="el-icon-delete" size="small" style="margin-left: 35px" @click="deleteInformation">删除</el-button>
            </el-row>
          </div>
          <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="90px">
            <el-form-item label="序号:" label-width="60px" prop="serialNumber" style="display: inline-block">
              <el-input-number v-model="form.serialNumber" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="测量位置X:" prop="positionX" style="display: inline-block">
              <el-input-number v-model="form.positionX" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="组号:" label-width="60px" prop="groupNumber" style="display: inline-block">
              <el-input-number v-model="form.groupNumber" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="测量位置Y:" prop="positionY" style="display: inline-block">
              <el-input-number v-model="form.positionY" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="顺序:" label-width="60px" prop="orderNumber" style="display: inline-block">
              <el-input-number v-model="form.orderNumber" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="控件宽度:" prop="measureWidth" style="display: inline-block">
              <el-input-number v-model="form.measureWidth" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="套筒号:" label-width="60px" prop="sleeveNumber:" style="display: inline-block">
              <el-input-number v-model="form.sleeveNumber" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="控件高度:" prop="measureHeight" style="display: inline-block">
              <el-input-number v-model="form.measureHeight" :min="0" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="扭矩:" label-width="60px" prop="torque" style="display: inline-block">
              <el-input v-model="form.torque" class="inputTorque" size="mini" style="width: 70px;"/>
            </el-form-item>
            <el-form-item label="控件类型:" prop="measureType" style="display: inline-block">
              <el-input-number v-model="form.measureType" :min="1" :max="3" size="mini" controls-position="right"/>
            </el-form-item>
            <el-form-item label="说明:" label-width="50px" prop="explain" style="display: inline-block">
              <el-input
                v-model="form.explain"
                :rows="2"
                clearable
                size="small"/>
            </el-form-item>
          </el-form>
          <el-button type="info" plain size="small" style="margin-left: 80px" @click="viewAllow()">预览全部</el-button>
          <el-button type="info" plain size="small" @click="clearAll()">全部清除</el-button>
          <el-button type="success" size="small" plain icon="el-icon-document-copy" style="margin: 48px 0 0 0" @click="copyOrder('form4')">复制工步</el-button>
        </el-col>
        <el-col style="width: 1000px">
          <div style="border: 1px solid black;width: 772px;height: 642px;margin-top: 10px">
            <div v-for="(url, key) in urls" :key="key" style="position: absolute;z-index: 1;" @click="getMouseXY($event)">
              <img :key="url.url" :src="url.url" :preview-src-list="urls1" lazy style="width: 770px; height:640px">
            </div>
            <div v-for="(item, index) in tableData" v-show="allowView" :key="item + index" style="position: absolute;z-index: 2;">
              <img
                v-show="Number(item.控件类型) === 1"
                :style="{width: item.控件宽度 + 'px', height:item.控件高度 + 'px',left: item.测量位置X + 'px', top: item.测量位置Y + 'px'}"
                src="src/assets/img/green.png"
                style="position: absolute">
              <img
                v-show="Number(item.控件类型) === 2"
                :style="{width: item.控件宽度 + 'px', height:item.控件高度 + 'px',left: item.测量位置X + 'px', top: item.测量位置Y + 'px'}"
                src="src/assets/img/gray.png"
                style="position: absolute">
              <img
                v-show="Number(item.控件类型) === 3"
                :style="{width: item.控件宽度 + 'px', height:item.控件高度 + 'px',left: item.测量位置X + 'px', top: item.测量位置Y + 'px'}"
                src="src/assets/img/init1.png"
                style="position: absolute">
            </div>
            <div v-show="singleView" style="position: absolute;z-index: 2;">
              <img
                v-show="Number(form.measureType) === 1"
                :style="{width: form.measureWidth + 'px', height:form.measureHeight + 'px',left: form.positionX + 'px', top: form.positionY + 'px'}"
                src="src/assets/img/green.png"
                style="position: absolute">
              <img
                v-show="Number(form.measureType) === 2"
                :style="{width: form.measureWidth + 'px', height:form.measureHeight + 'px',left: form.positionX + 'px', top: form.positionY + 'px'}"
                src="src/assets/img/gray.png"
                style="position: absolute">
              <img
                v-show="Number(form.measureType) === 3"
                :style="{width: form.measureWidth + 'px', height:form.measureHeight + 'px',left: form.positionX + 'px', top: form.positionY + 'px'}"
                src="src/assets/img/init1.png"
                style="position: absolute">
            </div>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-table
          ref="multipleTable"
          :data="tableData"
          :cell-class-name="tableRowClassName"
          highlight-current-row
          height="300px"
          class="main"
          tooltip-effect="dark"
          style="width: 1122px"
          border
          size="mini"
          @row-click="rowClick">
          <el-table-column align="center" width="70px" label="序号">
            <template slot-scope="scope">
              {{ scope.row.序号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" width="70px" label="组号">
            <template slot-scope="scope">
              {{ scope.row.组号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" width="80px" label="工作顺序">
            <template slot-scope="scope">
              {{ scope.row.工作顺序 }}
            </template>
          </el-table-column>
          <el-table-column align="center" width="70px" label="套筒号">
            <template slot-scope="scope">
              {{ scope.row.套筒号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="位置X">
            <template slot-scope="scope">
              {{ scope.row.测量位置X }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="位置Y">
            <template slot-scope="scope">
              {{ scope.row.测量位置Y }}
            </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="扭矩">
            <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>
      </el-row>
      <!--      <div v-drag v-show="numKeyboarInfo.isShowFlag" ref="numKeyboardsBox" :style="numKeyboarInfo.styleObject" class="numKeyboardsBox">-->
      <!--        <numKeyboards id="keyboards" ref="keyboards" :keyboardtext="numKeyboarInfo.keyboardtext" @updatekey="numKeyboardsGetValueFn" @enterClick="numKeyboardsEnterClickFn" />-->
      <!--      </div>-->
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible4" title="数据复制" center style="min-height: 200px" width="400px">
      <el-form ref="form1" :model="form4" :rules="rules4" label-position="right" label-width="110px">
        <el-form-item label="产品型号" prop="engineTypeValue" style="display: inline-block">
          <el-select v-model="form4.stationNumberValue" placeholder="请选择工位号" size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="产品型号" prop="engineTypeValue" style="display: inline-block">
          <el-select v-model="form4.engineTypeValue" placeholder="请选择产品型号" size="small">
            <el-option
              v-for="item in engineType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff4()">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd4()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      urls: [], // 图片数组
      urls1: [], // 图片数组
      action: '',
      showFileList: false,
      hadFile: [
      ],
      fileList: [],
      fileName: '',
      fileName1: '',
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      stationName: '',
      moduleDescription: [],
      moduleDescriptionValue: '',
      engineTypeValue: '',
      engineTypeValue1: '',
      engineTypeName: '',
      engineType: [],
      isDisableValue: 0,
      labelText: '',
      labelNumber: '',
      title: '',
      allowView: false,
      singleView: false,
      MID: '',
      form: {
        serialNumber: 0,
        groupNumber: 0,
        orderNumber: 0,
        sleeveNumber: 1,
        torque: '105N.m',
        positionX: 100,
        positionY: 100,
        measureWidth: 10,
        measureHeight: 10,
        measureType: 1,
        explain: ''
      },
      rules: {
      },
      dialogFormVisible4: false,
      form4: {
        engineTypeValue: '',
        stationNumberValue: ''
      },
      rules4: { // 订单下发验证
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        stationNumberValue: [{ required: true, message: '请选择工位号', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token',
      'departmentName'
    ])
  },
  watch: {
    stationNumberValue() {
      for (let i = 0; i < this.stationNumber.length; i++) {
        if (this.stationNumber[i].value === this.stationNumberValue) {
          console.log(this.stationNumber[i].label, 'this.stationNumber[i].label')
          this.stationName = this.stationNumber[i].labelName
        }
      }
      if (!this.stationNumberValue) {
        this.stationName = ''
      }
    },
    engineTypeValue() {
      for (let i = 0; i < this.engineType.length; i++) {
        if (this.engineType[i].value === this.engineTypeValue) {
          this.engineTypeValue1 = this.engineType[i].labelName
          console.log(this.engineTypeValue1)
        }
      }
    }
  },
  created() {
    this.getStationNumber()
    this.getEndStation()
  },
  methods: {
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号,
            labelName: response.data[i].工序名称
          })
        }
      })
    },
    // 初始化获取产品型号
    getEndStation() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机型号,
            labelName: response.data[i].发动机机型标志
          })
        }
      })
    },
    searchTable() {
      this.urls = []
      this.urls1 = []
      var param = []
      var engineTypeValue1 = 0
      for (let i = 0; i < this.engineType.length; i++) {
        if (this.engineType[i].value === this.engineTypeValue) {
          engineTypeValue1 = this.engineType[i].labelName
        }
      }
      param[0] = ['发动机型号', engineTypeValue1]
      param[1] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', '电子看板_工位名称与产品型号产品图片_SPC查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          console.log(response.data, 'response.data')
          if (response.data[i].产品图片) {
            this.urls.push({
              url: 'data:image/png;base64,' + response.data[i].产品图片,
              id: response.data[i].id
            })
            this.urls1.push('data:image/png;base64,' + response.data[i].产品图片)
          }
        }
      })
    },
    searchTable1() {
      this.tableData = []
      var param = []
      param[0] = ['发动机型号', this.engineTypeValue]
      param[1] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', '电子看板_工位机型_质量数据测量位置_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          console.log(response.data, 'response.data')
          if (response.data) {
            this.tableData = response.data
          }
        }
      })
    },
    copyOrder(formName) {
      if (this.tableData.length) {
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.dialogFormVisible4 = true
      } else {
        this.$message({
          message: '暂无可复制的数据！',
          type: 'error'
        })
      }
    },
    async submitAdd4() {
      let serialNumber, positionX, positionY, measureWidth, measureHeight, groupNumber, orderNumber, sleeveNumber, torque, measureType, explain
      for (let i = 0; i < this.tableData.length; i++) {
        serialNumber = this.tableData[i].序号
        positionX = this.tableData[i].测量位置X
        positionY = this.tableData[i].测量位置Y
        measureWidth = this.tableData[i].控件宽度
        measureHeight = this.tableData[i].控件高度
        groupNumber = this.tableData[i].组号
        orderNumber = this.tableData[i].工作顺序
        sleeveNumber = this.tableData[i].套筒号
        torque = this.tableData[i].扭矩
        measureType = this.tableData[i].控件类型
        explain = this.tableData[i].说明
        await this.submitAdd5(serialNumber, positionX, positionY, measureWidth, measureHeight, groupNumber, orderNumber, sleeveNumber, torque, measureType, explain)
      }
      this.$message({
        message: '增加成功',
        type: 'success'
      })
      this.dialogFormVisible4 = false
      this.engineTypeValue = this.form4.engineTypeValue
      this.stationNumberValue = this.form4.stationNumberValue
      this.searchTable()
      this.searchTable1()
    },
    async submitAdd5(serialNumber, positionX, positionY, measureWidth, measureHeight, groupNumber, orderNumber, sleeveNumber, torque, measureType, explain) {
      var param = []
      param[0] = ['工位号', this.form4.stationNumberValue]
      param[1] = ['发动机型号', this.form4.engineTypeValue]
      param[2] = ['序号', serialNumber]
      param[3] = ['测量位置X', positionX]
      param[4] = ['测量位置Y', positionY]
      param[5] = ['控件宽度', measureWidth]
      param[6] = ['控件高度', measureHeight]
      param[7] = ['组号', groupNumber]
      param[8] = ['工作顺序', orderNumber]
      param[9] = ['套筒号', sleeveNumber]
      param[10] = ['扭矩', torque]
      param[11] = ['控件类型', measureType]
      param[12] = ['说明', explain]
      console.log(param)
      var Data = this.CreateData('12', '电子看板_工位机型_质量数据测量位置_增加', param)
      return new Promise((resolve, reject) => {
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            console.log('1')
            resolve(1)
          } else {
            console.log('0')
            reject(2)
          }
        })
      })
    },
    callOff4() {
      this.dialogFormVisible4 = false
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('您正在添加拧紧位置信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            var param = []
            param[0] = ['工位号', this.stationNumberValue]
            param[1] = ['发动机型号', this.engineTypeValue]
            param[2] = ['序号', this.form.serialNumber]
            param[3] = ['测量位置X', this.form.positionX]
            param[4] = ['测量位置Y', this.form.positionY]
            param[5] = ['控件宽度', this.form.measureWidth]
            param[6] = ['控件高度', this.form.measureHeight]
            param[7] = ['组号', this.form.groupNumber]
            param[8] = ['工作顺序', this.form.orderNumber]
            param[9] = ['套筒号', this.form.sleeveNumber]
            param[10] = ['扭矩', this.form.torque]
            param[11] = ['控件类型', this.form.measureType]
            param[12] = ['说明', this.form.explain]
            console.log(param)
            var Data = this.CreateData('12', '电子看板_工位机型_质量数据测量位置_增加', param)
            this.ExecDatabase(Data).then(response => {
              if (response.data[0].result === '1') {
                this.$message({
                  message: '增加成功',
                  type: 'success'
                })
                this.dialogFormVisible = false
                this.searchTable1()
              } else {
                this.$message.error('增加失败！')
              }
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消增加'
            })
          })
        } else {
          return false
        }
      })
    },
    submitUpdate() {
      if (this.MID) {
        this.$confirm('您正在修改拧紧位置信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var param = []
          param[0] = ['工位号', this.stationNumberValue]
          param[1] = ['发动机型号', this.engineTypeValue]
          param[2] = ['序号', this.form.serialNumber]
          param[3] = ['测量位置X', this.form.positionX]
          param[4] = ['测量位置Y', this.form.positionY]
          param[5] = ['控件宽度', this.form.measureWidth]
          param[6] = ['控件高度', this.form.measureHeight]
          param[7] = ['组号', this.form.groupNumber]
          param[8] = ['工作顺序', this.form.orderNumber]
          param[9] = ['套筒号', this.form.sleeveNumber]
          param[10] = ['扭矩', this.form.torque]
          param[11] = ['控件类型', this.form.measureType]
          param[12] = ['说明', this.form.explain]
          param[13] = ['ID', this.MID]
          console.log(param)
          var Data = this.CreateData('12', '电子看板_工位机型_质量数据测量位置_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.dialogFormVisible = false
              this.searchTable1()
              this.MID = ''
            } else {
              this.$message.error('修改失败！')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          })
        })
      } else {
        this.$message.error('请选择信息后修改！')
      }
    },
    deleteInformation() {
      if (this.MID) {
        this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var param = []
          param[0] = ['ID', this.MID]
          var Data = this.CreateData('12', '电子看板_工位机型_质量数据测量位置_删除', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.searchTable1()
              this.MID = ''
            } else {
              this.$message.error('删除失败！')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.$message.error('请选择信息后删除！')
      }
    },
    viewAllow() {
      this.allowView = true
      this.singleView = false
      this.MID = ''
    },
    clearAll() {
      this.allowView = false
      this.singleView = false
      this.MID = ''
    },
    getMouseXY(e) {
      if (this.allowView) {
        return
      } else {
        // console.log(e, 'e')
        console.log(e.offsetX, e.offsetY)
        this.form.positionX = e.offsetX
        this.form.positionY = e.offsetY
      }
    },
    // 表格颜色变
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    rowClick(row) {
      this.form.serialNumber = row.序号
      this.form.groupNumber = row.组号
      this.form.orderNumber = row.工作顺序
      this.form.sleeveNumber = row.套筒号
      this.form.torque = row.扭矩
      this.form.positionX = row.测量位置X
      this.form.positionY = row.测量位置Y
      this.form.measureWidth = row.控件宽度
      this.form.measureHeight = row.控件高度
      this.form.measureType = row.控件类型
      this.form.explain = row.说明
      this.allowView = false
      this.singleView = true
      this.MID = row.ID
    },
    addTo() {
    }
  }
}
</script>

<style scoped>
  .topCard{
    margin: 5px;
  }
  .topDiv{
    margin-bottom: 10px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  .text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    position: absolute;
    right: 6px;
    top: 80px;
    width: 300px;
  }
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
  ::v-deep.el-table{
    border: 1px solid black!important;
  }
  ::v-deep.el-table th, .el-table tr{
    border-color: black!important;
  }
  ::v-deep.el-table--enable-row-transition .el-table__body td{
    border-color: black!important;
  }

  ::v-deep.el-input.is-disabled .el-input__inner{
    color: #606266!important;
  }
  ::v-deep.el-input.is-disabled .el-input__inner{
    background-color: white!important;
  }

  ::v-deep.el-input-number.is-controls-right .el-input__inner{
    padding: 0!important;
    text-align: left!important;
  }
  ::v-deep.el-input-number--mini{
    width: 70px;
  }

  ::v-deep.el-form-item{
    margin-bottom: 20px!important;
    margin-left: 10px;
  }

  ::v-deep.el-input__inner{
    padding: 0px!important;
  }
</style>

