<template>
  <Mask :width="540" :height="363" :is-show="isExchange" @close="close" :title="'兑换积分'"
        :show-footer="true">
    <div class="content_grant">
      <div class="head">
        <img style="width: 60px;height: 60px"
             :src="'/static/images/member/' + (props.info.sex === 1 ? 'man' : 'women') + '.png'"
             alt="">
        <div class="right">
          <div class="name">
            {{ props.info.name }}
          </div>
          <div class="text">
            <div class="phone">
              {{ props.info.phone }}
            </div>
            <div class="grant-name">
              积分：
              <img style="width: 16px;height: 16px" src="/static/images/member/1.png" alt="">
              <span class="num">{{ props.info.integralBalance }}</span>
            </div>
          </div>
        </div>
      </div>
      <div style="width: 100%">
        <el-form
            label-position="top" :rules="rules" ref="formDataRef" :model="data"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="兑换积分" prop="integral">
                <el-input placeholder="请输入兑换积分" v-model="data.integral"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="兑换备注" prop="remark">
                <el-input placeholder="请输入兑换备注" v-model="data.remark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </div>
    </div>
    <template #footer>
      <div class="footer">
        <div>
          <el-checkbox v-model="printReceipt">同时打印凭证</el-checkbox>
        </div>
        <div style="display:flex;">
          <div class="small-btn" @click="close">取消</div>
          <div class="small-btn" @click="reduceIntegral">兑换</div>
        </div>
      </div>
    </template>
  </Mask>
</template>
<script setup lang="ts">
import {ref, defineEmits, defineProps} from 'vue'
import {ElFormItem} from "element-plus";
import {post} from "@/utils/request.ts";
import Mask from "@/components/common/Mask.vue";

const isExchange = defineModel()
const emit = defineEmits(['close'])
const props = defineProps({
  info: {
    type: Object,
    default: () => ({})
  }
})
const printReceipt = ref(false)
const close = () => {
  data.value = {}
  emit('close')
}
const rules = {
  integral: [
    {required: true, message: '请输入兑换积分', trigger: 'blur'},
    {min: 1, message: '积分不能小于1', trigger: 'blur'}
  ],
  remark: [
    {required: true, message: '请输入兑换备注', trigger: 'blur'},
  ]
}
const data = ref<any>({
  integral: 0,
});
const formDataRef = ref();
const reduceIntegral = () => {
  formDataRef.value?.validate((valid: boolean) => {
    if (!valid) {
      return;
    }
    // 如果验证通过，发送请求
    data.value.integral = data.value.integral * -1
    data.value.vipId = props.info.id
    post("vip/integral/add", data.value).then((res: any) => {
      formDataRef.value?.resetFields();
      close()
    })
  });

}

</script>
<style scoped lang="scss">
.content_grant {
  height: 100%;
  padding: 24px;

  .head {
    height: 60px;
    display: flex;
    align-items: center;
    margin-bottom: 16px;

    .right {
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
      justify-content: center;
      margin-left: 16px;

      .name {
        font-weight: bold;
        font-size: 24px;
        color: #333333;
        line-height: 33px;
        text-align: left;
        font-style: normal;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .text {
        margin-top: 4px;
        display: flex;

        .phone {
          margin-right: 20px;
        }

        .grant-name {
          display: flex;

          .num {
            font-weight: bold;
            font-size: 24px;
            color: #4D6DE4;
            font-style: normal;
            line-height: 16px;
            margin-left: 6px;
          }
        }

      }
    }
  }
}

.footer {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px;
}
</style>