<template>
  <el-dialog
    :title="!dataForm.edgenodesId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="edgenodesName">
      <el-input v-model="dataForm.edgenodesName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="edgenodesStatus">
      <!-- <el-input v-model="dataForm.edgenodesStatus" placeholder="状态"></el-input> -->
      <el-select
          label="状态"
          v-model="dataForm.edgenodesStatus"
        >
          <el-option
            v-for="data in edgenodesStatusOption"
            :key="data.type"
            :label="data.label"
            :value="data.type"
          ></el-option>
        </el-select>
    </el-form-item>
    <!-- <el-form-item label="角色" prop="edgenodesRole">
      <el-input v-model="dataForm.edgenodesRole" placeholder="角色"></el-input>
    </el-form-item> -->
    <el-form-item label="CPU核数" prop="cpu">
      <el-input v-model="dataForm.cpu" placeholder="CPU核数"></el-input>
    </el-form-item>
    <el-form-item label="内存" prop="memory">
      <el-input v-model="dataForm.memory" placeholder="内存"></el-input>
    </el-form-item>
    <!-- <el-form-item label="容器组" prop="edgenodesPods">
      <el-input v-model="dataForm.edgenodesPods" placeholder="容器组"></el-input>
    </el-form-item>
    <el-form-item label="已分配CPU" prop="allocatedCpu">
      <el-input v-model="dataForm.allocatedCpu" placeholder="已分配CPU"></el-input>
    </el-form-item>
    <el-form-item label="已分配内存" prop="allocatedMemory">
      <el-input v-model="dataForm.allocatedMemory" placeholder="已分配内存"></el-input>
    </el-form-item> -->
    <el-form-item label="边缘节点IP" prop="edgenodesIp">
      <el-input v-model="dataForm.edgenodesIp" placeholder="边缘节点IP"></el-input>
    </el-form-item>
    <el-form-item label="操作系统(具体的类型 如：centos7)" prop="osImage">
      <el-input v-model="dataForm.osImage" placeholder="操作系统(具体的类型 如：centos7)"></el-input>
    </el-form-item>
    <!-- <el-form-item label="操作系统类型(类似linux、mac、win)" prop="operatingSystem">
      <el-input v-model="dataForm.operatingSystem" placeholder="操作系统类型(类似linux、mac、win)"></el-input>
    </el-form-item>
    <el-form-item label="内核版本" prop="kernelVersion">
      <el-input v-model="dataForm.kernelVersion" placeholder="内核版本"></el-input>
    </el-form-item>
    <el-form-item label="容器版本" prop="containerRuntimeVersion">
      <el-input v-model="dataForm.containerRuntimeVersion" placeholder="容器版本"></el-input>
    </el-form-item>
    <el-form-item label="Kubelet版本" prop="kubeletVersion">
      <el-input v-model="dataForm.kubeletVersion" placeholder="Kubelet版本"></el-input>
    </el-form-item>
    <el-form-item label="系统架构" prop="architecture">
      <el-input v-model="dataForm.architecture" placeholder="系统架构"></el-input>
    </el-form-item> 
    <el-form-item label="创建时间" prop="createdTime">
      <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          edgenodesName: '',
          edgenodesStatus: '',
          edgenodesId: 0,
          edgenodesRole: '',
          cpu: '',
          memory: '',
          edgenodesPods: '',
          allocatedCpu: '',
          allocatedMemory: '',
          edgenodesIp: '',
          osImage: '',
          operatingSystem: '',
          kernelVersion: '',
          containerRuntimeVersion: '',
          kubeletVersion: '',
          architecture: '',
          createdTime: ''
        },
        dataRule: {
          edgenodesName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          edgenodesStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          
          cpu: [
            { required: true, message: 'CPU不能为空', trigger: 'blur' }
          ],
          memory: [
            { required: true, message: '内存不能为空', trigger: 'blur' }
          ],
          
          edgenodesIp: [
            { required: true, message: '边缘节点IP不能为空', trigger: 'blur' }
          ],
          osImage: [
            { required: true, message: '操作系统(具体的类型 如：centos7)不能为空', trigger: 'blur' }
          ],
          
          createdTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        },
        edgenodesStatusOption:[
          {type:'1',label:'在线'},
          {type:'2',label:'离线'}
        ]
      }
    },
    methods: {
      init (id) {
        this.dataForm.edgenodesId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.edgenodesId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/edgenodes/info/${this.dataForm.edgenodesId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.edgenodesName = data.edgenodes.edgenodesName
                this.dataForm.edgenodesStatus = data.edgenodes.edgenodesStatus
                this.dataForm.edgenodesRole = data.edgenodes.edgenodesRole
                this.dataForm.cpu = data.edgenodes.cpu
                this.dataForm.memory = data.edgenodes.memory
                this.dataForm.edgenodesPods = data.edgenodes.edgenodesPods
                this.dataForm.allocatedCpu = data.edgenodes.allocatedCpu
                this.dataForm.allocatedMemory = data.edgenodes.allocatedMemory
                this.dataForm.edgenodesIp = data.edgenodes.edgenodesIp
                this.dataForm.osImage = data.edgenodes.osImage
                this.dataForm.operatingSystem = data.edgenodes.operatingSystem
                this.dataForm.kernelVersion = data.edgenodes.kernelVersion
                this.dataForm.containerRuntimeVersion = data.edgenodes.containerRuntimeVersion
                this.dataForm.kubeletVersion = data.edgenodes.kubeletVersion
                this.dataForm.architecture = data.edgenodes.architecture
                this.dataForm.createdTime = data.edgenodes.createdTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/edgenodes/${!this.dataForm.edgenodesId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'edgenodesName': this.dataForm.edgenodesName,
                'edgenodesStatus': this.dataForm.edgenodesStatus,
                'edgenodesId': this.dataForm.edgenodesId || undefined,
                'edgenodesRole': this.dataForm.edgenodesRole,
                'cpu': this.dataForm.cpu,
                'memory': this.dataForm.memory,
                'edgenodesPods': this.dataForm.edgenodesPods,
                'allocatedCpu': this.dataForm.allocatedCpu,
                'allocatedMemory': this.dataForm.allocatedMemory,
                'edgenodesIp': this.dataForm.edgenodesIp,
                'osImage': this.dataForm.osImage,
                'operatingSystem': this.dataForm.operatingSystem,
                'kernelVersion': this.dataForm.kernelVersion,
                'containerRuntimeVersion': this.dataForm.containerRuntimeVersion,
                'kubeletVersion': this.dataForm.kubeletVersion,
                'architecture': this.dataForm.architecture,
                'createdTime': this.dataForm.createdTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
