<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">VCU码：</span>
        <el-input v-model="vcu" placeholder="请输入VCU码" clearable size="small" style="width: 200px;" />
        <span style="margin-left: 10px">产品编号：</span>
        <el-input v-model="rhythm" placeholder="请输入产品编号" clearable size="small" style="width: 200px;" />
        <span style="margin-left: 10px">刷写日期：</span>
        <el-date-picker :clearable="false" v-model="dateTime" size="small" type="daterange" align="center"
          style="width: 240px;" value-format="yyyy-MM-dd" format="yyyy-MM-dd" range-separator="～"
          start-placeholder="开始日期" end-placeholder="结束日期" />
        <el-button type="primary" plain size="small" icon="el-icon-order"
          @click="pageCurrent = 1, pageSize = 15, searchTable()">查询</el-button>
        <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="add()">新增</el-button>
      </div>
      <el-table ref="multipleTable" :data="tableData" :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        height="640px" class="main" tooltip-effect="dark" style="width: 75%" border size="mini">
        <el-table-column type="index" width="60" label="序号" align="center" />
        <el-table-column align="center" label="VCU码">
          <template slot-scope="scope">
            {{ scope.row.VCU码 }}
          </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="150">
          <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="150">
          <template slot-scope="scope">
            {{ scope.row.消耗时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button v-if="scope.row.产品编号 !== ''" type="text" icon="el-icon-delete" size="small"
              style="color: #ff0000" @click="deleteOrder(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination v-if="!loading" :current-page="pageCurrent" :page-sizes="[15, 30, 40]" :page-size="pageSize"
          :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" title="VCU刷写记录" center style="min-height: 200px;" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="80px">
        <el-form-item label="VCU码:" prop="vcu" style="display: inline-block">
          <el-input ref="vcuInput" v-model="form.vcu" :rows="3" type="textarea" size="small" placeholder="请输入/扫描VCU码"
            style="width: 230px" />
        </el-form-item>

        <el-form-item label="上线编号:" prop="workCode" style="display: inline-block">
          <el-input-number v-model="form.workCode" type="number" size="small" placeholder="请输入上线编号" style="width: 230px" />
        </el-form-item>
      </el-form>
      <div style="text-align: left;margin-left: 10px">注意：</div>
      <div style="text-align: left;">1.请使用<span style="color: #ff0000">英文输入法</span>!</div>
      <div style="text-align: left;">2.<span style="color: #ff0000">不要扫描条形码</span>！</div>
      <div style="text-align: left;">3.<span style="color: #ff0000">上线编号 不需要输入开头D 直接输入编号</span>！</div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="addRhythm('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible1" title="工艺节拍编辑" center style="min-height: 200px;" width="400px">
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-position="right" label-width="110px">
        <el-form-item label="产品型号:" prop="type" style="display: inline-block">
          <el-select v-model="editForm.type" placeholder="请选择产品型号" clearable size="small">
            <el-option v-for="item in engineType" :key="item.label" :label="item.label" :value="item.value"
              size="mini" />
          </el-select>
        </el-form-item>
        <el-form-item label="工序号:" prop="stationNumber" style="display: inline-block">
          <el-input v-model="editForm.stationNumber" readonly size="small" placeholder="请输入工序号" />
        </el-form-item>
        <el-form-item label="工位名称:" prop="name" style="display: inline-block">
          <el-input v-model="editForm.name" readonly size="small" placeholder="请输入工位名称" />
        </el-form-item>
        <el-form-item label="工位节拍(s):" prop="rhythm" style="display: inline-block">
          <el-input-number v-model="editForm.rhythm" :min="0" :step="1" size="small" placeholder="请输入工位节拍" />
        </el-form-item>
        <el-form-item label="返修节拍(s):" prop="rhythm" style="display: inline-block">
          <el-input-number v-model="editForm.rhythm1" :min="0" :step="1" size="small" placeholder="请输入返修节拍" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitEdit('editForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNowTime2 } from '../../../utils/tool'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      dateTime: [],
      vcu: '',
      rhythm: '',
      engineTypeValue: '',
      engineType: [],
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        vcu: '',
        workCode: 0
      },
      editForm: {
        id: '',
        id1: '',
        type: '',
        stationNumber: '',
        name: '',
        rhythm1: '',
        rhythm: ''
      },
      rules: {
        vcu: [{ required: true, message: '请输入/扫描VCU码', trigger: 'blur' }]
      },
      editRules: {
        rhythm: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      loading: false, // 时间
      date: new Date(),
      timer: '',
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1 // 后台获取页
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'token',
      'id' // 人员编号
    ])
  },
  created() {
    this.dateTime = [getNowTime2(), getNowTime2()]
    this.searchTable()
    // this.getProductType()
  },
  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].工位名称
          })
        }
      })
    },
    // 初始化获取产品型号
    getProductType() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'CATL_基础信息_机型信息_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) this.engineTypeValue = response.data[0].机型ID
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].机型号,
            value: response.data[i].机型ID,
            labelName: response.data[i].机型号
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询信息
    searchTable() {
      this.tableData = []
      var param = []
      param[0] = ['VCU码', this.vcu]
      param[1] = ['产品编号', this.rhythm]
      param[2] = ['开始日期', this.dateTime[0]]
      param[3] = ['结束日期', this.dateTime[1]]
      param[4] = ['PageCurrent', this.pageCurrent]
      param[5] = ['PageSize', this.pageSize]
      param[6] = ['PageCount', '1111', 'int', '1']
      param[7] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', 'CATL_VCU记录_查询_分页', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
        } else {
          this.$message.error('暂无数据')
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    add() {
      this.dialogFormVisible = true
      this.form.vcu = ''
    },
    callOff() {
      this.dialogFormVisible = false
      this.dialogFormVisible1 = false
      this.$refs['form'].resetFields()
      this.$refs['editForm'].resetFields()
    },
    // 新增节拍
    addRhythm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.vcu.length !== 33) {
            this.$message.error('VCU码长度不正确！')
            return
          }
          if (!this.form.workCode) {
            this.$message.error('请输入上线编号')
            return
          }
          var param = []
          param[0] = ['VCU码', this.form.vcu]
          param[1] = ['刷写人', this.name]
          param[2] = ['上线编号', this.form.workCode]
          var Data = this.CreateData('12', 'CATL_VCU记录_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.searchTable()
              this.form.vcu = ''
              this.$refs.vcuInput.focus()
            } else {
              this.$message.error('新增失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 编辑
    edit(row) {
      // console.log(row, 'row')
      this.dialogFormVisible1 = true
      this.editForm.id = row.ID
      this.editForm.id1 = row.ID1
      this.editForm.type = row.机型代码
      this.editForm.stationNumber = row.工位号
      this.editForm.name = row.工位名称
      this.editForm.rhythm = row.作业时间
      this.editForm.rhythm1 = row.返修时间
    },
    //  提交编辑
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['id', this.editForm.id]
          param[1] = ['基本机型', this.editForm.type]
          param[2] = ['工序节拍', this.editForm.rhythm]
          param[3] = ['返修节拍', this.editForm.rhythm1]
          param[4] = ['id1', this.editForm.id1]
          var Data = this.CreateData('12', 'CATL_基本数据_工位节拍_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('编辑失败！')
            }
          })
          this.dialogFormVisible1 = false
        } else {
          return false
        }
      })
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['VCU码', row.VCU码]
        var Data = this.CreateData('12', 'CATL_VCU记录_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</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;
}
</style>
