<template>
  <basic-container>
    <jl-go-back></jl-go-back>
    <el-form :model="formOption" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="内容标题" prop="title">
        <el-input v-model="formOption.title"></el-input>
      </el-form-item>
      <el-form-item label="一级分类" prop="firstId">
        <el-select v-model="formOption.firstId" placeholder="请选择一级分类" @change="secondList">
          <el-option v-for="(item, index) in first" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" prop="secondId">
        <el-select v-if="formOption.firstId" v-model="formOption.secondId" placeholder="请选择二级分类">
          <el-option v-for="(item, index) in second" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-select v-else disabled v-model="formOption.secondId" placeholder="请选择二级分类"></el-select>
      </el-form-item>
      <el-form-item label="编辑内容" prop="content">
        <Tinymce v-model="formOption.content" ref="tinymce"></Tinymce>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="btnLoading">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </basic-container>
</template>

<script>
  import Tinymce from "@/components/Tinymce";
  import {
    getAllContentCategoryFirstList,
    getAllContentCategorySecondList
  } from '@/api/help/classify'
  import {
    contentSave,
    contentEdit
  } from '@/api/help/content/allContent'

  export default {
    name: "addContent",
    props: {
      id: Number,
      type: String
    },
    components: {
      Tinymce
    },
    data () {
      return {
        btnLoading: false,
        formOption: {
          firstId: '',
          secondId: ''
        },
        rules: {
          title: [
            { required: true, message: '请输入内容标题', trigger: 'blur' }
          ],
          firstId: [
            { required: true, message: '请选择一级分类', trigger: 'blur' }
          ],
          secondId: [
            { required: true, message: '请选择二级分类', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '请输入内容', trigger: 'blur' }
          ],
        },
        first: [],
        second: []
      }
    },
    mounted() {
      this.firstList()
    },
    methods: {
      firstList(){
        if (this.id && this.type == 'edit') {
          this.formOption = JSON.parse(this.id)
          this.searchSecondList(this.formOption.firstId)
        }
        getAllContentCategoryFirstList().then(res => {
          this.first = res.data.data
        })
      },
      secondList (id) {
        getAllContentCategorySecondList (id).then(res => {
          this.formOption.secondId = ''
          this.second = res.data.data
        })
      },
      searchSecondList (id) {
        getAllContentCategorySecondList (id).then(res => {
          this.second = res.data.data
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            if (this.type === 'add') {
              contentSave(JSON.stringify(this.formOption)).then(res => {
                this.$message.success('添加成功');
                this.btnLoading = false
                this.$router.go(-1);
              })
            } else if (this.type === 'edit') {
              contentEdit(JSON.stringify(this.formOption)).then(res => {
                this.$message.success('修改成功');
                this.btnLoading = false
                this.$router.go(-1);
              })
              }
            }
        });
      },
      resetForm(formName) {
        this.viewDrawer = false
        this.$refs[formName].resetFields();
      },
    }
  }
</script>

<style scoped>
  .el-form-item{
    margin-bottom: 28px !important;
  }
</style>
