<!--
<new-env
  :editRow="editRow"
  :showDialog="showDialog"
  @hideDialog="hideDialog">
</new-env>

import newEnv from '@/components/env/new-env.vue'
 -->
<template>
  <div v-show="customDialog">
    <el-dialog
      class="edit-dialog"
      :title="editRow.sceneCode ? '编辑环境' : '新建环境'"
      :visible.sync="customDialog"
      width="600px"
      :before-close="handleClose">
      <div class="edit-dialog-body">
        <el-form
          ref="envForm"
          :model="formData"
          label-width="94px"
          :rules="rules">
          <el-form-item
            label="环境名称:"
            prop="sceneName">
            <limit-input
              :input-width="400"
              :input-value.sync="formData.sceneName"
              :holder="'请输入环境名称'"
              :get-by-type="'word'"
              :max-length="20" />
          </el-form-item>
          <el-form-item
            label="环境code:"
            prop="sceneCode">
            <limit-input
              :input-width="400"
              :disflag="editRow.sceneCode ? true : false"
              :input-value.sync="formData.sceneCode"
              :holder="'请输入环境code'"
              :get-by-type="'word'"
              :max-length="20" />
          </el-form-item>
          <el-form-item
            label="环境域名:"
            prop="domainUrl">
            <limit-input
              :input-width="400"
              :input-value.sync="formData.domainUrl"
              :holder="'请输入环境域名'"
              :get-by-type="'word'"
              :max-length="50" />
          </el-form-item>
          <el-form-item label="默认环境:">
            <el-checkbox v-model="formData.defaultFlag">
              设置为默认环境
            </el-checkbox>
          </el-form-item>
        </el-form>
      </div>
      <div
        slot="footer"
        class="dialog-footer"
        style="padding-bottom: 10px;">
        <el-button @click="customCancel">
          取 消
        </el-button>
        <el-button
          type="primary"
          @click="customConfirm('envForm')">
          {{ editRow.sceneCode ? '保 存' : '新 建' }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import limitInput from '@/components/limit-input.vue';
import { getRequest } from '@/api/api';
import showMsg from '@/assets/js/showmsg';
import errMsg from '@/assets/js/error';
export default {
  name: 'NewEnv',
  components: {
    limitInput
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    editRow: {
      type: Object,
      default() {
        return {
          sceneName: '',
          sceneCode: '',
          domainUrl: '',
          defaultFlag: false
        };
      }
    }
  },
  data() {
    return {
      customDialog: false, // 弹框显示
      formData: {
        sceneName: '',
        sceneCode: '',
        domainUrl: '',
        defaultFlag: false
      },
      rules: {
        sceneName: [ { required: true, message: '请输入环境名称', trigger: 'blur' } ],
        sceneCode: [ { required: true, message: '请输入环境code', trigger: 'blur' } ],
        domainUrl: [ { required: true, message: '请输入环境域名', trigger: 'blur' } ]
      }
    };
  },

  /* 接收数据 */
  mounted() {
    const that = this;
    that.customDialog = that.showDialog;
    if (Object.keys(that.editRow).length) {
      that.formData = JSON.parse(JSON.stringify(that.editRow));
    }
  },
  methods: {
    /**
     * @description: 关闭
     * @param {Function} done
     * @author: 无尘
     */
    handleClose(done) {
      const that = this;
      that.$refs['envForm'].resetFields();
      done();
      that.hideDialog();
    },

    /**
     * @description: 取消
     * @author: 无尘
     */
    customCancel() {
      const that = this;
      that.$refs['envForm'].resetFields();
      that.hideDialog();
    },

    hideDialog() {
      const that = this;
      that.customDialog = false;
      that.formData.sceneName = '';
      that.formData.sceneCode = '';
      that.formData.domainUrl = '';
      that.$emit('hideDialog', '');
      /* that.$nextTick(() => {
        that.formData.classifyName = '';
      }); */
    },

    /**
     * @description: 确定
     * @param {String} formName
     * @author: 无尘
     */
    customConfirm(formName) {
      const that = this;
      that.formData.sceneName = that.formData.sceneName ? String(that.formData.sceneName).replace(/\s+/g, '') : '';
      that.formData.sceneCode = that.formData.sceneCode ? String(that.formData.sceneCode).replace(/\s+/g, '') : '';
      that.formData.domainUrl = that.formData.domainUrl ? String(that.formData.domainUrl).replace(/\s+/g, '') : '';
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.postEditData();
        } else {
          return false;
        }
      });
    },
    postEditData() {
      const that = this;
      let para = {
        sceneName: that.formData.sceneName,
        sceneCode: that.formData.sceneCode,
        domainUrl: that.formData.domainUrl,
        defaultFlag: that.formData.defaultFlag ? '1' : '0'
      };
      getRequest(that.editRow.sceneCode ? '/gateway-manage/update-scene' : '/gateway-manage/create-scene', para)
        .then(res => {
          let resData = res.data;
          if (resData.code == '0000') {
            showMsg.showmsg('保存成功', 'success');
            that.setData();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * @description: 提交数据
     * @author: 无尘
     */
    setData() {
      const that = this;
      that.$emit('hideDialog', that.formData);
      that.$nextTick(() => {
        that.formData.sceneName = '';
        that.formData.sceneCode = '';
        that.formData.domainUrl = '';
      });
    }
  },
  watch: {
    showDialog: function(newData, oldData) {
      const that = this;
      that.customDialog = newData;
    },
    editRow: function(newData, oldData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.formData = JSON.parse(JSON.stringify(newData));
      }
    }
  }
};
</script>

<style lang="less" scoped>
.edit-dialog {
  /deep/ .el-dialog__body {
    padding: 2px 55px 0 34px;
  }

  /deep/ .el-dialog__footer {
    border: none;
    padding-top: 3px;
  }

  &__title {
    width: 100%;
    height: 38px;
    padding: 12px 15px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background: rgba(235, 238, 245, 1);
  }

  .el-alert--info {
    width: 400px;
    font-size: 14px;
    color: #606266;
    background: #e6f7ff;
    border: 1px solid rgba(145, 213, 255, 1);
  }
  .el-icon-info {
    width: 12px;
    font-size: 12px;
    color: #1890ff;
  }
  .el-form {
    margin-top: 40px;
  }
}
</style>
