<template>
  <a-modal v-model:open="visible" :width="334" centered>
    <template #title> 填涂区设置 </template>

    <a-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      :label-col="{ style: { width: '60px' } }"
    >
      <a-form-item label="未填涂">
        <div class="color-block color-unfill"></div>
        <a-checkbox v-model:checked="formData.unfillShow">显示</a-checkbox>
      </a-form-item>
      <a-form-item label="已填涂">
        <div class="color-block color-fill"></div>
        <a-checkbox v-model:checked="formData.fillShow">显示</a-checkbox>
      </a-form-item>
      <a-form-item name="borderWidth" label="宽度">
        <a-input-number
          v-model:value="formData.borderWidth"
          style="width: 40px; margin-right: 4px"
          :min="1"
          :max="9"
          :precision="0"
          :controls="false"
        />
        <span class="input-tips">像素</span>
      </a-form-item>
    </a-form>

    <template #footer>
      <div class="box-justify">
        <a-button type="link" @click="initSet">恢复默认值</a-button>

        <div>
          <a-button type="text" @click="close">取消</a-button>
          <a-button type="primary" @click="confirm">确认</a-button>
        </div>
      </div>
    </template>
  </a-modal>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from "vue";
import { message } from "ant-design-vue";

import useModal from "@/hooks/useModal";
import { useUserStore } from "@/store";
import { objModifyAssign } from "@/utils/tool";

defineOptions({
  name: "FillAreaSetDialog",
});

/* modal */
const { visible, open, close } = useModal();
defineExpose({ open, close });

const userStore = useUserStore();

const defaultFormData = {
  fillColor: "#f53f3f ",
  fillShow: true,
  unfillColor: "#165DFF",
  unfillShow: false,
  borderWidth: 2,
};

const emit = defineEmits(["modified"]);

type FormDataType = typeof defaultFormData;
const formRef = ref();
const formData: UnwrapRef<FormDataType> = reactive({
  ...defaultFormData,
});
const rules: FormRules<keyof FormDataType> = {
  borderWidth: [
    {
      required: true,
      message: "请输入",
      trigger: "change",
    },
  ],
};

function initSet() {
  objModifyAssign(formData, defaultFormData);
}

async function confirm() {
  const valid = await formRef.value?.validate().catch(() => false);
  if (!valid) return;

  userStore.setRecogFillSet(formData);

  message.success("保存成功！");
  emit("modified", formData);
  close();
}

/* init modal */
watch(
  () => visible.value,
  (val) => {
    if (val) {
      modalOpenHandle();
    }
  },
  {
    immediate: true,
  }
);

function modalOpenHandle() {
  objModifyAssign(formData, userStore.recogFillSet || defaultFormData);
}
</script>

<style lang="less" scoped>
.color-block {
  display: inline-block;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  margin-right: 8px;

  &.color-unfill {
    background: #165dff;
  }
  &.color-fill {
    background: #f53f3f;
  }
}
.input-tips {
  display: inline-block;
  vertical-align: middle;
  margin-top: 3px;
}
</style>
