<!-- 设备配置 -->
<template>
  <modal v-model="modal" width="500px">
    <div class="title" slot="header">
      设备配置
    </div>
    <div class="footer" slot="footer">
      <Button @click="cfm" class="btn" type="primary">确认</Button>
      <Button @click="cancel" class="btn">取消</Button>
    </div>
    <!--  表单主要内容  -->
    <div class="content" ref="print">
      <div class="row">
        <div class="col">
          <div class="label">绑定设备IP：</div>
          <div class="value" @keyup.enter="cfm">
            <Input ref="ip1" :maxlength="3" v-model="ip1" style="width: 50px;"/> -
            <Input ref="ip2" :maxlength="3" v-model="ip2" style="width: 50px;"/> -
            <Input ref="ip3" :maxlength="3" v-model="ip3" style="width: 50px;"/> -
            <Input ref="ip4" :maxlength="3" v-model="ip4" style="width: 50px;"/>
          </div>
        </div>
      </div>
    </div>
  </modal>
</template>

<script>
export default {
  name: 'ip-form',
  data () {
    return {
      modal: false,
      ip1: '',
      ip2: '',
      ip3: '',
      ip4: ''
    }
  },
  components: {
  },
  props: {
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        let ip = localStorage.getItem('ip')
        if (ip) {
          let ips = ip.split('.')
          this.ip1 = ips[0]
          this.ip2 = ips[1]
          this.ip3 = ips[2]
          this.ip4 = ips[3]
        }
        this.$nextTick(() => {
          this.$refs['ip1'].focus()
        })
      }
    },
    ip1 (n) {
      if (n.length === 3) {
        this.$refs['ip2'].focus()
      }
    },
    ip2 (n) {
      if (n.length === 3) {
        this.$refs['ip3'].focus()
      }
    },
    ip3 (n) {
      if (n.length === 3) {
        this.$refs['ip4'].focus()
      }
    }
  },
  methods: {
    cfm () {
      if (this.ip1 && this.ip2 && this.ip3 && this.ip4) {
        let ip = `${this.ip1}.${this.ip2}.${this.ip3}.${this.ip4}`
        localStorage.setItem('ip', ip)
        this.cancel()
      } else {
        this.$Message.error('请输入完整ip地址')
      }
    },
    cancel () {
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  .sub-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px;
    font-size: 14px;
    font-weight: bold;
    &.border {
      border-bottom: 1px solid #000000;
    }
    .go {
      font-size: 10px;
      font-weight: normal;
      color: #0000ff;
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: center;
      .table {
        width: 100%;
        table {
          border-collapse:collapse;
          border:none;
          width: 100%;
          tr {
            td,th {
              padding: 5px;
              text-align: center;
              border:solid #dcdee2 1px;
              position: relative;
              font-size: 12px;
              th {
                background-color: #eeeeee;
              }
            }
            &.green {
              color: #4B7902;
            }
            &.red {
              color: red;
            }
            &.orange {
              color: #B8741A;
            }
          }
        }
      }
    }
  }
  .card {
    margin-bottom: 5px;
    background-color: #FFFFFF;
    border: 1px solid rgba(215, 215, 215, 1);
    border-radius: 5px;
    .border {
      border-bottom: 1px solid #5d6c7b;
    }
    .line {
      width: 100%;
      border-bottom: 1px solid #5d6c7b;
    }
  }
}
</style>
