<template>
  <div class="right-box right-box-cabinet" v-clickoutside="clickOutside">
    <!-- begin--顶部按钮-->
    <div class="right-box-top-btns right-box-form-delete">
      <button @click="del" class="nz-btn nz-btn-size-normal nz-btn-size-alien" id="cab-del" type="button" v-has="'dc_cabinet_delete'" v-if="!editCabinet.id">
        <span class="right-box-top-btn-icon"><i class="nz-icon nz-icon-delete"></i></span>
        <span class="right-box-top-btn-txt">{{$t('overall.delete')}}</span>
      </button>
    </div>
    <!-- end--顶部按钮-->

    <!-- begin--标题-->
    <div class="right-box-title">{{editCabinet.id ? $t("config.dc.editCabinet") + " ID: " + editCabinet.id : $t("overall.createCabinet")}}</div>
    <!-- end--标题-->

    <!-- begin--表单-->
    <el-scrollbar class="right-box-form-box">
      <el-form class="right-box-form right-box-form-left" label-width="120px" label-position="right" :model="editCabinet" ref="cabinetForm" :rules="rules">
        <el-form-item :label="$t('overall.name')" prop="name">
          <el-input size='mini' v-model="editCabinet.name"/>
        </el-form-item>
        <el-form-item :label="$t('config.dc.dc')" prop="idcId">
          <el-input size='mini' v-model="currentDc.name" disabled/>
        </el-form-item>
        <el-form-item :label="$t('asset.uSize')" prop="uSize">
          <el-input v-model.number="editCabinet.uSize" :max="47" ></el-input>
        </el-form-item>
        <el-form-item :label="$t('config.dc.remark')">
          <el-input size='mini' v-model="editCabinet.remark" type="textarea" :rows="2"/>
        </el-form-item>
      </el-form>
    </el-scrollbar>

    <!--底部按钮-->
    <div class="right-box-bottom-btns">
      <button @click="esc" id="cab-box-esc" class="nz-btn nz-btn-size-normal nz-btn-style-light nz-btn-min-width-100">
        <span>{{$t('overall.cancel')}}</span>
      </button>
      <button @click="save" class="nz-btn nz-btn-size-normal nz-btn-style-normal nz-btn-min-width-100" id="cab-box-save" v-has="'dc_cabinet_save'">
        <span>{{$t('overall.save')}}</span>
      </button>
    </div>
  </div>
</template>

<script>
    export default {
      name: "cabinetBox",
      props:{
        cabinet: {type:Object},
        currentDc: {type:Object}
      },
      data(){
        return {
          editCabinet:{},
          rules:{
            name: [
              {required: true, message: this.$t('validate.required'), trigger: 'blur'}
            ],
            uSize: [
              {required: true,  message: this.$t('validate.required'), trigger: 'blur'},
              {type: 'number', min: 1, max: 47,message: this.$t('validate.uSize'),trigger: 'blur'}
            ],
          },
        }
      },
      methods:{
        clickOutside() {
          this.esc(false);
        },
        esc(refresh) {
          this.$emit("close", refresh);
        },
        save() {
          this.$refs.cabinetForm.validate((valid => {
            if(valid){
              this.editCabinet.idcId = this.currentDc.id;
              if (this.editCabinet.id) {
                this.$put('cabinet', this.editCabinet).then(res => {
                  if (res.code === 200) {
                    this.$message({duration: 2000, type: 'success', message: this.$t("tip.saveSuccess")});
                    this.esc(true);
                  } else {
                    this.$message.error(res.msg);
                  }
                })
              } else {
                this.$post('cabinet', this.editCabinet).then(res => {
                  if (res.code === 200) {
                    this.$message({duration: 2000, type: 'success', message: this.$t("tip.saveSuccess")});
                    this.esc(true);
                  } else {
                    this.$message.error(res.msg);
                  }
                })
              }
            }else{
              return false;
            }
          }))
        },
        del() {
          this.$confirm(this.$t("tip.confirmDelete"), {
            confirmButtonText: this.$t("tip.yes"),
            cancelButtonText: this.$t("tip.no"),
            type: 'warning'
          }).then(() => {
            this.$delete('/cabinet?ids=' + this.editCabinet.id).then(response=>{
              if(response.code ==  200){
                this.$message({duration: 2000, type: 'success', message: this.$t("tip.saveSuccess")});
                this.esc(true);
              }else{
                this.$message.error(response.msg);
              }
            })
          })
        }
      },
      watch:{
        cabinet:{
          deep:true,
          immediate:true,
          handler:function(n,o){
            if (n) {
              this.editCabinet = JSON.parse(JSON.stringify(n));
            }
          }
        },
      }
    }
</script>
<style >
</style>
