<template>
  <div class="createPost-container">
    <el-form ref="postForm" :label-position="labelPosition" :model="postForm" :rules="rules" class="form-container" label-width="60px">
      <div class="createPost-main-container">
        <el-row>
          <el-form-item prop="head_img" style="margin-bottom: 30px;" label="头像">
            <el-image :src="postForm.head_img" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="user_name" style="margin-bottom: 30px;" label="姓名">
            <el-input v-model="postForm.user_name" placeholder="" style="width: 300px;" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="mobile" style="margin-bottom: 30px;" label="手机号">
            <el-input v-model="postForm.mobile" placeholder="" style="width: 200px;" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="open_id" style="margin-bottom: 30px;" label="openid">
            <el-input v-model="postForm.open_id" placeholder="" style="width: 200px;" :disabled="true" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-button v-if="!isEdit" v-loading="loading" type="success" @click="submitForm">
            提交
          </el-button>
          <el-button v-if="isEdit" v-loading="loading" type="success" @click="updateArticle">
            修改
          </el-button>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script>

// import Upload from '@/components/Upload/SingleImage3'

// import Sticky from '@/components/Sticky' // 粘性header组件
// import { validURL } from '@/utils/validate'
import { fetchUser, updateUser } from '@/api/user'
// import Warning from './Warning'
// import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'

const defaultForm = {
  // status: 'draft',
  user_name: '', // 文章题目
  status: true, // 文章内容
  head_img: '', // 文章摘要
  mobile: '', // 文章外链
  open_id: '', // 文章图片
  display_time: undefined, // 前台展示时间
  id: undefined,
  platforms: ['a-platform'],
  comment_disabled: false,
  importance: 0
}

export default {
  name: 'ArticleDetail',
  components: { },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      userListOptions: [],
      rules: {
      },
      tempRoute: {},
      labelPosition: 'left'
    }
  },
  computed: {
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    }
    // Why need to make a copy of this.$route here?
    // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
    // https://github.com/PanJiaChen/vue-element-admin/issues/1221
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    fetchData(id) {
      fetchUser(id).then(response => {
        this.postForm = response.data
        // set tags view title
        // this.setTagsViewTitle()
        // set page title
        // this.setPageTitle()
      }).catch(err => {
        console.log(err)
      })
    },
    setPageTitle() {
      const title = 'Edit Article'
      document.title = `${title} - ${this.postForm.id}`
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    updateArticle() {
      console.log(this.postForm)
      updateUser(this.postForm).then(response => {
        this.$notify({
          title: '修改',
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
        this.postForm.status = 'published'
        this.loading = false
        this.listLoading = false
        this.$router.push(`/user/list`)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
