<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.devices.editCamera')"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" />
    </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';
  // allCompanyApi
  import { editCameraApi } from '/@/api/device/list';
  import { useI18n } from '/@/hooks/web/useI18n';

  //   address: "11111"
  // balance: "111"
  // cameraType: "4"
  // childName: "11111"
  // companyId: 1146
  // orderSn: "1111"
  // own: "0"
  // rnd: 0.9923218970879999
  // snCode: "1111"
  // wifiName: "1111"
  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: 'cameraId',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          field: 'own',
          component: 'Select',
          label: t('routes.devices.own'),
          colProps: {
            span: 24,
          },
          componentProps: {
            // disabled: true,
            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: {
            disabled: true,
            options: [4, 1, 9, 10, 6].map((ele) => {
              return { value: ele, key: ele, label: rendercameraTypeLabel(ele) };
            }),
            // onChange: function (val) {
            //   console.log('appendSchemaByField', val);
            //   if (val !== 6) {
            //     appendSchemaByField(
            //       {
            //         field: 'snCode',
            //         component: 'Input',
            //         label: t('routes.devices.snCode'),
            //         required: true,
            //         colProps: {
            //           span: 24,
            //         },
            //       },
            //       'address',
            //     );
            //   } else {
            //     removeSchemaByFiled('snCode');
            //   }
            // },
          },
        },

        {
          field: 'childName',
          component: 'Input',
          label: t('routes.devices.childName'),
          required: true,
          colProps: {
            span: 24,
          },
          componentProps: {
            disabled: true,
          },
        },
        {
          field: 'wifiName',
          component: 'Input',
          label: t('routes.devices.wifiName'),
          required: true,
          componentProps: {
            disabled: true,
          },
          colProps: {
            span: 24,
          },
        },
        {
          field: 'address',
          component: 'Input',
          required: true,
          label: t('routes.devices.address'),
          componentProps: {
            disabled: true,
          },
          colProps: {
            span: 24,
          },
        },
        {
          field: 'snCode',
          component: 'Input',
          label: t('routes.devices.snCode'),
          required: true,
          componentProps: {
            disabled: true,
          },
          ifShow: ({ model }) => {
            console.log('record', model.cameraType);

            return model.cameraType && model.cameraType !== 6;
          },
          colProps: {
            span: 24,
          },
        },
        // {
        //   field: 'balance',
        //   component: 'Input',
        //   label: t('routes.devices.balance'),
        //   colProps: {
        //     span: 24,
        //   },
        // },

        // {
        //   field: 'companyId',
        //   component: 'ApiSelect',
        //   label: t('routes.devices.companyId'),
        //   componentProps: {
        //     api: allCompanyApi,
        //     numberToString: true,
        //     labelField: 'companyName',
        //     valueField: 'id',
        //     immediate: true,
        //     params: {
        //       page: 1,
        //       limit: 1000,
        //     },
        //   },
        //   colProps: {
        //     span: 24,
        //   },
        // },
      ];
      const [
        registerForm,
        {
          // getFieldsValue,
          // validateFields,
          // appendSchemaByField,
          // removeSchemaByFiled,
          setFieldsValue,
          validate,
          // setProps
        },
      ] = 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);
        setFieldsValue({
          ...data,
          cameraId: Number(data.id),
          cameraType: Number(data.cameraType),
        });
      }
      const handleSubmit = async () => {
        // console.log('getFieldsValue()', getFieldsValue());
        try {
          const values = await validate();
          console.log('values', values);
          if (values) {
            const res = await editCameraApi(values);
            emit('update');
            console.log('res', res);
            createMessage.success(t('common.optSuccess'));
            closeModal();
          }
        } 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,
        t,
      };
    },
  });
</script>
