<template>
  <el-form :model="formData" label-width="120px" ref="ruleForm" :rules="rules" v-loading="loading" class="pr-4">
    <div class="flex flex-row justify-between">
      <div class="w-full">
        <el-form-item label="电子发票" prop="ticket">
          <my-upload acceptType=".png,.jpg,.jpeg" :isMultiple="true" :maxFileSize="2" @fileRemove="fileRemove"
            @fileSuccess="fileSuccess" :fileList="fileList" action="/qiniu/upload/image"></my-upload>
        </el-form-item>
      </div>
    </div>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(ruleForm)">{{ $t('system.confirm') }}</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import { InfoFilled } from '@element-plus/icons-vue';
import type { FormInstance, FormRules } from 'element-plus'
import { jiesuanManageJiesuanUpdateTicket } from '@/api/settlement/index'
const ruleForm = ref<FormInstance>()
import { onMounted, ref } from 'vue';
const props = defineProps({
  primary: Object,
});
const formData = ref({})
const fileList = ref([])
const rules = reactive({
  ticket: [
    {
      required: true,
      message: '请上传电子发票',
    }
  ],
});
const loading = ref(false)
const fileSuccess = (e) => {
  console.log(e);
  formData.value.ticket = e;
}
const fileRemove = (e) => {
  console.log(e);
  formData.value.ticket = '';
}

const submitForm = (formEl: FormInstance | undefined) => {
  console.log(formData.value, 'formData.valueformData.value');
  if (!formEl) return;
  formEl
    .validate(valid => {
      if (valid) {
        jiesuanManageJiesuanUpdateTicket(formData.value.id, formData.value).then(res => {
          console.log(res);
          ElMessage.success(res.message)
          emit('close')
        })
      } else {
        loading.value = false;
      }
    })
    .then(() => { });
}

if (props.primary) {
  formData.value.id = props.primary.id
  if (props.primary.ticket_link) {
    fileList.value = [{ uid: props.primary.id, url: props.primary.ticket_link }]
  }
}
const emit = defineEmits(['close']);
onMounted(() => {
});
</script>
