<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">设备IP：</span>
        <el-input v-model="equipmentIP" 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: 100%;"
        border
        size="mini">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <el-table-column align="center" label="设备IP">
          <template slot-scope="scope">
            {{ scope.row.设备IP }}
          </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="110px">
        <el-form-item label="IP" prop="equipmentIP" style="display: inline-block">
          <el-input
            v-model="form.equipmentIP"
            clearable
            size="small"
            placeholder="设备IP"/>
        </el-form-item>
        <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="enable" style="display: inline-block">
          <el-select v-model="form.enable" placeholder="请选择" size="small" style="display: inline-block">
            <el-option
              v-for="item in enableArr"
              :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>
export default {
  data() {
    return {
      equipmentIP: '',
      title: '',
      enableArr: [{
        label: '是',
        value: 1
      }, {
        label: '否',
        value: 0
      }],
      form: { // 创建订单
        equipmentIP: '',
        equipmentType: '',
        enable: '',
        ID: ''
      },
      rules: { // 创建订单验证
        equipmentIP: [{ required: true, message: '请输入', trigger: 'blur' }],
        enable: [{ required: true, message: '请选择', trigger: 'change' }],
        equipmentType: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      tableData: [],
      dialogFormVisible: false, // 创建订单
      // total: 0, // 总条数
      // pageList: 15, // 每页显示条数
      // pageSize: 15, // 每页显示条数
      // pageCurrent: 1, // 后台获取页
      loading: false
    }
  },
  watch: {
  },
  created() {
    this.searchOrder()
  },
  methods: {
    // 查询订单
    searchOrder() {
      this.loading = true
      this.tableData = []
      let equipmentIP_check
      this.equipmentIP ? (equipmentIP_check = 1) : (equipmentIP_check = 0)
      var param = []
      param[0] = ['IP_check', equipmentIP_check]
      param[1] = ['IP', this.equipmentIP]
      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.equipmentIP = ''
      this.form.equipmentType = ''
      this.form.enable = ''
      this.dialogFormVisible = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['设备IP', this.form.equipmentIP]
          param[1] = ['设备类型', this.form.equipmentType]
          param[2] = ['是否启用', this.form.enable]
          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] = ['ID', row.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('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改订单
    handleEdit(row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '编辑'
      this.form.equipmentIP = row.设备IP
      this.form.equipmentType = row.设备类型
      this.form.enable = Number(row.是否启用)
      this.form.ID = row.ID
      this.dialogFormVisible = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['设备IP', this.form.equipmentIP]
          param[1] = ['设备类型', this.form.equipmentType]
          param[2] = ['是否启用', this.form.enable]
          param[3] = ['ID', 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>

