<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">
      <p v-if="detail.button_show === 3">紫荆花：{{ detail.bauhinia }}</p>
      <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 } })
          "
          style="margin: 0"
        >
          组织成员
        </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="showJoinGroup = true"
        v-if="detail.button_show === 1"
      />
      <input
        type="button"
        class="button button-default"
        value="已申请，等待审核"
        v-if="detail.button_show === 2"
        @click="onCancelJoin"
      />
    </div>
    <van-overlay :show="showJoinGroup" @click="showJoinGroup = false">
      <div class="dialog-group" @click.stop>
        <div class="dialog-group-hd">申请理由</div>
        <div class="dialog-group-bd">
          <van-form @submit="joinGroup">
            <van-field type="textarea" v-model="remark" name="remark" />
            <van-button round type="primary" native-type="joinGroup">提交</van-button>
          </van-form>
        </div>
      </div>
    </van-overlay>
    <van-overlay :show="showJoinGroup" @click="showJoinGroup = false">
      <div class="dialog-group" @click.stop>
        <div class="dialog-group-hd">申请理由</div>
        <div class="dialog-group-bd">
          <van-form @submit="joinGroup">
            <van-field type="textarea" v-model="remark" name="remark" />
            <van-button round type="primary" native-type="joinGroup">提交</van-button>
          </van-form>
        </div>
      </div>
    </van-overlay>
    <van-overlay :show="showJoinGroupSuccess" @click="showJoinGroupSuccess = false">
      <div class="dialog-group" @click.stop>
        <div class="dialog-group-hd">
          <div><img src="../../assets/img/icon_success2.png" width="60px" /></div>
          <p style="margin-top: 18px">提交成功</p>
        </div>
        <div class="dialog-group-bd">
          <p>请您等待管理员审核，审核<br />结果会在24小时内在我的消<br />息显示</p>
          <van-button round type="primary" @click="showJoinGroupSuccess = false">确定</van-button>
        </div>
      </div>
    </van-overlay>
  </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: '活动计划' }
      ],
      showJoinGroup: false,
      remark: '',
      showJoinGroupSuccess: false
    }
  },
  computed: {
    pid() {
      return this.$route.params.id
    }
  },
  methods: {
    // 组织详情
    getDetail() {
      api.getGroup(this.pid).then(response => {
        this.detail = response
      })
    },
    // 加入组织
    joinGroup() {
      api.joinGroup(this.pid, { remark: this.remark }).then(() => {
        this.showJoinGroup = false
        this.showJoinGroupSuccess = true
        this.remark = ''
        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;
  }
}
.dialog-group {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 240px;
  margin: 0 auto;
  padding: 30px 20px;
  background-color: #fff;
  text-align: center;
  border-radius: 6px;
  ::v-deep .van-cell {
    padding: 0;
  }
  ::v-deep .van-field__control {
    height: 150px;
    padding: 10px;
    border: 1px solid #a0a0a0;
  }
  ::v-deep .van-button {
    width: 130px;
    background-color: #af1b40;
    border: 0;
    margin: 24px auto 0;
  }
}
.dialog-group-hd {
  font-size: 17px;
  font-weight: 400;
  color: #313131;
  line-height: 1;
  padding-bottom: 16px;
}
</style>
