<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="1000"
    :visible="visible"
    :maskClosable="false"
    :title="isUpdate ? '编辑选项' : '添加选项'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <ele-pro-table
      ref="tableRef"
      row-key="id"
      :columns="columns"
      :datasource="options"
      tool-class="ele-toolbar-form"
      class="sys-org-table"
    >
      <template #toolbar>
        <a-button type="primary" class="ele-btn-icon" @click="add">
          <template #icon>
            <PlusOutlined />
          </template>
          <span>新增</span>
        </a-button>
      </template>
      <template #bodyCell="{ column, record, index }">
        <template v-if="column.key === 'name'">
          <a-input v-model:value="record.value" />
        </template>
        <template v-if="column.key == 'action'">
          <a class="ele-text-danger" @click="remove(index)">删除</a>
        </template>
      </template>
    </ele-pro-table>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form } from 'ant-design-vue';
  import { type EleProTable } from 'ele-admin-pro';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import useFormData from '@/utils/use-form-data';
  import { PlusOutlined } from '@ant-design/icons-vue';

  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);
  const disabled = ref(false);
  // 编辑器内容，双向绑定
  const content = ref<any>('');

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: any | null;
  }>();

  const emit = defineEmits<{
    (e: 'done', {}): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 顶部图片
  const photo = ref<ItemType[]>([]);
  // 背景图片
  const background = ref<ItemType[]>([]);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);
  const formData = ref<[]>([]);

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<any>({
    id: undefined,
    type: '',
    name: '',
    checked: undefined,
    checkedList: [],
    options: [],
    radio: []
  });

  // 表格列配置
  const columns = ref<any[]>([
    {
      title: '选项',
      dataIndex: 'name',
      key: 'name'
    },
    {
      title: '操作',
      width: 180,
      key: 'action',
      align: 'center'
    }
  ]);

  const options = ref<any[]>([]);
  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  const add = () => {
    options.value.push({});
  };

  const remove = (index) => {
  options.value.splice(index, 1);
  }

  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        type: 'string',
        message: '请填写表单名称',
        trigger: 'blur'
      }
    ],
    path: [
      {
        required: true,
        type: 'string',
        message: '请填写路由地址',
        trigger: 'blur'
      }
    ],
    component: [
      {
        required: true,
        type: 'string',
        message: '请填写组件路径',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    form.options = options.value.map((d) => {
      return d.value;
    });
    updateVisible(false);
    emit('done', form);
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        options.value = [];
        if (props.data) {
          assignFields(props.data);
          props.data?.options.map((value) => {
            options.value.push({ value });
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
          options.value = [];
        }
      } else {
        resetFields();
        formRef.value?.clearValidate();
      }
    },
    { immediate: true }
  );
</script>
<style lang="less"></style>
