<template>
  <w-dialog ref="dialogRef" title="属性配置" width="80%" height="80%" :buttons="dialog.buttons">
    <w-form ref="formRef" :cols-num="1" :fields="form.fields" class="p-1.5"> </w-form>
  </w-dialog>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue';
import { NotifyManager, Tools } from 'platform-core';

const emit = defineEmits<{
  (
    e: 'setForEachParams', // 设置循环属性
    formData: any, // 循环属性对象
  ): void;
}>();

const dialogRef = ref();
const formRef = ref();

const dialog = {
  buttons: [
    {
      icon: 'save',
      label: '确定',
      click: async () => {
        const validate = await formRef.value.validate();
        if (validate) {
          const formData = formRef.value.getData();
          let flag = false;
          Object.keys(formData).forEach((item) => {
            if (!Tools.isEmpty(formData[item])) {
              flag = true;
            }
          });
          if (flag) {
            emit('setForEachParams', formData);
            dialogRef.value.hide();
          } else {
            NotifyManager.warn('未配置任何属性');
          }
        }
      },
    },
  ],
};
const form = {
  fields: [
    {
      label: '单元格格式',
      name: 'cellFormat',
      type: 'w-select',
      options: [
        { label: '纯文本', value: '1' },
        { label: '数字格式-整数', value: '2' },
        { label: '数字格式-两位小数', value: '3' },
        { label: '数字格式-四位小数', value: '4' },
        { label: '百分比整数', value: '5' },
        { label: '百分比两位小数', value: '6' },
        { label: '万元两位小数(示例：12万3456.00)', value: '7' },
        { label: '千位符整数(示例：1,235)', value: '8' },
        { label: '千位符两位小数(示例：1,234.56)', value: '9' },
      ],
    },
    {
      label: '单元格码值转换',
      name: 'cellValueTransformed',
      type: 'w-text',
      hint: `当值匹配上时显示转换后的值，等号左边为原始值，右边为转换值，位置需对应，英文逗号分隔。示例：1,2=年,月 转换为空示例：-999=NULL`,
    },
    { label: '单元格批注', name: 'cellComment', type: 'w-textarea' },
  ],
};

const show = (data) => {
  dialogRef.value.show();
  nextTick(() => {
    if (data) {
      formRef.value.setData(data);
    }
  });
};
const hide = () => {
  dialogRef.value.hide();
};

defineExpose({
  show,
  hide,
});
</script>
