<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="迁移场景"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
    :min-height="0"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
      <span>注意：迁移后该场景的权限配置将被清空，如需保留，请复制后再做迁移</span>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { sceneMove } from '/@/api/operate';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const fileFlow = reactive({
        file: null,
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'type',
          component: 'Input',
          // defaultValue: '场景标题',
          label: '场景标题',
          slot: 'text',
          colProps: {
            span: 24,
          },
          // required: true,
        },
        {
          field: 'snCode',
          component: 'Input',
          label: '迁移至',

          required: true,
          colProps: {
            span: 16,
          },
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                if (!value) {
                  return Promise.reject('请输入人员编号');
                }
                // if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
                //   /* eslint-disable-next-line */
                //   return Promise.reject('不支持中文字符');
                // }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          componentProps: {
            placeholder: '请输入人员编号',
            maxLength: 15,
            onChange: (data) => {
              console.log('data', data);
            },
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        console.log(data);
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        modelRef.value = data;
        resetFields();
        setFieldsValue({
          type: data.sceneName,
        });
      }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          const apiData = {
            ryNo: params.snCode,
            num: modelRef.value.num,
          };
          console.log('res', apiData, params);
          const res = await sceneMove(apiData);
          console.log('res', res);
          closeModal();
          resetFields();
          createMessage.success('场景迁移成功。');
          emit('update');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        // console.log(v);
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
