<template>
  <div class="createPost-container">
    <el-form ref="postForm" :label-position="labelPosition" :model="postForm" :rules="rules" class="form-container" label-width="200px">
      <div class="createPost-main-container">
        <el-form-item prop="meet_people" label="对接人">
          <el-input v-model="postForm.meet_people" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="name" label="申请人姓名">
          <el-input v-model="postForm.name" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="mobile" label="手机号">
          <el-input v-model="postForm.mobile" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="id_card" label="身份证">
          <el-input v-model="postForm.id_card" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="education" label="学历">
          <el-input v-model="postForm.education" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="address" label="开店地址">
          <el-input v-model="postForm.address" placeholder="" style="width: 600px;" />
        </el-form-item>
        <el-form-item prop="investment_money" label="投资金额">
          <el-input v-model="postForm.investment_money" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="money_from" label="资金来源">
          <template>
            <el-radio-group v-model="postForm.money_from">
              <el-radio :label="1">自有</el-radio>
              <el-radio :label="2">借贷</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item prop="loan_ration" label="借贷比例">
          <el-input v-model="postForm.loan_ration" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="is_partners" label="是否有合作伙伴">
          <template>
            <el-radio-group v-model="postForm.is_partners">
              <el-radio :label="0">无</el-radio>
              <el-radio :label="1">有</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item prop="partners" label="有几位，占股如何">
          <el-input v-model="postForm.partners" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="has_clerk" label="是否有二位以上的店员">
          <template>
            <el-radio-group v-model="postForm.has_clerk">
              <el-radio :label="0">无</el-radio>
              <el-radio :label="1">有</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item prop="is_manage" label="是否可以全职在店经营">
          <template>
            <el-radio-group v-model="postForm.is_manage">
              <el-radio :label="0">无</el-radio>
              <el-radio :label="1">有</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item prop="experience" label="是否有开店经验/创业经验/管理经验">
          <el-input v-model="postForm.experience" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="sub_ayi" label="关注沪上阿姨多长时间了">
          <el-input v-model="postForm.sub_ayi" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="channel" label="通过那个渠道了解阿姨">
          <el-input v-model="postForm.channel" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="friend" label="是否有认识的人正在经营沪上阿姨">
          <el-input v-model="postForm.friend" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="use_product" label="喝过沪上阿姨几款产品">
          <el-input v-model="postForm.use_product" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="often_product" label="经常喝哪一款">
          <el-input v-model="postForm.often_product" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="tea_view" label="对茶饮行业的看法">
          <el-input v-model="postForm.tea_view" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="why_join" label="为什么想加入沪上阿姨">
          <el-input v-model="postForm.why_join" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="resource" label="是否有寻找店铺的资源">
          <el-input v-model="postForm.resource" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="user_id" label="用户id">
          <el-input v-model="postForm.user_id" placeholder="" style="width: 300px;" />
        </el-form-item>
        <el-form-item prop="mark" label="备注">
          <el-input v-model="postForm.mark" placeholder="" style="width: 300px;" />
        </el-form-item>
        <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 { fetchJoin } from '@/api/join'
// import Warning from './Warning'
// import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'

const defaultForm = {
  // status: 'draft',
  redirect: '', // 文章题目
  status: true, // 文章内容
  position: 1, // 文章摘要
  url: '', // 文章外链
  cover_img: '', // 文章图片
  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) {
      fetchJoin(id).then(response => {
        this.postForm = response.data.info
        // 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)
      this.$router.push(`/join/list`)
      // updateJoin(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(`/banner/list`)
      // })
    },
    draftForm() {
      if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
        this.$message({
          message: '请填写必要的标题和内容',
          type: 'warning'
        })
        return
      }
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    },
    formatCreatePosition(position) {
      switch (position) {
        case 1:
          return 'HOME'
        case 2:
          return 'ACTIVITY'
      }
    },
    editPosition(position) {
      switch (position) {
        case 'HOME':
          return 1
        case 'ACTIVITY':
          return 2
      }
    }
  }
}
</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>
