<script lang="ts">
export default {
  name: 'memberauditdetail'
};
</script>

<script setup lang="ts">
  import { onMounted, reactive, ref, toRefs } from 'vue'
  import {
    memberRealNameDetail,
    memberCheckPass,
    memberCheckNotPass
  } from '@/api/member/audit';
  import {
    AuditDetailResponse,
    CheckNotPassForm
  } from '@/types/api/member/audit';
  import { ElForm, ElMessage } from 'element-plus';
  import { Picture as IconPicture } from '@element-plus/icons-vue';
  import { useRoute } from 'vue-router';

  const dataFormRef = ref(ElForm);

  const state = reactive({
    loading: true,
    memberId: 0 as any,
    memberInfo: {} as AuditDetailResponse,
    srcList: {
      front: "",
      inHand: "",
      reverse: ""
    },
    radioPass: "2",
    ifPass: true,
    formData: {} as CheckNotPassForm
  });

  const {
    loading,
    memberInfo,
    srcList,
    radioPass,
    ifPass,
    formData
  } = toRefs(state);

  /**
   * 获取会员实名审核详情
   * @param id 会员id
   */
  function getMemberRealNameDetail(id: any) {
    state.loading = true;
    memberRealNameDetail({ id: id }).then(({ data }) => {
      state.memberInfo = data;
      state.srcList = {
        front: data.identityCardImgFront,
        inHand: data.identityCardImgInHand,
        reverse: data.identityCardImgReverse
      }
      state.loading = false;
    });
  }

  const submitFormData = () => {
    if (state.ifPass) {
      memberCheckPass(state.memberId).then((res: any) => {
        if (!res.code) {
          ElMessage.success(res.message)
          getMemberRealNameDetail(state.memberId)
        } else {
          ElMessage.error(res.message)
        }
      }).catch((err: any) => {
        console.log(err)
      })
    } else {
      dataFormRef.value.validate((valid: any) => {
        if (valid) {
          loading.value = true;
          memberCheckNotPass(state.memberId, state.formData).then((res: any) => {
            if (!res.code) {
              ElMessage.success(res.message);
              getMemberRealNameDetail(state.memberId);
              loading.value = false;
            } else {
              ElMessage.error(res.message);
              loading.value = false;
            }
          }).catch(() => {
            loading.value = false;
          })
        }
      });
    }
  }

  onMounted(() => {
    const route = useRoute();
    state.memberId = route.query.id;
    getMemberRealNameDetail(state.memberId);
  });
</script>

<template>
    <div class="app-container">
      <el-row class="row-detail">
        <el-col :span="10" class="fw-b">客户名称：</el-col>
        <el-col :span="14">
          {{ memberInfo.realName }}
        </el-col>
      </el-row>
      <el-row class="row-detail">
        <el-col :span="10" class="fw-b">身份证号码：</el-col>
        <el-col :span="14">
          {{ memberInfo.idCard }}
        </el-col>
      </el-row>
      <el-row class="row-detail">
        <el-col :span="6" class="col-img">
          <el-image :src="memberInfo.identityCardImgFront"
          :preview-src-list="Object.values(srcList)"
          :initial-index="0">
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
          <span class="img-alt">身份证正面</span>
        </el-col>
        <el-col :span="6" class="col-img">
          <el-image
          :src="memberInfo.identityCardImgInHand"
          :preview-src-list="Object.values(srcList)"
          :initial-index="1">
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
          <span class="img-alt">手持证件</span>
        </el-col>
        <el-col :span="6" class="col-img">
          <el-image :src="memberInfo.identityCardImgReverse"
          :preview-src-list="Object.values(srcList)"
          :initial-index="2">
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
          <span class="img-alt">身份证反面</span>
        </el-col>
      </el-row>
      <el-row class="row-detail row-res-pass">
        <el-col :span="24" class="col-res-pass">
          <div v-if="memberInfo.auditStatus === 0">
            <el-form
              ref="dataFormRef"
              :model="formData"
              label-width="100px"
            >
              <el-form-item label-width="0">
                <el-radio-group v-model="radioPass" @change="ifPass = !ifPass" class="ml-4" style="margin: 0 auto;">
                  <el-radio label="2" size="large">审核通过</el-radio>
                  <el-radio label="1" size="large">审核驳回</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label-width="0" prop="rejectReason">
                <el-col :span="12" style="margin: 0 auto;">
                <el-input
                  :rows="3"
                  v-show="!ifPass"
                  v-model="formData.rejectReason"
                  type="textarea"
                  placeholder="请输入" />
                </el-col>
              </el-form-item>
              <el-button type="success" :loading="loading" @click="submitFormData" style="width: 200px;">确 认</el-button>
            </el-form>
          </div>
          <div v-else-if="memberInfo.auditStatus === 2">审核通过</div>
          <div v-else-if="memberInfo.auditStatus === 1" style="color: #ed3f14; font-weight: bolder;">审核未通过</div>
        </el-col>
      </el-row>
    </div>
</template>

<style lang="scss" scoped>
  .row-detail {
    margin-top: 10px;
    margin-bottom: 20px;

    .col-img {
      height: 153px;
      text-align: center;
      margin: 0 auto;
      .el-image {
        width: 100%;
        height: 100%;
        cursor: pointer;
      }
      .image-slot {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        background: var(--el-fill-color-light);
        color: var(--el-text-color-secondary);
        font-size: 30px;
        .el-icon {
          font-size: 30px;
        }
      }
      .img-alt {
        display: block;
        color: var(--el-text-color-secondary);
        font-size: 14px;
        margin-top: 20px;
      }
    }
  }
  .row-res-pass {
    margin-top: 100px;
    .col-res-pass {
      width: 100%;
      text-align: center;
    }
  }
  .fw-b {
    font-weight: bold;
    text-align: right;
  }
</style>
