<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" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { allCompanyApi, addCameraApi } from '/@/api/device/list';
  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 { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'own',
          component: 'Select',
          label: t('routes.devices.own'),
          colProps: {
            span: 24,
          },
          componentProps: {
            options: [0, 2, 1, 3].map((ele) => {
              return { value: ele, key: ele, label: renderOwnTypeLabel(ele) };
            }),
          },
          required: true,
        },
        {
          field: 'cameraType',
          component: 'Select',
          label: t('routes.devices.cameraType'),
          required: true,
          colProps: {
            span: 24,
          },
          componentProps: {
            options: [4, 1, 9, 10, 6].map((ele) => {
              return { value: ele, key: ele, label: rendercameraTypeLabel(ele) };
            }),
          },
        },
        {
          field: 'childName',
          component: 'Input',
          label: t('routes.devices.childName'),
          required: true,
          colProps: {
            span: 24,
          },
        },
        {
          field: 'wifiName',
          component: 'Input',
          required: true,
          label: t('routes.devices.wifiName'),
          colProps: {
            span: 24,
          },
        },
        {
          field: 'address',
          required: true,
          component: 'Input',
          label: t('routes.devices.address'),
          colProps: {
            span: 24,
          },
        },
        {
          field: 'snCode',
          component: 'Input',
          label: t('routes.devices.snCode'),
          required: true,
          ifShow: ({ model }) => {
            console.log('record', model['cameraType']);
            if (model['cameraType'] && [4, 1, 9, 10].includes(model['cameraType'])) {
              return true;
            } else {
              return false;
            }
          },
          colProps: {
            span: 24,
          },
        },
        {
          field: 'balance',
          component: 'Input',
          required: true,
          label: t('routes.devices.balance'),
          colProps: {
            span: 24,
          },
        },
        {
          field: 'orderSn',
          component: 'Input',
          label: t('routes.devices.orderSn'),
          colProps: {
            span: 24,
          },
          required: true,
        },
        {
          field: 'companyId',
          component: 'ApiSelect',
          label: t('routes.devices.companyId'),
          required: true,
          componentProps: {
            api: allCompanyApi,
            numberToString: true,
            labelField: 'name',
            valueField: 'id',
            immediate: true,
            params: {
              page: 1,
              limit: 1000,
            },
          },
          colProps: {
            span: 24,
          },
        },
      ];
      const [registerForm, { validate, resetFields }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function renderOwnTypeLabel(type: number): string {
        switch (type) {
          case 0:
            return t('routes.devices.ownName.0');
          case 2:
            return t('routes.devices.ownName.2');
          case 1:
            return t('routes.devices.ownName.1');
          case 3:
            return t('routes.devices.ownName.3');
          default:
            return '';
        }
      }
      function rendercameraTypeLabel(cameraType: number): string {
        switch (cameraType) {
          case 4:
            return t('routes.devices.cameraName.4');
          case 1:
            return t('routes.devices.cameraName.1');
          case 9:
            return t('routes.devices.cameraName.9');
          case 10:
            return t('routes.devices.cameraName.10');
          case 6:
            return t('routes.devices.cameraName.6');
          default:
            return '';
        }
      }
      function onDataReceive(data) {
        console.log('Data Received', data);
        const { addList } = data;
        if (typeof addList === 'function') {
          //是函数    其中 FunName 为函数名称
          addListFunc = addList;
        }
      }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          const res = await addCameraApi(params as any);
          console.log('res', res);
          closeModal();
          resetFields();
          createMessage.success(res);
          emit('update');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        register,
        rendercameraTypeLabel,
        renderOwnTypeLabel,
        schemas,
        registerForm,
        model: modelRef,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
