<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="编辑"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <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, 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 { companyList } from '/@/api/customer';
  // import { sceneMove } from '/@/api/operate';
  import { downTemplate } from '/@/api/customer';
  import { updateCameraType } from '/@/api/device';
  import { agentNewList } from '/@/api/dealer';
  import { CameraList } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import dayjs from 'dayjs';
  const { t } = useI18n();
  interface Option {
    value: string;
  }

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['reload'],
    setup(_, { emit }) {
      const modelRef = ref({
        isUpdate: false,
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          field: 'cameraType',
          component: 'Select',
          label: '设备类型',
          required: true,
          colProps: {
            span: 18,
          },
          componentProps: {
            options: [
              {
                label: t('routes.product.type.1'),
                value: '1',
                key: '1',
              },
              {
                label: t('routes.product.type.9'),
                value: '9',
                key: '9',
              },
              {
                label: t('routes.product.type.10'),
                value: '10',
                key: '10',
              },
              {
                label: t('routes.product.type.11'),
                value: '11',
                key: '1',
              },
            ],
          },
        },
      ];
      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;
        const { type } = data;
        resetFields();
        setFieldsValue({
          ...data,
          cameraType: type,
        });
      }

      const handleSubmit = async () => {
        try {
          const params = await validate();
          console.log('params', params);
          const res = await updateCameraType(params);
          console.log('res', res);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('reload');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function onFilterOption(inputText: string, option: Option) {
        if (option.label) {
          return option.label.toUpperCase().indexOf(inputText.toUpperCase()) >= 0;
        }
        return option.value.toUpperCase().indexOf(inputText.toUpperCase()) >= 0;
      }

      function handleVisibleChange(v) {
        // console.log(v);
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      async function getTemplate() {
        try {
          const res = await downTemplate({ type: 2 });
          console.log('res', res);
        } catch (error) {
          console.log('not passing', error);
        }
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        handleVisibleChange,
        onFilterOption,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
        getTemplate,
      };
    },
  });
</script>
