<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="500px"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.isShow" prop="isShow">
          <el-radio :label="true">显示</el-radio>
          <el-radio :label="false">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          controls-position="right"
          v-model="form.sort"
          :precision="0"
          :min="1"
          :max="9999"
          label="序号"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getItem, saveItem } from '@/api/help/type';

export default {
  name: 'IssueTypeItemModal',

  props: {
    id: {
      type: String,
      default: ''
    },
    parentId: {
      type: String,
      default: '-1'
    }
  },

  data() {
    return {
      modal: true,

      form: {
        name: '',
        isShow: false,
        sort: 1
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    };
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑分类';
      } else {
        return '新增分类';
      }
    }
  },

  watch: {
    id: {
      handler(id) {
        id && this.loadData();
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      });
      if (success) {
        this.form = data;
      }
    },

    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const params = Object.assign({}, this.form);
          if (this.id) params.id = this.id;
          if (!this.form.parentId) {
            params.parentId = this.parentId;
          }
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('issue_type_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
