<template>
  <ai-detail class="audit">
    <template slot="title">
      <ai-title title="详情" isShowBack isShowBottomBorder @onBackClick="cancel(true)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <ai-wrapper
              label-width="120px">
            <ai-info-item label="申请人" :value="info.partyName"></ai-info-item>
            <ai-info-item label="申请时间" :value="info.createTime"></ai-info-item>
            <ai-info-item label="积分类型" :value="info.integralRuleName"></ai-info-item>
            <ai-info-item label="规则类型">
              {{ integralInfo.ruleType==0? '常规': integralInfo.ruleType==1? '阶梯': '区间'}}
            </ai-info-item>
            <ai-info-item label="申请描述" :value="info.description" isLine></ai-info-item>
            <ai-info-item label="图片资料" isLine v-show="info.files.length">
              <ai-uploader v-model="info.files" disabled></ai-uploader>
            </ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
      <ai-card title="审核详情" v-if="info.auditStatus !== '0'">
        <div slot="content" style="margin-top: 16px;margin-bottom:24px">
          <ai-wrapper label-width="120px">
            <ai-info-item label="审核结果" :value="info.auditStatus == '1' ? '通过' : '拒绝'"></ai-info-item>
            <!-- 通过 -->
            <ai-info-item label="积分调整" v-if="integralInfo.ruleType == '1' && info.auditStatus == '1'" :value="'+' +  info.auditIntegral + '分' + '('+ info.auditIntegralItem +')'"></ai-info-item>
            <!-- 未通过（阶梯状态下显示加分项） -->
            <ai-info-item label="积分调整" v-if="integralInfo.ruleType != '1' && info.auditStatus == '1'" :value="'+' + info.auditIntegral + '分' "></ai-info-item>
            <ai-info-item label="积分调整" v-if="info.auditStatus != 1" :value="'0分'"></ai-info-item>
            <ai-info-item label="审核人" :value="info.auditUserName"></ai-info-item>
            <ai-info-item label="审核时间" :value="info.auditTime"></ai-info-item>
            <ai-info-item label="驳回理由" v-if="info.auditStatus == '2'" isLine :value="info.auditOpinion"></ai-info-item>
            
          </ai-wrapper>
        </div>
      </ai-card>
      <ai-dialog
          :visible.sync="isShow"
          width="800px"
          @close="onClose"
          title="事件审核"
          @onConfirm="onConfirm">
        <el-form class="ai-form" label-width="120px" :model="form" ref="form">

          <el-form-item label="是否通过审核" prop="pass" style="width: 100%;" :rules="[{ required: true, message: '请选择是否通过审核' }]">
            <el-radio-group v-model="form.pass" @change="onStatusChange">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- 常规 -->
          <el-form-item label="积分分值" style="width: 100%;" v-show="integralInfo.ruleType == 0 && form.pass == 1">
            {{ integralInfo.ruleType == 0? '+'+ integralInfo.integral : '' }}
          </el-form-item>
          <!-- 区间 -->
          <el-form-item label="区间范围" style="width: 100%;" v-show="integralInfo.ruleType == 2 && form.pass == 1">
            <span>{{ integralInfo.integralMin }}</span> - <span>{{ integralInfo.integralMax }}</span>
          </el-form-item>
          <el-form-item label="积分分值" style="width: 100%;" :rules="[{ required: true, message: '请输入区域范围内的积分分值' }]" v-show="integralInfo.ruleType == 2 && form.pass == 1">
            <el-input v-model.number="form.auditIntegral" placeholder="请输入区域范围内的积分分值" @input="inputChange"></el-input>
          </el-form-item>
          <!-- 阶梯 -->
          <el-form-item label="加分项" style="width: 100%;" :rules="[{ required: true, message: '请选择加分项' }]" v-show="integralInfo.ruleType == 1 && form.pass == 1">
            <ai-select v-model="form.integralItem" :selectList="scoreItem"  @change="selectItem"/>
          </el-form-item>

          <el-form-item label="积分分值" style="width: 100%;" :rules="[{ required: true, message: '请输入积分分值' }]" v-show="integralInfo.ruleType == 1 && form.pass == 1">
            <el-input v-model.number="form.auditIntegral" placeholder="请输入1-10的整数" disabled></el-input>
          </el-form-item>

          <el-form-item label="审核意见" v-show="form.pass == '0'" style="width: 100%;" :rules="[{ required: true, message: '请输入审核意见' }]">
            <el-input type="textarea" :rows="5" :maxlength="200" v-model="form.opinion" clearable placeholder="请输入审核意见" show-word-limit></el-input>
          </el-form-item>

        </el-form>
      </ai-dialog>
    </template>

    <template #footer v-if="info.auditStatus === '0'">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="isShow = true">审核</el-button>
    </template>

  </ai-detail>
</template>

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

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

  data() {
    return {
      total: 0,
      info: {
        auditStatus: '0'
      },
      id: '',
      isShow: false,
      form: {
        pass: '',
        integralItem: '',
        opinion: '',
        auditIntegral: '',
      },
      ruleList: [],
      scoreItem: [],
      integralInfo: {},
    }
  },

  computed: {
    integralText() {
      if (!this.form.auditRuleId) {
        return ''
      }

      const integral = this.ruleList.filter(v => v.dictValue === this.form.auditRuleId)[0].integral

      return integral >= 0 ? `+${integral}分` : `${integral}分`
    }
  },

  created() {
    let loading = this.$loading({
      text: 'Loading',
    });
    if (this.params && this.params.id) {
      this.id = this.params.id
      this.dict.load(['atWillReportType', 'auditStatus']).then(() => {
        this.getInfo(this.params.id)
        this.$nextTick(() => {
          loading.close()
        })
      })
    }
  },

  methods: {
    getInfo(id) {
      this.instance.post(`/app/apppartyintegraldeclare/queryDetailById?id=${id}`).then(res => {
        if (res?.data) {
          this.info = res.data,
          this.integralInfo = res.data.integralRule
          if(res.data.integralRule.ruleType == 1) {
            this.scoreItem = JSON.parse(res.data.integralRule.ladderRule)?.map(e=>{
              return {
                dictName: e.viewCount,
                dictValue: e.integral.toString()
              }
            })
          }
          if(res.data.integralRule.ruleType == 0) {
            this.form.auditIntegral = this.integralInfo.integral
          }
        }
      })
    },

    onStatusChange() {
      this.$refs.form.clearValidate()
    },

    inputChange(e) {
      if(e > (this.integralInfo.integralMax || this.integralInfo.integralMax)) {
        this.$message.error("请输入区间范围内的分值")
        this.form.auditIntegral = ''
      } else {
        this.form.auditIntegral = e
      }
    },

    selectItem(val) {
      this.form.auditIntegral = val
    },

    onClose() {
      this.form = {
        pass: '',
        integralItem: '',
        opinion: '',
        auditIntegral: '',
      }
    },

    onConfirm() {
      if(this.integralInfo.ruleType == 1 && this.form.pass == 1) {
        this.form.integralItem = this.scoreItem.filter(e=> e.dictValue == this.form.auditIntegral)[0]?.dictName
      }
      if(this.integralInfo.ruleType == 0 && this.form.pass == 1) {
        this.form.auditIntegral = this.integralInfo.integral
      }
      // console.log(this.form.integralItem);
      this.$refs.form.validate(v => {
        if (v) {
          this.instance.post('/app/apppartyintegraldeclare/examine', null, {
            params: {
              ...this.form,
              id: this.params.id,
            }
          }).then(res => {
            if (res.code == 0) {
              this.isShow = false
              this.getInfo(this.params.id)
              this.$message.success('审核成功!')
            }
          })
        }
      })
    },
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'list',
        isRefresh: !!isRefresh
      })
    }
  }
}
</script>

<style scoped lang="scss">
.audit {
  .flex-warpper {
    display: flex;
    align-items: center;

    ::v-deep .el-form-item .el-form-item__content {
      margin-left: 0 !important;
    }

    ::v-deep .ai-select {
      margin: 0 !important;
    }

    ::v-deep .el-form-item {
      width: auto;
      margin-bottom: 0;

      &:last-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
