<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-form ref="form" :model="form" label-width="100px" style="">
        <el-form-item label="Title" style="width: 500px;"><!--标题-->
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="Category"><!--分类-->
          <el-select v-model="form.cid" placeholder="please select category"><!--请选择分类-->
            <el-option v-for="item in allCategory" :label="item.CATE_NAME" :value="item.ID" :key="item.ID"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Order" style="width: 500px;"><!--排序值-->
            <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="Content"><!--内容-->
          <editor id="contentEditor" :cloud-channel="editorRootUrl" ref="contentEditor" v-model="form.content"
                  :init="editorOptions"></editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="submitButtonStat" @click="onSubmit">Confirm<!-- 提交 --></el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import Editor from 'nix-tinymce'
import {fullEditor, textEditor, EDITOR_ROOT_URL} from '@/utils/editor'
import store from '@/utils/vuexStore'
import network from '@/utils/network'
import tool from '@/utils/tool'

export default {
  name: 'article_edit',
  components: {
    'editor': Editor
  },
  mounted () {
    if (this.$route.name === 'article_edit') {
      network.getData('article/edit/' + this.$route.params.id).then(response => {
        this.form.title = response.oneData.TITLE
        this.form.cid = response.oneData.CID
        this.form.content = response.oneData.CONTENT
        this.form.sort = response.oneData.SORT
        this.allCategory = response.allCategory
        this.loading = false
        this.isEdit = true
      })
    } else {
      network.getData('article/add').then(response => {
        this.allCategory = response.allCategory
        this.loading = false
      })
    }
  },
  data () {
    return {
      form: {
        title: '',
        cid: null,
        content: '',
        sort: 1,
      },
      allCategory: null,
      loading: false,
      submitButtonStat: false,
      isEdit: false,
      editorRootUrl: EDITOR_ROOT_URL,
      editorOptions: textEditor,
    }
  },
  methods: {
    onSubmit () {
      this.submitButtonStat = true
      let path = 'article/add'
      if (this.$route.name === 'article_edit') {
        path = 'article/edit/' + this.$route.params.id
      }
      network.postData(path, this.form).then(response => {
        this.submitButtonStat = false
        this.$message({
          message: response,
          type: 'success'
        })
        this.$router.go(-1)
      }).catch(() => {
        this.submitButtonStat = false
      })
    },
  }
}
</script>

<style scoped>

</style>
