<template>
  <div>
    <van-nav-bar title="积分申请" left-text="返回" @click-left="$route_back" left-arrow></van-nav-bar>
    <div class="body_com has_header">
      <scroller :isNeed="isNeed">
        <div v-for="(item,index) in fromData.items" :key="index" class="item_class" @click="itemIndex = index">
          <div class="clear operation">
            <span class="fl color_ccc">申请(第{{index+1}}条)</span>
            <span class="fr del_btn" v-show="fromData.items.length > 1" @click="del_one(index)">删除</span>
          </div>
          <integralApplyItem v-model="itemData" :num="index" />
        </div>
        <van-cell-group>
          <van-cell @click="add_one" title-class="color_bule" title="+ 再加一条"></van-cell>
        </van-cell-group>
        <div style="padding:0.32rem;">
          <van-button size="large" @click="data_verify" :disabled="subloading" type="info">提交</van-button>
        </div>
      </scroller>
    </div>
  </div>
</template>
<script>
import request from '@/utils/request'
import integralApplyItem from '@/components/common/integralApplyItem'
import moment from 'moment'

export default {
  name: 'integral_application',
  components: {integralApplyItem},
  watch: {
    itemData: {
      handler (val, oldVal) {
        this.fromData.items.forEach((element, index) => {
          if (val.num == index) {
            this.fromData.items[index] = val
          }
        })
      },
      deep: true
    }
  },
  data () {
    let date = moment().format('YYYY-MM-DD')
    return {
      isNeed:this.$getCache('isAndroid')==1? false: true,
      close_buttom: false,
      fromData: {
        items: [{}]
      },
      itemData: {},
      subloading: false
    }
  },
  methods: {
    // 加一条
    add_one () {
      let data = {}
      this.fromData.items.push(data)
    },
    // 删除一条
    del_one (index) {
      this.$dialog.confirm({
        title: '提示',
        message: '确认删除该条申请?'
      }).then(() => {
        this.fromData.items.splice(index, 1)
        this.$toast('删除成功')
      })
    },
    data_verify () {
      let self = this
      self.$validator.validateAll().then(result => {
        if (!result) {
          self.$notify({ type: 'danger', message: self.$validator.errors.items[0].msg })
        } else {
          self.send()
        }
      })
    },
    send () {
      let self = this
      self.subloading = true
      for (const i in self.fromData.items) {
        if (!self.fromData.items[i].employee_id) {
          self.$notify({ type: 'danger', message: '第' + (parseInt(i) + 1) + '条请选择录入对象'})
          self.subloading = false
          return false
        }
        if (!self.fromData.items[i].event_time) {
          self.$notify({ type: 'danger', message: '第' + (parseInt(i) + 1) + '条请选择日期'})
          self.subloading = false
          return false
        }
        if (!self.fromData.items[i].reviewer_id) {
          self.subloading = false
          self.$notify({ type: 'danger', message: '第' + (parseInt(i) + 1) + '条请选择审批人'})
          return false
        }
        if (!self.fromData.items[i].remark) {
          self.$notify({ type: 'danger', message: '第' + (parseInt(i) + 1) + '条请输入申请事由'})
          self.subloading = false
          return false
        }
      }
      self.$toast.loading({
        message: '正在处理'
      })
      if (this.close_buttom == false) { // 第一次点击提交为false，为false可进入请求。请求成为 为true。0.5秒后变为false
        request('post', '/api/integral/review/apply', self.fromData, 'v2').then(res => {
          self.$toast.clear()
          if (res.data.code == 1) {
            if (res.data.data.list.some(item => item.status != 0)) {
              self.$toast.success(res.data.msg)
              self.fromData = {
                items: [{}]
              }
              self.itemData = {}
              if (window.plus) {
                plus.storage.setItem('callback_data', JSON.stringify({detail_url: this.$route.path, result: res.data.data}))
              } else {
                localStorage.setItem('callback_data', JSON.stringify({detail_url: this.$route.path, result: res.data.data}))
              }
              this.$router.push({path: '/callback_new'})
            } else {
              let index = res.data.data.list.findIndex(item => item.status == 0)
              // self.$notify({ type: 'danger', message: '第' + (index + 1) + '条审批者超出管理范围'})
              self.$notify({ type: 'danger', message: res.data.data.list[0].msg})
            }
          } else {
            self.$notify({ type: 'danger', message: res.data.msg})
          }
        }).catch(e => {
          self.$toast.clear()
        }).finally(() => {
          self.subloading = false
          self.close_buttom = true
          setTimeout(() => {
            self.close_buttom = false
          }, 1000)
        })
      } else {
        self.subloading = false
      }
    }
  }
}
</script>
<style scoped lang="less">
  .body_com{
    height: calc(100% - 0.92rem);
    position: relative;
    overflow-y: scroll;
  }
  .item_class{
    .operation{
      padding: 0.24rem 0.32rem 0;
      font-size: 0.3rem;
      .del_btn{
        color: #26A2FF;
      }
    }
  }
  .color_bule{
    color: #26A2FF;
  }
</style>
