<template>
  <div class="app-container">
    <el-card class="topCard">
      <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)"
        class="main"
        tooltip-effect="dark"
        height="790px"
        style="width: 60%;"
        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="规格(大机型)">-->
        <!--          <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">
            <span v-if="Number(scope.row.是否禁用) === 1"> 是 </span>
            <span v-if="Number(scope.row.是否禁用) === 0"> 否 </span>
          </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="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.equipmentType"
            clearable
            size="small"
            placeholder="发动机型号"/>
        </el-form-item>
        <el-form-item label="发动机机型标志" prop="equipmentSign" style="display: inline-block">
          <el-input
            v-model="form.equipmentSign"
            clearable
            size="small"
            placeholder="发动机机型标志"/>
        </el-form-item>
        <el-form-item label="总成物料号" prop="itemNo" style="display: inline-block">
          <el-input
            v-model="form.itemNo"
            clearable
            size="small"
            placeholder="总成物料号"/>
        </el-form-item>
        <!--        <el-form-item label="规格(大机型)" prop="boxSign" style="display: inline-block">-->
        <!--          <el-select v-model="form.boxSign" placeholder="请选择" size="small" style="display: inline-block">-->
        <!--            <el-option-->
        <!--              v-for="item in boxSign"-->
        <!--              :key="item.value"-->
        <!--              :label="item.label"-->
        <!--              :value="item.value"-->
        <!--              size="mini"/>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="型号(小机型)" prop="tonnageSign" style="display: inline-block">-->
        <!--          <el-select v-model="form.tonnageSign" placeholder="请选择" size="small" style="display: inline-block">-->
        <!--            <el-option-->
        <!--              v-for="item in tonnageSign"-->
        <!--              :key="item.value"-->
        <!--              :label="item.label"-->
        <!--              :value="item.value"-->
        <!--              size="mini"/>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="是否禁用" prop="enDisable" style="display: inline-block">
          <el-select v-model="form.enDisable" placeholder="请选择" size="small" style="display: inline-block">
            <el-option
              v-for="item in disableArr"
              :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 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>
import request from '@/utils/request'
export default {
  data() {
    return {
      equipmentType: '',
      title: '',
      boxSign: [],
      tonnageSign: [],
      disableArr: [{
        label: '是',
        value: 1
      }, {
        label: '否',
        value: 0
      }],
      form: { // 创建订单
        equipmentType: '',
        equipmentSign: '',
        itemNo: '',
        boxSign: '',
        tonnageSign: '',
        enDisable: 0,
        formula: '',
        ID: ''
      },
      rules: { // 创建订单验证
        equipmentSign: [{ 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' }],
        formula: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      tableData: [],
      dialogFormVisible: false, // 创建订单
      // total: 0, // 总条数
      // pageList: 15, // 每页显示条数
      // pageSize: 15, // 每页显示条数
      // pageCurrent: 1, // 后台获取页
      loading: false
    }
  },
  watch: {
  },
  created() {
    this.getBox()
    this.getTonnageSign()
    this.searchOrder()
  },
  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 = []
      param[0] = ['发动机型号_check', equipmentType_check]
      param[1] = ['发动机型号', this.equipmentType]
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
        this.loading = false
      })
    },
    // 创建订单
    creatOrder(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.title = '增加'
      this.form.equipmentType = ''
      this.form.formula = ''
      this.form.equipmentSign = ''
      this.form.itemNo = ''
      this.form.boxSign = ''
      this.form.tonnageSign = ''
      this.form.enDisable = 0
      this.dialogFormVisible = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['发动机型号', this.form.equipmentType]
          param[1] = ['发动机机型标志', this.form.equipmentSign]
          param[2] = ['总成物料号', this.form.itemNo]
          param[3] = ['箱体标志', this.form.boxSign]
          param[4] = ['吨位标志', this.form.tonnageSign]
          param[5] = ['是否禁用', this.form.enDisable]
          var Data = this.CreateData('12', '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('增加失败！')
            }
          })
          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', '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.title = '编辑'
      this.form.equipmentType = row.发动机型号
      this.form.equipmentSign = row.发动机机型标志
      this.form.itemNo = row.总成物料号
      this.form.boxSign = Number(row.箱体标志)
      this.form.tonnageSign = Number(row.吨位标志)
      this.form.enDisable = Number(row.是否禁用)
      this.form.formula = row.配方号
      this.form.ID = row.序号
      this.dialogFormVisible = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['发动机型号', this.form.equipmentType]
          param[1] = ['发动机机型标志', this.form.equipmentSign]
          param[2] = ['总成物料号', this.form.itemNo]
          param[3] = ['箱体标志', this.form.boxSign]
          param[4] = ['吨位标志', this.form.tonnageSign]
          param[5] = ['是否禁用', this.form.enDisable]
          param[6] = ['序号', 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.dialogFormVisible = false
        } else {
          return false
        }
      })
    }
    // // 分页
    // 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>

