<template>
  <ai-detail class="detail">
    <template slot="title">
      <ai-title title="积分审核" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
        <template #rightBtn>
          <el-button size="small" type="primary" @click="isShow = true" v-if="info.status === '0'">审核</el-button>
        </template>
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <ai-wrapper>
            <ai-info-item label="事件类型" isLine :value="info.applyItemName"></ai-info-item>
            <ai-info-item label="申请人" :value="info.integralUserName"></ai-info-item>
            <ai-info-item label="手机号" :value="info.phone"></ai-info-item>
            <ai-info-item label="所属地区" :value="info.areaName"></ai-info-item>
            <ai-info-item label="所属网格" :value="info.girdName"></ai-info-item>
            <ai-info-item label="审核时间" v-if="info.status === '1'" :value="info.auditTime"></ai-info-item>
            <ai-info-item label="申请时间" v-if="info.status === '0'" :value="info.createTime"></ai-info-item>
            <ai-info-item label="状态">
              <span>{{ dict.getLabel('appIntegralApplyEventStatus', info.status) }}</span>
              <span v-if="info.status === '1'" style="margin-left: 10px; color: green">积分+{{ info.applyIntegral }}</span>
            </ai-info-item>
            <ai-info-item label="审批意见" v-if="info.status === '2'" isLine>
              <span style="color: red;">{{ info.auditDesc }}</span>
            </ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
      <ai-card title="事件信息">
        <template #right>
          <el-button size="small" type="primary" @click="showEvent" v-if="info.status === '0'">编辑</el-button>
        </template>
        <template #content>
          <ai-wrapper>
            <ai-info-item label="积分值" isLine :value="info.applyIntegral"></ai-info-item>
            <ai-info-item label="事件描述" isLine :value="info.content"></ai-info-item>
            <ai-info-item label="图片" isLine v-if="info.images && info.images.length">
              <div class="files">
                <!-- <div class="file-item" v-for="(item, index) in info.images" :key="index" v-viewer="{movable: true}">
                  <img :src="item.url">
                </div> -->
                <ai-uploader
                  :instance="instance"
                  fileType="img"
                  acceptType=".jpg,.png,.jpeg"
                  v-model="info.images"
                  :limit="9" :disabled="true">
                </ai-uploader>
              </div>
            </ai-info-item>
            <ai-info-item label="视频" isLine v-if="info.videos && info.videos.length">
              <div class="files">
                <div class="file-item" v-for="(item, index) in info.videos" :key="index">
                  <video controls :src="item.url"></video>
                </div>
              </div>
            </ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
      <ai-dialog
        :visible.sync="isShow"
        @onConfirm="onConfirm"
        @close="onClose"
        width="890px"
        title="审核">
        <el-form class="ai-form" :model="form" label-width="120px" ref="form">
          <el-form-item label="是否通过" prop="auditStatus" style="width: 100%;" :rules="[{required: true, message: '请选择是否通过', trigger: 'change'}]">
            <el-radio-group v-model="form.auditStatus">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="auditDesc" v-if="form.auditStatus === '0'" label="审批意见" style="width: 100%" :rules="[{required: true, message: '请输入审批意见', trigger: 'blur'}]">
            <el-input size="small" type="textarea" :rows="5" v-model="form.auditDesc" clearable placeholder="请输入审批意见"></el-input>
          </el-form-item>
        </el-form>
      </ai-dialog>
      <ai-dialog
        :visible.sync="isShowEvent"
        @onConfirm="onEventConfirm"
        @close="onClose"
        width="890px"
        title="编辑">
        <el-form class="ai-form" :model="eventForm" label-width="120px" ref="eventForm">
          <el-form-item label="积分值" prop="applyIntegral" :rules="[{required: true, message: '请输入积分值', trigger: 'change'}]">
            <el-input-number style="width: 200px;" size="small" :precision="2" type="input" v-model="eventForm.applyIntegral" clearable placeholder="请输入积分值" :min="0"></el-input-number>
          </el-form-item>
          <el-form-item prop="content" label="事件描述" style="width: 100%" :rules="[{required: true, message: '请输入事件描述', trigger: 'blur'}]">
            <el-input size="small" type="textarea" :rows="5" :maxlength="300" show-word-limit v-model="eventForm.content" clearable placeholder="请输入事件描述"></el-input>
          </el-form-item>
          <el-form-item style="width: 100%" label="图片">
            <ai-uploader
              :instance="instance"
              fileType="img"
              acceptType=".jpg,.png,.jpeg"
              v-model="eventForm.images"
              :limit="9">
            </ai-uploader>
          </el-form-item>
          <el-form-item style="width: 100%" label="视频">
            <ai-uploader
              :instance="instance"
              fileType="file"
              acceptType=".mp4,.MOV"
              v-model="eventForm.videos"
              :limit="9">
            </ai-uploader>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Detail',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        info: {},
        form: {
          auditDesc: '',
          auditStatus: ''
        },
        eventForm: {
          files: null,
          images: [],
          videos: [],
          content: '',
          applyIntegral: ''
        },
        isShowEvent: false,
        isShow: false
      }
    },

    created () {
      this.getInfo()
    },

    methods: {
      getInfo () {
        this.instance.post(`/app/appintegraluserapply/queryDetailById?id=${this.params.id}`).then(res => {
          if (res.code == 0) {
            if (res.data) {
              this.info = {
                ...res.data,
                files: res.data.files.map(v => {
                  return {
                    ...v,
                    postfix: v.postfix.toLowerCase()
                  }
                })
              }

              if (res.data.status === '0') {
                this.eventForm.files = res.data.files
                this.eventForm.content = res.data.content
                this.eventForm.applyIntegral = res.data.applyIntegral
              }
              this.info.images = res.data.files.filter(e => (['jpeg', 'jpg', 'png'].includes(e.postfix.split('.')[1])))
              this.info.videos = res.data.files.filter(e => (['mp4', 'MOV'].includes(e.postfix.split('.')[1])))
            }
          }
        })
      },

      showEvent () {
        this.eventForm.files = null
        this.eventForm.content = this.info.content
        this.eventForm.applyIntegral = this.info.applyIntegral
        this.isShowEvent = true
      },

      onClose () {
        this.form.auditDesc = ''
        this.form.auditStatus = ''
      },

      onEventConfirm () {
        if ((this.eventForm.images.length + this.eventForm.videos.length) > 9) {
          return this.$message.error('图片和视频不得超过9个')
        } else {
          this.eventForm.files = [...this.eventForm.images,...this.eventForm.videos]
        }
        this.$refs.eventForm.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appintegraluserapply/updateByGirdMember`, {
              ...this.eventForm,
              id: this.params.id,
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('编辑成功！')
                this.isShowEvent = false
                this.getInfo()
              }
            })
          }
        })
      },

      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appintegraluserapply/auditById`, {
              ...this.form,
              id: this.params.id
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('审核成功！')
                this.isShow = false
                this.getInfo()
              }
            })
          }
        })
      },

      cancel () {
        this.$emit('change', {
          type: 'List',
          isRefresh: true
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .detail {
    .files {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      .file-item {
        width: 118px;
        height: 118px;
        margin: 0 20px 20px 0;

        img, video {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }

        img {
          cursor: pointer;
          transition: all ease 0.3s;

          &:hover {
            opacity: 0.7;
          }
        }
      }
    }
  }
</style>
