<template>
  <el-dialog
    v-model="visible"
    title="签署责任书"
    width="540px"
    class="sign-responsibility-dialog"
    :close-on-click-modal="false"
    align-center
    append-to-body
    destroy-on-close
    @closed="onClosed"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="110px" class="sign-form">
      <el-form-item label="签署日期：" prop="signDate">
        <el-date-picker
          v-model="form.signDate"
          type="date"
          placeholder="选择日期"
          value-format="YYYY-MM-DD"
          style="width: 100%"
        />
      </el-form-item>

      <el-form-item label="上传责任书：" prop="fileList">
        <el-upload
          v-model:file-list="form.fileList"
          class="sign-upload"
          list-type="picture-card"
          :auto-upload="false"
          :limit="1"
          accept="image/*,.pdf"
          :on-exceed="onExceed"
        >
          <el-icon class="sign-upload__plus"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="warning" :loading="submitting" @click="onSubmit">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed, reactive, ref, watch } from "vue";
import { Plus } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  /** 当前签署的人员信息 */
  person: {
    type: Object,
    default: null,
  },
});

const emit = defineEmits(["update:modelValue", "submit"]);

const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit("update:modelValue", val),
});

const formRef = ref(null);
const submitting = ref(false);

const createForm = () => ({
  signDate: "",
  fileList: [],
});

const form = reactive(createForm());

const rules = {
  signDate: [{ required: true, message: "请选择签署日期", trigger: "change" }],
  fileList: [
    {
      required: true,
      validator: (_rule, value, callback) => {
        if (!value?.length) {
          callback(new Error("请上传责任书"));
          return;
        }
        callback();
      },
      trigger: "change",
    },
  ],
};

watch(
  () => props.modelValue,
  (val) => {
    if (val) {
      Object.assign(form, createForm());
      formRef.value?.clearValidate?.();
    }
  }
);

const onExceed = () => {
  ElMessage.warning("最多上传 1 个文件");
};

const onClosed = () => {
  Object.assign(form, createForm());
  formRef.value?.clearValidate?.();
};

const onSubmit = async () => {
  if (!formRef.value) return;
  try {
    await formRef.value.validate();
  } catch {
    return;
  }

  submitting.value = true;
  try {
    emit("submit", {
      person: props.person,
      signDate: form.signDate,
      fileList: [...form.fileList],
    });
    visible.value = false;
  } finally {
    submitting.value = false;
  }
};
</script>

<style lang="scss" scoped>
$primary: $warning-color;

.sign-form {
  padding: 8px 12px 0;

  :deep(.el-form-item__label) {
    color: #333;
  }
}

.sign-upload {
  :deep(.el-upload--picture-card) {
    width: 80px;
    height: 80px;
    border-radius: 4px;
  }

  :deep(.el-upload-list--picture-card .el-upload-list__item) {
    width: 80px;
    height: 80px;
    border-radius: 4px;
  }

  &__plus {
    font-size: 22px;
    color: #909399;
  }
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding-bottom: 4px;

  :deep(.el-button--warning) {
    background: $primary;
    border-color: $primary;
  }
}
</style>

<style lang="scss">
.sign-responsibility-dialog {
  .el-dialog__header {
    margin-right: 0;
    padding-bottom: 12px;
    border-bottom: 1px solid #f0f0f0;
  }

  .el-dialog__body {
    padding: 20px 20px 8px;
  }

  .el-dialog__footer {
    padding-top: 12px;
    border-top: 1px solid #f0f0f0;
  }
}
</style>
