<template>
  <el-dialog
    :title="errorSettingId ? '编辑错误码' : '新建错误码'"
    :visible.sync="dialogVisible"
    width="500px"
    @close="cancelForm('errorForm')">
    <el-form
      ref="errorForm"
      :model="errorForm"
      :rules="modalRules"
      style="width:450px">
      <el-form-item
        label="错误码"
        label-width="90px"
        prop="errorCode">
        <dm-input
          v-model="errorForm.errorCode"
          :byte-type="1"
          type="text"
          :maxlength="20" />
      </el-form-item>
      <el-form-item
        label="错误信息"
        label-width="90px"
        prop="errorInfo">
        <dm-input
          v-model="errorForm.errorInfo"
          :byte-type="1"
          type="text"
          :maxlength="20" />
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer">
      <el-button @click="cancelForm('errorForm')">
        取消
      </el-button>
      <el-button
        type="primary"
        @click="submitForm('errorForm')">
        {{ errorSettingId ? '保存' : '新建' }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { postRequest } from '@/api/api';
export default {
  props: {
    errorSettingId: {
      type: String,
      default() {
        return '';
      }
    },
    errorCode: {
      type: String,
      default() {
        return '';
      }
    },
    errorInfo: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      dialogVisible: true,
      errorForm: {
        errorCode: '',
        errorInfo: ''
      },
      modalRules: {
        errorCode: [ { required: true, message: '请输入错误码', trigger: 'blur' } ],
        errorInfo: [ { required: true, message: '请输入错误信息', trigger: 'blur' } ]
      }
    };
  },
  created() {
    this.dialogVisible = true;
    this.errorForm.errorCode = this.errorCode;
    this.errorForm.errorInfo = this.errorInfo;
  },
  methods: {
    submitForm(errorForm) {
      let that = this;
      this.$refs[errorForm].validate(valid => {
        if (valid) {
          let url = '';
          let params = {
            errorCode: this.errorForm.errorCode,
            errorInfo: this.errorForm.errorInfo,
            errorType: 2
          };
          if (this.errorSettingId) {
            params.errorSettingId = this.errorSettingId;
            url = '/gateway-manage/update-error-setting';
          } else {
            url = '/gateway-manage/create-error-setting';
          }
          postRequest(url, params)
            .then(res => {
              if (res.data.code == '0000') {
                this.$emit('getList');
                this.dialogVisible = false;
                this.$emit('cancelNewCoce');
              } else {
                this.$message.error(res.data.message);
              }
            })
            .catch(function(error) {
              that.$message.error({
                duration: 1000,
                message: error.message
              });
            });
        } else {
          return false;
        }
      });
    },
    cancelForm(errorForm) {
      this.$refs[errorForm].resetFields();
      this.$emit('cancelNewCoce');
    }
  }
};
</script>
<style lang="less" scoped></style>
