<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">发布
        </el-button>
      </sticky>
      <div class="createPost-main-container">
        <el-row :gutter="20">
          <el-card class="box-card">

            <div slot="header" class="clearfix">
              <span>小组相关</span>
            </div>

            <el-row>
              <el-col :span="24">
                <el-form-item style="margin-bottom: 20px;" prop="name">
                  <MDinput v-model="postForm.name" :maxlength="100" name="name" required>
                    小组名称
                  </MDinput>
                </el-form-item>

                <div class="postInfo-container">
                  <el-row>
                    <el-col :span="8">
                      <el-form-item label-width="75px" label="明星称号:" class="postInfo-container-item">
                        <el-select v-model="postForm.star_name" :remote-method="getRemoteStarList" filterable remote multiple value-key="id"
                                   placeholder="搜索明星" style="width: 100%">
                          <el-option v-for="(item,index) in starListOptions" :key="item+index" :label="item.name" :value="item.id"/>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label-width="75px" label="小组组长:" class="postInfo-container-item">
                        <el-select v-model="postForm.group_leader" :remote-method="getRemoteUserList" filterable remote multiple value-key="id"
                                   placeholder="搜索明星" style="width: 100%">
                          <el-option v-for="(item,index) in starListOptions" :key="item+index" :label="item.name" :value="item.id"/>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item style="margin-bottom: 20px;" label-width="75px" label="组员数量:" prop="user_nums">
                        <el-input v-model="postForm.user_nums" type="number" placeholder="请输入内容" style="width: 230px;" readonly/>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="8">
                      <el-form-item style="margin-bottom: 20px;" label-width="75px" label="下线小组:">
              <el-radio-group v-model="postForm.is_online">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item style="margin-bottom: 20px;" label-width="75px" label="推荐小组:">
              <el-radio-group v-model="postForm.is_recommend">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
                    </el-col>
                    <el-col :span="8">

                    </el-col>
                  </el-row>
                </div>
              </el-col>
            </el-row>
            <el-form-item style="margin-bottom: 20px;" label-width="75px" label="小组简介:">
              <el-input :rows="1" v-model="postForm.description" type="textarea" class="article-textarea" autosize
                        placeholder="请输入内容"/>
              <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}字</span>
            </el-form-item>

          </el-card>
        </el-row>

        <el-row :gutter="20" style="margin-top:50px;">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>组员相关</span>
            </div>
            <div style="margin-bottom:50px;">
              <div class="filter-container">
                <el-input :placeholder="'添加用户'" v-model="listQuery.filter.value" style="width: 180px;"
                          class="filter-item"
                          @keyup.enter.native="appendUser"/>
                <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="appendUser">添加
                </el-button>
                <el-button v-waves class="filter-item" type="primary" icon="el-icon-delete" @click="delUser">移除
                </el-button>
              </div>
              <el-table :data="list" border fit highlight-current-row style="width: 100%"
                        ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="76" align="center"></el-table-column>
                <el-table-column align="center" label="用户ID " >
                  <template slot-scope="scope">
                    <router-link :to="'/push/edit/'+scope.row.id" class="link-type">
                      <span>{{ scope.row.id }}</span>
                    </router-link>
                  </template>
                </el-table-column>
                <el-table-column  align="center" label="用户名">
                  <template slot-scope="scope">
                    <span>{{ scope.row.username }}</span>
                  </template>
                </el-table-column>
                <el-table-column  align="center" label="联系电话">
                  <template slot-scope="scope">
                    <span>{{ scope.phone }}</span>
                  </template>
                </el-table-column>
                <el-table-column  align="center" label="发布帖子数">
                  <template slot-scope="scope">
                    <span>{{ scope.row.topic_nums }}</span>
                  </template>
                </el-table-column>
                <el-table-column  align="center" label="用户身份">
                  <template slot-scope="scope">
                    <span>{{ scope.row.user_identity }}</span>
                  </template>
                </el-table-column>
                <el-table-column  align="center" label="组内身份">
                  <template slot-scope="scope">
                    <span>{{ scope.row.internal_identity }}</span>
                  </template>
                </el-table-column>

              </el-table>

              <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                          style="margin-left: 250px;" @pagination="getList"/>
            </div>
          </el-card>
        </el-row>


      </div>
    </el-form>

  </div>
</template>

<script>
  import MDinput from '@/components/MDinput'
  import Sticky from '@/components/Sticky' // 粘性header组件
  import waves from '@/directive/waves'
  import Pagination from '@/components/Pagination'

  import {validateURL} from '@/utils/validate'
  import {fetchList} from '@/api/user'
  import {starSearch, userSearch} from '@/api/remoteSearch'

  const defaultForm = {
    status: 'draft',
    title: '', // 文章题目
    name: '', // 文章内容
    description: '', // 文章摘要
    display_time: undefined, // 前台展示时间
    id: undefined,
    user_nums: 0,
    group_leader: '',
  }

  export default {
    name: 'GroupDetail',
    components: {MDinput, Sticky, Pagination},
    directives: {waves},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      const validateRequire = (rule, value, callback) => {
        if (value === '') {
          this.$message({
            message: rule.field + '为必传项',
            type: 'error'
          })
          callback(new Error(rule.field + '为必传项'))
        } else {
          callback()
        }
      }
      const validateSourceUri = (rule, value, callback) => {
        if (value) {
          if (validateURL(value)) {
            callback()
          } else {
            this.$message({
              message: '外链url填写不正确',
              type: 'error'
            })
            callback(new Error('外链url填写不正确'))
          }
        } else {
          callback()
        }
      }
      return {
        postForm: Object.assign({}, defaultForm),
        loading: false,
        userListOptions: [],
        rules: {
          title: [{validator: validateRequire}],
          content: [{validator: validateRequire}],
        },
        tempRoute: {},
        starListOptions: [],
        userListOptions: [],

        // 小组相关
        list: null,
        total: 1,
        listLoading: true,
        multipleSelection: [],
        del_list: [],
        listQuery: {
          page: 0,
          limit: 10,
          filter: {
            value: '',
            key: '',
          },
        },
      }
    },
    computed: {
      contentShortLength() {
        return this.postForm.description.length
      },
    },
    created() {
      if (this.isEdit) {
        const id = this.$route.params && this.$route.params.id
        this.fetchData(id)
        this.getList()
      } else {
        this.postForm = Object.assign({}, defaultForm)
      }

      this.tempRoute = Object.assign({}, this.$route)
    },
    methods: {
      fetchData(id) {
        fetchArticle(id).then(response => {
          this.postForm = response.data
        }).catch(err => {
          console.log(err)
        })
      },
      submitForm() {
        this.postForm.display_time = parseInt(this.display_time / 1000)
        console.log(this.postForm)
        this.$refs.postForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$notify({
              title: '成功',
              message: '发布文章成功',
              type: 'success',
              duration: 2000
            })
            this.postForm.status = 'published'
            this.loading = false
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      getRemoteUserList(query) {
        userSearch(query).then(response => {
          if (!response.data.items) return
          this.userListOptions = response.data.items.map(v => v.name)
        })
      },

      // 添加组员相关
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      appendUser() {

      },
      delUser() {

      },
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = []
          this.total = 100
          this.listLoading = false
        })
      },
      getRemoteStarList(query){
        starSearch(query).then(response => {
          if (!response.data.data.data) return
          this.starListOptions = response.data.data.data
        })
      },
      getRemoteUserList(query){
        userSearch(query).then(response => {
          if (!response.data.data.data) return
          this.starListOptions = response.data.data.data
        })
      },
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/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;
        }
      }
      .editor-container {
        min-height: 500px;
        margin: 0 0 30px;
        .editor-upload-btn-container {
          text-align: right;
          margin-right: 10px;
          .editor-upload-btn {
            display: inline-block;
          }
        }
      }
    }
    .word-counter {
      width: 40px;
      position: absolute;
      right: -10px;
      top: 0px;
    }
  }
</style>
