<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>
    </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 { cameraIn } from '/@/api/device'
  import { companyUploadExcel, downTemplate } from '/@/api/customer';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update',],
    setup(_, { emit }) {
      const modelRef = ref({});
      const fileFlow = reactive({
        file: null,
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'wifiName',
          component: 'Input',
          label: 'wifi名称',
          colProps: {
            span: 18,
          },
          helpMessage:'wifi名称需包含前缀，如“4DKKPRO_”、“4DKKMI_”、“4DSS_”、“4DKK_”。',
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                // var reg = /\S+@\S+\.\S+/;
                if (!value) {
                  return Promise.reject('请输入wifi名称');
                }
                var list:string[] = ['4DKKPRO_','4DKKMI_','4DSS_','4DKK_'];
                const some = list.some(item=>{
                  return value.indexOf(item) != -1
                })
                console.log('validator',some)
                if(!some){
                  return Promise.reject('wifi名称需包含前缀，如“4DKKPRO_”、“4DKKMI_”、“4DSS_”、“4DKK_”。');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
        }
      ];
      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,
        });
      }
      function handleVisibleChange() {

      }

      const handleSubmit = async () => {
        try {
          const params = await validate();
          await cameraIn(params);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
