<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" style="width: 150px">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="margin-left: 10px">工位名称：</span>
        <el-input v-model="stationName" disabled style="width:150px;" placeholder="" size="small"/>
        <span style="margin-left: 10px">设备编号：</span>
        <el-input v-model="engineTypeNumber" style="width:150px;" placeholder="设备编号" size="small"/>
        <span style="margin-left: 10px">设备类型：</span>
        <el-select v-model="engineTypeValue" placeholder="请选择设备类型" size="small" style="width: 150px">
          <el-option
            v-for="item in engineType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 10px" @click="searchOrder()">查询</el-button>
        <el-button type="distribution" size="small" icon="el-icon-circle-plus" @click="creatOrder('form')">增加</el-button>
      </div>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        class="main"
        tooltip-effect="dark"
        style="width: 100%;max-height: 700px"
        border
        size="mini"
        @selection-change="handleSelectionChange">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <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-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">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)">修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="deleteOrder(scope.row)">删除</el-button>
          </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="title" center style="min-height: 200px;" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px">
        <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">
          <el-select v-model="form.stationNumberValue" size="small" placeholder="请选择工位号">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="engineTypeValue" style="display: inline-block">
          <el-select v-model="form.engineTypeValue" size="small" placeholder="请选择设备类型">
            <el-option
              v-for="item in engineType"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编号" prop="orderNumber" style="display: inline-block">
          <el-input
            v-model="form.orderNumber"
            clearable
            size="small"
            placeholder="设备编号"/>
        </el-form-item>
        <el-form-item label="设备名称" prop="partName" style="display: inline-block">
          <el-input
            v-model="form.partName"
            clearable
            size="small"
            placeholder="设备名称"/>
        </el-form-item>
        <el-form-item label="设备型号" prop="partDrawing" style="display: inline-block">
          <el-input
            v-model="form.partDrawing"
            clearable
            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 v-if="title === '增加'" type="primary" size="small" @click="submitAdd('form')">确定</el-button>
        <el-button v-if="title === '编辑'" type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stationNumber: [],
      stationNumberValue: '',
      stationName: '',
      orderNumber: [],
      orderNumberValue: '',
      engineTypeValue: '',
      engineType: [],
      engineTypeNumber: '',
      title: '',
      form: { // 创建订单
        partName: '',
        stationNumberValue: '',
        orderNumber: '',
        engineTypeValue: '',
        partDrawing: '',
        ID: ''
      },
      rules: { // 创建订单验证
        partName: [{ required: true, message: '请输入', trigger: 'blur' }],
        stationNumberValue: [{ required: true, message: '请选择', trigger: 'change' }],
        orderNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择', trigger: 'change' }],
        partDrawing: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      tableData: [],
      dialogFormVisible: false, // 创建订单
      total: 0, // 总条数
      pageList: 15, // 每页显示条数
      pageSize: 15, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      loading: false
    }
  },
  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 = ''
      }
    }
  },
  created() {
    this.getStationNumber()
    this.getEndStation()
    this.searchOrder()
  },
  methods: {
    // 初始化获取产品型号
    getEndStation() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', '设备管理_设备类型_查询', 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].设备类型
          })
        }
      })
    },
    // 初始化获取订单号
    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++) {
          if (response.data.length) {
            this.stationNumberValue = response.data[0].工位号
            this.stationName = response.data[0].工序名称
          }
          this.stationNumber.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号,
            labelName: response.data[i].工序名称
          })
        }
      })
    },
    // 查询订单
    searchOrder() {
      this.tableData = []
      let stationNumberValue_check, engineTypeNumber_check, engineTypeValue_check
      this.stationNumberValue ? (stationNumberValue_check = 1) : (stationNumberValue_check = 0)
      this.engineTypeNumber ? (engineTypeNumber_check = 1) : (engineTypeNumber_check = 0)
      this.engineTypeValue ? (engineTypeValue_check = 1) : (engineTypeValue_check = 0)
      var param = []
      param[0] = ['工位号_ischeck', stationNumberValue_check]
      param[1] = ['工位号', this.stationNumberValue]
      param[2] = ['设备编号_ischeck', engineTypeNumber_check]
      param[3] = ['设备编号', this.engineTypeNumber]
      param[4] = ['设备类型_ischeck', engineTypeValue_check]
      param[5] = ['设备类型', this.engineTypeValue]
      param[6] = ['PageCurrent', this.pageCurrent]
      param[7] = ['PageSize', this.pageSize]
      param[8] = ['PageCount', '1111', 'int', '1']
      param[9] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '设备管理_基础表_设备台账_综合查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 创建订单
    creatOrder(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.title = '增加'
      this.form.partName = ''
      this.form.stationNumberValue = ''
      this.form.orderNumber = ''
      this.form.engineTypeValue = ''
      this.form.partDrawing = ''
      this.dialogFormVisible = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumberValue]
          param[1] = ['设备类型', this.form.engineTypeValue]
          param[2] = ['设备编号', this.form.orderNumber]
          param[3] = ['设备名称', this.form.partName]
          param[4] = ['设备型号', this.form.partDrawing]
          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.searchOrder()
            } else {
              this.$message.error('增加失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['设备流水号', row.设备流水号]
        var Data = this.CreateData('12', '设备管理_基础表_设备台账_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchOrder()
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改订单
    handleEdit(row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '编辑'
      this.form.partName = row.设备名称
      this.form.orderNumber = row.设备编号
      this.form.partDrawing = row.设备型号
      this.form.engineTypeValue = row.设备类型
      this.form.stationNumberValue = row.工位号
      this.form.ID = row.设备流水号
      this.dialogFormVisible = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumberValue]
          param[1] = ['设备类型', this.form.engineTypeValue]
          param[2] = ['设备编号', this.form.orderNumber]
          param[3] = ['设备名称', this.form.partName]
          param[4] = ['设备型号', this.form.partDrawing]
          param[5] = ['设备流水号', this.form.ID]
          var Data = this.CreateData('12', '设备管理_基础表_设备台账_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchOrder()
            } else {
              this.$message.error('修改失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 多选
    handleSelectionChange(val) {
      this.MIDGroup = []
      for (let i = 0; i < val.length; i++) {
        this.MIDGroup.push(val[i].MID)
      }
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchOrder()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchOrder()
    }
  }
}
</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;
  }

  /deep/.el-input.is-disabled .el-input__inner{
    color: #606266!important;
  }
  /deep/.el-input.is-disabled .el-input__inner{
    background-color: white!important;
  }
</style>

