<template>
  <div class="AddAlbum">
    <div class="form-group">
      <div class="form-group__item">
        <div class="left">
          <label>*</label>
          <span>相册名称</span>
        </div>
        <div class="right">
          <input placeholder="请输入相册名称" v-model="albumName" placeholder-style="color: #999" />
        </div>
      </div>
      <div class="form-group__item">
        <div class="left">
          <label>*</label>
          <span>水印</span>
        </div>
        <div class="right" @click="linkTo('./WatermarkSetting?value=' + watermarkId)">
          <span>{{ watermarkId ? '已选择' : '不限' }}</span>
          <image src="./images/right.png" />
        </div>
      </div>
      <div class="form-group__item">
        <div class="left">
          <label>*</label>
          <span>拍摄人</span>
        </div>
        <div class="right" @click="linkTo('./PersonnelSetting?id=' + id)">
          <span>{{ albumUserList.length ? '已选择' : '不限' }}</span>
          <image src="./images/right.png" />
        </div>
      </div>
      <div class="form-group__item" @click="linkTo('./SourceSetting?value=' + photoSource)">
        <div class="left">
          <label>*</label>
          <span>照片来源</span>
        </div>
        <div class="right">
          <span>{{ photoSource == 1 ? '仅限乡村相册拍摄' : '不限' }}</span>
          <image src="./images/right.png" />
        </div>
      </div>
    </div>
    <button v-if="user.openId === info.createUserId" @click="remove" class="form-btn form-btn__remove" hover-class="text-hover">删除相册</button>
    <button :loading="isLoading" @click="save" class="form-btn" hover-class="text-hover">保存</button>
  </div>
</template>
<script>
  import { mapState } from 'vuex'

  export default {
    name: 'AddAlbum',

    appName: '新增相册',

    data () {
      return {
        albumName: '',
        createUserId: '',
        photoSource: 0,
        watermarkId: '',
        isLoading: false,
        id: '',
        info: {},
        albumUserList: []
      }
    },

    computed: {
      ...mapState(['user']),
    },

    onLoad (query) {
      this.id = query.id || ''

      if (query.id) {
        this.getInfo(query.id)
      }

      uni.$on('change', e => {
        if (e.type === 'watermark') {
          this.watermarkId = e.value
        }

        if (e.type === 'photoSource') {
          this.photoSource = e.value
        }

        if (e.type === 'personnel') {
          this.albumUserList = e.value
        }
      })
    },

    methods: {
      linkTo (url) {
        uni.navigateTo({
          url
        })
      },

      remove () {
        this.$confirm('确定删除该相册？').then(() => {
          this.$http.post(`/api/appalbum/delete?ids=${this.id}`).then(res => {
            if (res.code == 0) {
              this.$u.toast('删除成功')
              uni.$emit('update')

              setTimeout(() => {
                uni.reLaunch({
                  url: './AppCountryAlbum'
                })
              }, 500)
            }
          })
        }).catch(() => {
        })
      },

      getInfo (id) {
        this.$http.post(`/api/appalbum/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.info = res.data
            this.albumName = res.data.albumName
            this.createUserId = res.data.createUserId || ''
            this.photoSource = res.data.photoSource || 0
            this.watermarkId = res.data.watermarkId || ''
            this.albumUserList = res.data.albumUserList || []
          }
        })
      },

      save () {
        if (!this.albumName) {
          return this.$u.toast('请输入相册名称')
        }

        this.$loading()

        this.$http.post('/api/appalbum/addOrUpdate', {
          albumName: this.albumName,
          createUserId: this.createUserId,
          photoSource: this.photoSource,
          watermarkId: this.watermarkId,
          albumUserList: this.albumUserList,
          id: this.id || ''
        }).then(res => {
          if (res.code === 0) {
            this.$u.toast(this.id ? '编辑成功' : '新建成功')

            if (this.id) {
              uni.$emit('change')
            } else {
              uni.$emit('update')
            }

            setTimeout(() => {
              uni.navigateBack({
                delta: 1
              })
            }, 500)
          } else {
            this.$hideLoading()
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .AddAlbum {
    padding-bottom: 130px;

    * {
      line-height: 1;
      box-sizing: border-box;
    }

    .form-btn {
      position: fixed;
      bottom: 0;
      left: 0;
      z-index: 1;
      width: 100%;
      height: 112px;
      line-height: 112px;
      text-align: center;
      color: #fff;
      font-size: 32px;
      background: #1365DD;

      &:active {
        opacity: 0.8;
      }

      &.form-btn__remove {
        bottom: 140rpx;
        background: #FF4466;
        color: #fff;
      }
    }
    .form-group {
      margin-bottom: 16px;
      padding: 0 32px;
      background: #fff;

      .form-group__item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 112px;
        font-size: 32px;
        color: #333333;
        border-bottom: 1px solid #DDDDDD;

        &.form-group__checked {
          height: auto;
          padding: 32px 0;

          .left {
            h2 {
              margin-bottom: 12px;
              color: #333333;
              font-size: 32px;
            }

            &.left-add {
              display: flex;
              align-items: center;

              h2 {
                margin-bottom: 0;
              }
            }

            .add-btn {
              position: relative;
              width: 48px;
              height: 48px;
              margin-right: 16px;
              border-radius: 50%;
              overflow: hidden;
              background: #1088F9;

              &::after {
                position: absolute;
                top: 50%;
                left: 50%;
                z-index: 1;
                width: 4px;
                height: 24px;
                background: #fff;
                content: ' ';
                transform: translate(-50%, -50%);
              }

              &::before {
                position: absolute;
                top: 50%;
                left: 50%;
                z-index: 1;
                width: 24px;
                height: 4px;
                background: #fff;
                content: ' ';
                transform: translate(-50%, -50%);
              }
            }

            p {
              color: #999999;
              font-size: 28px;
            }
          }
        }

        label {
          color: #FF4466;
        }

        &:last-child {
          border: none;
        }

        .right {
          display: flex;
          align-items: center;
          color: #999999;
          image {
            width: 32px;
            height: 32px;
          }

          input {
            text-align: right;
            color: #999;
            font-size: 32px;
          }
        }
      }
    }
  }
</style>
