<template>
  <div class="main">
    <card title="组织详情" v-if="detail">
      <template #aside>
        <h6
          class="leave-group"
          v-if="detail.button_show === 3 && !detail.is_mine"
          @click="onLeave"
        >
          退出组织
        </h6>
      </template>
      <div class="info">
        <template v-for="item in fields">
          <div class="info-item" :key="item.name" v-if="detail[item.name]">
            <div class="info-item__label">{{ item.label }}</div>
            <div class="info-item__content">
              <template v-if="item.computed">
                <div v-html="item.computed(detail[item.name], detail)"></div>
              </template>
              <template v-else>
                {{ detail[item.name] }}
              </template>
            </div>
          </div>
        </template>
      </div>
      <div class="buttons">
        <input
          type="button"
          class="button button-info"
          value="查看更多活动"
          v-if="detail.button_show === 3"
          @click="$router.push({ name: 'groupActivity', params: { id: pid } })"
        />
        <!-- <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>
    </card>
  </div>
</template>

<script>
import Card from '@/components/Card'
import * as api from '@/api'
export default {
  name: 'Group',
  components: { Card },
  data() {
    return {
      detail: null,
      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: '活动计划' },
        {
          name: 'logo',
          label: '头像',
          computed(value) {
            return `<img src="${value}" class="group-avatar">`
          }
        }
      ]
    }
  },
  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>
.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 {
  margin: 20px 0;
  text-align: center;
}
.button {
  display: inline-block;
  min-width: 128px;
  height: 30px;
  padding: 0 16px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  border-radius: 20px;
  margin: 0 10px;
}
.button-default {
  background: #ccc;
}
.button-primary {
  background: linear-gradient(
    180deg,
    rgba(2, 236, 228, 1) 0%,
    rgba(18, 186, 244, 1) 100%
  );
}
.button-info {
  background: linear-gradient(
    180deg,
    rgba(255, 201, 44, 1) 0%,
    rgba(255, 116, 42, 1) 100%
  );
}
::v-deep .group-avatar {
  width: 100px;
  height: 100px;
  border-radius: 6px;
  object-fit: contain;
}
.leave-group {
  font-size: 12px;
  font-weight: 400;
  line-height: 30px;
  color: #ccc;
}
</style>
