<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-col :span="16">
        <div class="topDiv">
<!--          <span style="margin-left: 10px">物料号：</span>-->
<!--          <el-input v-model="equipmentType" style="width:150px;" clearable placeholder="物料号" size="small"/>-->
          <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'}"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          highlight-current-row
          class="main"
          tooltip-effect="dark"
          height="790px"
          style="width: 97%;"
          border
          size="mini">
          <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="操作" 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"
                style="color: #ff0000"
                @click="deleteOrder(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" title="新增机型" center style="min-height: 200px;" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="120px">
        <el-form-item label="物料号" prop="equipmentType" style="display: inline-block">
          <el-input
            v-model="form.engineType"
            clearable
            size="small"
            placeholder="物料号"/>
        </el-form-item>
        <el-form-item label="物料名" prop="equipmentTypeName" style="display: inline-block">
          <el-input
            v-model="form.engineTypeName"
            clearable
            size="small"
            placeholder="物料名"/>
        </el-form-item>
        <el-form-item label="产品类型" prop="bigEngineType" style="display: inline-block">
          <el-select v-model="form.bigEngineType" placeholder="产品类型" size="small" style="display: inline-block">
            <el-option
              v-for="item in bigEngineTypeList"
              :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="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible2" title="编辑机型" center style="min-height: 200px;" width="400px">
      <el-form ref="form2" :model="form" :rules="rules" label-position="right" label-width="120px">
        <el-form-item label="物料号" prop="equipmentType" style="display: inline-block">
          <el-input
            v-model="form.engineType"
            clearable
            size="small"
            placeholder="物料号"/>
        </el-form-item>
        <el-form-item label="物料名" prop="equipmentTypeName" style="display: inline-block">
          <el-input
            v-model="form.engineTypeName"
            clearable
            size="small"
            placeholder="物料名"/>
        </el-form-item>
        <el-form-item label="产品类型" prop="bigEngineType" style="display: inline-block">
          <el-select v-model="form.bigEngineType" placeholder="产品类型" size="small" style="display: inline-block">
            <el-option
              v-for="item in bigEngineTypeList"
              :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="callOff('form2')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      equipmentType: '',
      equipmentType1: '', // 轮毂
      equipmentType2: '', // 桥壳
      title: '',
      title1: '',
      boxSign: [],
      tonnageSign: [],
      bigEngineTypeList:[{label:'1T',value:1},{label:'3T',value:3}],
      LunGuList: [],
      QiaoKeList: [],
      disableArr: [
        {
          label: '是',
          value: 1
        }, {
          label: '否',
          value: 0
        }
      ],
      form: { // 创建订单
        engineType:'',
        engineTypeName:'',
        bigEngineType:'',
        ID: ''
      },
      rules: { // 创建订单验证
        equipmentSign: [{ required: true, message: '请输入总装线配方号', trigger: 'blur' }],
        equipmentSign2: [{ required: true, message: '请输入轮毂线配方号', trigger: 'blur' }],
        bigEngineType: [{ required: true, message: '请选择产品类型', trigger: 'blur' }],
        enDisable: [{ required: true, message: '请选择', trigger: 'change' }],
        // tonnageSign: [{ required: true, message: '请选择', trigger: 'change' }],
        // boxSign: [{ required: true, message: '请选择', trigger: 'change' }],
        engineType: [{ required: true, message: '请输入物料号', trigger: 'blur' }],
        engineTypeName: [{ required: true, message: '请输入物料名', trigger: 'blur' }],
        equipmentTypeName2: [{ required: true, message: '请输入物料名简称', trigger: 'blur' }],
        formula: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      form2: { // 创建订单
        equipmentType: '',
        equipmentTypeName: '',
        equipmentTypeName2: '',
        equipmentSign: 1,
        equipmentSign2: '',
        equipmentSign3: '',
        itemNo: '',
        boxSign: '',
        tonnageSign: '',
        enDisable: 0,
        formula: '',
        ID: ''
      },
      rules2: { // 创建订单验证
        equipmentSign: [{ required: true, message: '请输入总装线配方号', trigger: 'blur' }],
        equipmentSign2: [{ required: true, message: '请输入轮毂线配方号', trigger: 'blur' }],
        enDisable: [{ required: true, message: '请选择', trigger: 'change' }],
        // tonnageSign: [{ required: true, message: '请选择', trigger: 'change' }],
        // boxSign: [{ required: true, message: '请选择', trigger: 'change' }],
        equipmentType: [{ required: true, message: '请输入物料号', trigger: 'blur' }],
        equipmentTypeName: [{ required: true, message: '请输入物料名', trigger: 'blur' }],
        equipmentTypeName2: [{ required: true, message: '请输入物料名简称', trigger: 'blur' }],
        formula: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      form1: { // 创建订单
        equipmentType: 1,
        equipmentTypeName: '',
        ID: ''
      },
      rules1: { // 创建订单验证
        equipmentType: [{ required: true, message: '请输入轮毂配方号', trigger: 'blur' }],
        equipmentTypeName: [{ required: true, message: '请输入轮毂号', trigger: 'blur' }]
      },
      form3: { // 创建订单
        equipmentType: 1,
        equipmentTypeName: '',
        ID: ''
      },
      rules3: { // 创建订单验证
        equipmentType: [{ required: true, message: '请输入轮毂配方号', trigger: 'blur' }],
        equipmentTypeName: [{ required: true, message: '请输入轮毂号', trigger: 'blur' }]
      },
      tableData: [],
      tableData1: [],
      tableData2: [],
      dialogFormVisible: false, // 创建订单
      dialogFormVisible1: false, // 创建订单
      dialogFormVisible2: false, // 创建订单
      dialogFormVisible3: false, // 创建订单
      // total: 0, // 总条数
      // pageList: 15, // 每页显示条数
      // pageSize: 15, // 每页显示条数
      // pageCurrent: 1, // 后台获取页
      loading: false,
      loading1: false,
      loading2: false

    }
  },
  watch: {
  },
  created() {
    // this.getBox()
    // this.getTonnageSign()
    this.searchOrder()
    this.searchOrder1()
    this.searchOrder2()


  },
  methods: {
    getBox() {
      this.boxSign = []
      request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `SELECT [箱体标志],[箱体名称] FROM [MES_基本变量_机型代码_箱体标志]`
        }
      }).then(response => {
        if (response.data.length) {
          for (let i = 0; i < response.data.length; i++) {
            this.boxSign.push({
              label: response.data[i].箱体名称,
              value: response.data[i].箱体标志
            })
          }
        }
      })
    },
    getTonnageSign() {
      this.tonnageSign = []
      request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `SELECT [吨位标志],[吨位名称]FROM [MES_基本变量_机型代码_吨位标志]`
        }
      }).then(response => {
        if (response.data.length) {
          for (let i = 0; i < response.data.length; i++) {
            this.tonnageSign.push({
              label: response.data[i].吨位名称,
              value: response.data[i].吨位标志
            })
          }
        }
      })
    },
    // 查询订单
    searchOrder() {
      this.loading = true
      this.tableData = []
      let equipmentType_check
      this.equipmentType ? (equipmentType_check = 1) : (equipmentType_check = 0)
      var param = []
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
        this.loading = false
      })
    },
    // 查询订单
    searchOrder1() {
      this.loading1 = true
      this.tableData1 = []
      this.LunGuList = []
      let equipmentType_check
      this.equipmentType1 ? (equipmentType_check = 1) : (equipmentType_check = 0)
      var param = []
      param[0] = ['轮毂型号_check', equipmentType_check]
      param[1] = ['轮毂型号', this.equipmentType1]
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询数据_轮毂', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData1 = response.data
          for (let i = 0; i < response.data.length; i++) {
            this.LunGuList.push({
              label: response.data[i]['机型代码_轮毂'],
              value: response.data[i]['机型代码_轮毂']
            })
          }
        }
        this.loading1 = false
      })
    },
    searchOrder2() {
      this.loading2 = true
      this.tableData2 = []
      this.QiaoKeList = []
      let equipmentType_check
      this.equipmentType2 ? (equipmentType_check = 1) : (equipmentType_check = 0)
      var param = []
      param[0] = ['桥壳型号_check', equipmentType_check]
      param[1] = ['桥壳型号', this.equipmentType2]
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询数据_桥壳', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData2 = response.data
          for (let i = 0; i < response.data.length; i++) {
            this.QiaoKeList.push({
              label: response.data[i]['机型代码_桥壳'],
              value: response.data[i]['机型代码_桥壳']
            })
          }
        }
        this.loading2 = false
      })
    },
    // 创建订单
    creatOrder(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form.engineType = ''
      this.form.engineTypeName = ''
      this.form.bigEngineType = ''
      this.form.ID = 0
      this.dialogFormVisible = true
    },
    // 创建订单
    creatOrder1(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.dialogFormVisible1 = true
    },
    // 创建订单
    creatOrder2(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.dialogFormVisible3 = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['物料号', this.form.engineType]
          param[1] = ['物料名称', this.form.engineTypeName]
          param[2] = ['生产类型', this.form.bigEngineType]
          var Data = this.CreateData('11', 'MES_基本变量_机型代码_增加数据', 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('增加失败，有重复型号！！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff(formName) {
      this.dialogFormVisible = false
      this.dialogFormVisible2 = false
      this.dialogFormVisible3 = false
      this.$refs[formName].resetFields()
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['序号', row.序号]
        var Data = this.CreateData('12', 'MES_基本变量_机型代码_删除数据', 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.form.engineType = row.工作中心
      this.form.engineTypeName = row.标识字符
      this.form.bigEngineType = row.发动机机型标志
      this.form.ID = row.序号
      this.dialogFormVisible2 = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['物料号', this.form.engineType]
          param[1] = ['物料名称', this.form.engineTypeName]
          param[2] = ['生产类型', this.form.bigEngineType]
          param[3] = ['序号', this.form.ID]
          var Data = this.CreateData('12', 'MES_基本变量_机型代码_编辑数据', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchOrder()
            } else {
              this.$message.error('修改失败！')
            }
          })
          this.dialogFormVisible2 = false
        } else {
          return false
        }
      })
    },
    // 提交新增订单
    submitAdd_Lungu(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['轮毂型号', this.form1.equipmentTypeName]
          param[1] = ['轮毂机型标志', this.form1.equipmentType]
          var Data = this.CreateData('11', 'MES_基本变量_机型代码_增加数据_轮毂', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.dialogFormVisible1 = false
              this.searchOrder1()
            } else {
              this.$message.error('增加失败，有重复型号！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff_Lungu(formName) {
      this.dialogFormVisible1 = false
      this.$refs[formName].resetFields()
    },
    // 删除订单
    deleteOrder_Lungu(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['序号', row.机型代码_轮毂]
        var Data = this.CreateData('11', 'MES_基本变量_机型代码_删除数据_轮毂', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchOrder1()
          } else {
            this.$message.error('删除失败，已被使用！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 提交新增订单
    submitAdd_QiaoKe(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['桥壳型号', this.form3.equipmentTypeName]
          param[1] = ['桥壳机型标志', this.form3.equipmentType]
          var Data = this.CreateData('11', 'MES_基本变量_机型代码_增加数据_桥壳', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.dialogFormVisible3 = false
              this.searchOrder2()
            } else {
              this.$message.error('增加失败，有重复型号！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff_QiaoKe(formName) {
      this.dialogFormVisible3 = false
      this.$refs[formName].resetFields()
    },
    // 删除订单
    deleteOrder_QiaoKe(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['序号', row.机型代码_桥壳]
        var Data = this.CreateData('11', 'MES_基本变量_机型代码_删除数据_桥壳', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchOrder2()
          } else {
            this.$message.error('删除失败，已被使用！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // // 分页
    // handleSizeChange(val) {
    //   this.pageCurrent = 1
    //   this.pageSize = val
    //   this.searchOrder()
    // },
    handleCurrentChange(val) {
      console.log(val)
      this.$refs.LunGuTable.setCurrentRow()
      this.$refs.QiaoKeTable.setCurrentRow()

      // 轮毂
      for (let item of this.tableData1){
        if (item.ID == val.机型代码_轮毂){
          console.log("轮毂匹配",item.ID,val.机型代码_轮毂)
          this.$refs.LunGuTable.setCurrentRow(item)
        }
      }

      // 桥壳
      for (let item of this.tableData2){
        if (item.ID == val.机型代码_桥壳){
          console.log("桥壳匹配",item.ID,val.机型代码_桥壳)
          this.$refs.QiaoKeTable.setCurrentRow(item)
        }
      }
    }
  }
}
</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>

