<template>
  <div class="main">
    <div class="bg">
      <img :src="detail.logo" class="group-avatar" />
      <h1 class="group-name">{{ detail.org_name }}</h1>
    </div>
    <card :title="detail.org_name" class="card-box">
      <template #aside>
        <h6 class="leave-group" v-if="detail.button_show === 3 && !detail.is_mine" @click="onLeave">退出组织</h6>
      </template>
      <div class="group-count">
        {{ detail.act_num }}活动 · {{ detail.num }}成员 · {{ detail.hot }}热度
        <div
          class="button default-button"
          @click.stop="
            $router.push({ name: 'groupMember', params: { id: detail.id }, query: { is_mine: detail.is_mine } })
          "
        >
          成员列表
        </div>
      </div>
      <div class="group-info">{{ detail.goal }}</div>
    </card>
    <group-activity></group-activity>
    <div class="buttons">
      <!-- <input
          type="button"
          class="button button-primary"
          value="创建活动"
          v-if="detail.is_mine"
          @click="
            $router.push({
              name: 'createActivity',
              query: { org_id: detail.id }
            })
          "
      />-->
      <input
        type="button"
        class="button button-primary"
        value="申请加入"
        @click="joinGroup"
        v-if="detail.button_show === 1"
      />
      <input
        type="button"
        class="button button-default"
        value="已申请，等待审核"
        v-if="detail.button_show === 2"
        @click="onCancelJoin"
      />
    </div>
  </div>
</template>

<script>
import Card from '@/components/Card'
import GroupActivity from './Activity'
import * as api from '@/api'
export default {
  name: 'Group',
  components: { Card, GroupActivity },
  data() {
    return {
      detail: {},
      fields: [
        { name: 'org_name', label: '名称' },
        { name: 'org_type_str', label: '类型' },
        { name: 'org_province', label: '省份' },
        { name: 'org_city', label: '城市' },
        { name: 'industry', label: '行业' },
        { name: 'purpose', label: '宗旨' },
        { name: 'goal', label: '目的' },
        { name: 'initiator', label: '主要发起人' },
        { name: 'rules', label: '组织章程' },
        { name: 'plan', label: '活动计划' }
      ]
    }
  },
  computed: {
    pid() {
      return this.$route.params.id
    }
  },
  methods: {
    // 组织详情
    getDetail() {
      api.getGroup(this.pid).then(response => {
        this.detail = response
      })
    },
    // 加入组织
    joinGroup() {
      api.joinGroup(this.pid).then(() => {
        this.getDetail()
      })
    },
    // 离开组织
    leaveGroup() {
      api.leaveGroup(this.pid).then(() => {
        this.getDetail()
      })
    },
    // 取消申请
    onCancelJoin() {
      this.$dialog
        .confirm({
          title: '提示',
          message: '你已申请加入该组织，是否取消申请？'
        })
        .then(() => {
          api.getGroup(this.pid).then(response => {
            if (response.button_show === 3) {
              this.$toast('你已加入该组织')
            } else {
              this.leaveGroup()
            }
          })
        })
        .catch(() => {})
    },
    // 退出组织
    onLeave() {
      this.$dialog
        .confirm({
          title: '提示',
          message: '确定要退出该组织吗？'
        })
        .then(() => {
          this.leaveGroup()
        })
        .catch(() => {})
    }
  },
  beforeMount() {
    this.getDetail()
  }
}
</script>

<style lang="scss" scoped>
.bg {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 2.1rem;
  margin-left: -15px;
  margin-right: -15px;
  background: url('~@/assets/img/group_bg.png') no-repeat;
  background-size: cover;
}
.group-avatar {
  width: 1.12rem;
  height: 1.12rem;
  object-fit: cover;
  border-radius: 50%;
  overflow: hidden;
}
.group-name {
  margin-top: 0.18rem;
  font-size: 0.17rem;
  color: #fff;
}
.group-count {
  position: relative;
  font-size: 12px;
  color: #a0a0a0;
  line-height: 2;
  border-bottom: 1px solid #c9c9c9;
}
.group-info {
  padding: 14px 0;
  font-size: 13px;
  color: #313131;
  line-height: 18px;
}
.info-item {
  margin-bottom: 20px;
  display: flex;
}
.info-item__label {
  width: 70px;
  color: #999;
  text-align: center;
}
.info-item__content {
  flex: 1;
  color: #222;
  overflow: hidden;
  margin-left: 20px;
}
.buttons {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 34px;
  text-align: center;
}
.button {
  display: inline-block;
  min-width: 200px;
  height: 40px;
  padding: 0 16px;
  line-height: 40px;
  color: #fff;
  text-align: center;
  border-radius: 20px;
  margin: 0 10px;
}
.button-default {
  background: #ccc;
}
.button-primary {
  background: #af1b40;
}
.leave-group {
  font-size: 12px;
  font-weight: 400;
  line-height: 30px;
  color: #ccc;
}
.default-button {
  font-size: 12px;
  color: #999;
  line-height: 30px;
  border: 1px solid #999;
}
.card-box {
  position: relative;
  .button {
    position: absolute;
    bottom: 5px;
    right: 0;
    min-width: 34px;
    height: 30px;
    text-align: center;
    border-radius: 5px;
  }
}
</style>
