<template>
  <div>
    <el-form ref="bindAddressRef" :rules="rules" :model="bindAddressForm" label-width="90px">
      <el-form-item label="环境名称:" prop="namespace" style="width:400px">
        <el-select v-model="bindAddressForm.namespace" placeholder="请选择环境" style="width:310px">
          <el-option v-for="item in namespaceList" :key="item.name" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="版本号:" prop="phoneNo" placeholder="请输入版本号" style="width:400px">
        <el-input v-model="bindAddressForm.version"></el-input>
      </el-form-item>
      <el-form-item label="手机号:" prop="phoneNo" placeholder="请输入手机号" style="width:400px">
        <el-input v-model="bindAddressForm.phoneNo"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="bindAddress">确 定</el-button>
    </span>
  </div>
</template>
<script>
import { bindingAddress } from '@/api/testdata'
import { getNamespaceList } from '@/api/k8s'
export default {
  data() {
    return {
      namespaceList: [],
      bindAddressForm: {
        namespace: window.sessionStorage.getItem('env')
      },
      rules: {
        namespace: [
          {
            required: true,
            message: '环境名称不能为空！',
            trigger: 'blur'
          }
        ],
        version: [
          {
            required: true,
            message: '版本号不能为空！',
            trigger: 'blur'
          }
        ],
        phoneNo: [
          {
            required: true,
            message: '电话号码不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getNamespaceList()
  },
  methods: {
    getNamespaceList() {
      getNamespaceList({ env: 'all' }).then((resp) => {
        this.namespaceList = resp.data.data
      })
    },
    bindAddress() {
      var formData = new FormData()
      formData.set('namespace', this.bindAddressForm.namespace)
      formData.set('version', this.bindAddressForm.version)
      formData.set('phoneNo', this.bindAddressForm.phoneNo)
      this.$refs.bindAddressRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          bindingAddress(formData).then((resp) => {
            if (resp.data.success === 'true') {
              this.$message.success('地址绑定成功！')
              this.bindAddressForm.namespace = ''
              this.bindAddressForm.version = ''
              this.bindAddressForm.phoneNo = ''
            } else {
              this.$message.error(resp.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
