<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="800px"
    :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="内容" prop="content">
        <RichText v-model="form.content"></RichText>
      </el-form-item>
      <el-form-item label="分类" prop="helpGroupId">
        <IssueTypeSelect v-model="form.helpGroupId"></IssueTypeSelect>
      </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/info';

export default {
  name: 'InformationItemModal',

  props: {
    id: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      modal: true,

      form: {
        name: '',
        content: '',
        helpGroupId: null,
        isShow: false,
        sort: 1
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入活动内容', trigger: 'blur' }
        ],
        helpGroupId: [
          { required: true, message: '请选择所属分类', trigger: 'change' }
        ]
      }
    };
  },

  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;
          params.content = this.$filterRichTxt(params.content);
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('information_reload');
          }
        }
      });
    }
  }
};
</script>

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