<template>
  <el-dialog
    class="dialog-article-type"
    :visible="visible"
    :before-close="e => hideDialog(false)"
    :title="isEdit ? 'Edit Article Type' : 'Add Article Type'">
    <el-form
      ref="attForm"
      :model="form"
      :rules="rules"
      label-position="top"
      v-loading="initial">
      <div class="form-row">
        <el-form-item
          prop="articleTypeName"
          class="form-item"
          label="Type Name:">
          <el-input
            v-model="form.articleTypeName"
            class="flex-item"
            maxlength="30"/>
        </el-form-item>
      </div>
      <div class="flexcc" style="margin-top: 80px;">
        <el-button
          class="button"
          type="primary"
          :loading="loading"
          @click="onFormSave">
          <span style="padding: 0 20px;">SAVE</span>
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import api from '../../api/article.js'
export default {
  name: "ArticleTypeDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    id: {
      type: String|Number,
      default: ''
    }
  },
  data() {
    return {
      initial: false,
      loading: false,
      form: {
        articleTypeName: ""
      },
      rules: {
        articleTypeName: {
          required: true,
          message: "Type Name is required",
          trigger: "blur"
        }
      }
    };
  },
  watch: {
    id(n, o) {
      if (this.visible && n) {
        this.$nextTick(() => {
          this.getTypeDetail();
        })
      }
    }
  },
  methods: {
    init() {
      this.form = {
        articleTypeId: "",
        articleTypeName: ""
      }
      this.loading = false;
      this.$nextTick(() => {
        if (this.$refs['attForm']) {
          this.$refs['attForm'].clearValidate();
        }
      })
    },
    hideDialog(success) {
      this.init();
      this.$emit("hide", success || false);
    },
    getTypeDetail() {
      this.initial = true;
      api.viewArticleType(this.id).then(res => {
        if (res.data) {
          this.form = res.data
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.initial = false;
      })
    },
    onFormSave() {
      this.$refs['attForm'].validate((valid) => {
        if (valid) {
          this.isEdit ? this.updateType() : this.addType();
        }
      })
    },
    addType() {
      this.loading = true;
      api.addArticleType(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Add successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    },
    updateType() {
      this.loading = true;
      api.updateArticleType(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Update successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    }
  }
}
</script>

<style scoped>
.dialog-article-type >>> .el-dialog {
  width: 100%;
  max-width: 480px;
}
.dialog-article-type >>> .el-form {
  padding-right: 10px;
}
.form-row {
  display: flex;
  flex-wrap: wrap;
  padding: 0 0 10px;
}
.form-item {
  flex: 1;
  margin-left: 10px;
  margin-bottom: 10px;
}
.form-item >>> label {
  padding: 0;
}
.flex-item {
  min-width: 200px;
  max-width: 450px;
}
.flex-item2 {
  width: 100%;
  min-width: 200px;
}
@media screen and (max-width: 500px) {
  .flex-item {
    width: 100%;
    max-width: none;
  }
}
</style>