<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.residentName"></ai-info-item>
            <ai-info-item label="申请时间" :value="info.createTime"></ai-info-item>
            <ai-info-item label="积分类型" :value="dict.getLabel('atWillReportType', info.applyIntegralType)"></ai-info-item>
            <ai-info-item label="规则类型" :value="info.residentName"></ai-info-item>
            <ai-info-item label="申请描述" :value="info.description" isLine></ai-info-item>
            <!-- <ai-info-item label="联系电话" isLine :value="info.residentPhone"></ai-info-item> -->
            <ai-info-item label="图片资料" isLine v-show="info.applyFiles">
              <ai-uploader v-model="info.applyFiles" 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="积分调整" isLine v-if="info.auditStatus === '1'"
                          :value="'+' + (info.auditIntegral >= 0 ? info.auditIntegral : info.auditIntegral) + '分'"></ai-info-item>
            <!-- 未通过（阶梯状态下显示加分项） -->
            <ai-info-item label="积分调整" isLine v-if="info.auditStatus === '1'"
                          :value="'+' + (info.auditIntegral >= 0 ? info.auditIntegral : info.auditIntegral) + '分'"></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="积分分值" prop="pass" style="width: 100%;" :rules="[{ required: true, message: '请输入积分分值' }]">
            <el-input v-model="num" placeholder="请输入1-10的整数"></el-input>
          </el-form-item>
          <!-- 区间 -->
          <el-form-item label="积分分值" prop="pass" style="width: 100%;" :rules="[{ required: true, message: '请输入积分分值' }]">
            <ai-range v-model="form.num" ref="resetagefoo" />
          </el-form-item>
          <!-- 阶梯 -->
          <el-form-item label="加分项" prop="pass" style="width: 100%;" :rules="[{ required: true, message: '请选择加分项' }]">
            <!-- <ai-select v-model="form.integralCalcType" :selectList="dict.getDict('integralCalcType')" @select="selectItem"/> -->
          </el-form-item>

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

          <el-form-item label="审核意见" v-if="form.pass == '0'" prop="opinion" 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() {
    const validatorRules = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入联系方式'))
      } else if (!/^1\d{10}$/.test(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      total: 0,
      info: {
        auditStatus: '0'
      },
      id: '',
      isShow: true,
      form: {
        auditIntegralType: '',
        auditRuleId: '',
        opinion: '',
        pass: ''
      },
      ruleList: [],
      num: ''
    }
  },

  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/appvillagerintegraldeclare/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.info = res.data
        }
      })
    },

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

    onClose() {
      this.form.auditIntegralType = ''
      this.form.auditRuleId = ''
      this.form.pass = ''
      this.form.opinion = ''
      this.id = ''
    },

    onConfirm() {
      this.$refs.form.validate(v => {
        if (v) {
          this.instance.post('/app/appvillagerintegraldeclare/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('审核成功!')
            }
          })
        }
      })
    },

    onChange(e) {
      this.form.auditRuleId = ''
      this.instance.post(`/app/appvillagerintegralrule/list?size=1000&classification=${e}`).then(res => {
        if (res?.code == 0) {
          this.ruleList = res.data.records.filter(v => v.ruleStatus === '1').map(v => {
            return {
              dictName: v.ruleName,
              dictValue: v.id,
              ruleName: v.ruleName,
              integral: v.integral
            }
          })
        }
      })
    },

    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>
