<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="'带看套餐'"
    :minHeight="200"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
    @cancel="resetFields"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  // import { checkUserAddAble } from '/@/api/corporation/modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { updateLiveRoomInfoApi } from '/@/api/device/list';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['register', 'submit'],
    setup(props, { emit }) {
      const modelRef = ref({
        ifShow: true,
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          show: false,
          field: 'canShow',
          component: 'RadioGroup',
          label: '是否支持带逛',
          required: true,
          colProps: {
            span: 22,
          },
          defaultValue: '0',
          componentProps: {
            options: [
              {
                label: '支持',
                value: 1,
              },
              {
                label: '不支持',
                value: 0,
              },
            ],
            onChange: (e) => {
              console.log('data', e.target.value);
              updateSchema({
                field: 'liveRoomCapacities',
                // ifShow: e.target.value == 1 ? true : false,
              });
            },
          },
        },
        {
          field: 'liveRoomCapacities',
          component: 'Select',
          label: '套餐',
          required: true,
          colProps: {
            span: 22,
          },
          // ifShow: modelRef.value.ifShow,
          componentProps: {
            options: [
              {
                label: '1+4',
                value: 5,
              },
              {
                label: '1+29',
                value: 30,
              },
              {
                label: '1+39',
                value: 40,
              },
              {
                label: '1+49',
                value: 50,
              },
              {
                label: '1+99',
                value: 100,
              },
            ],
            onChange: (data, item) => {
              console.log('data', data, item);
            },
          },
        },
      ];
      const [
        registerForm,
        {
          // getFieldsValue,
          setFieldsValue,
          // setProps
          resetFields,
          updateSchema,
          validate,
        },
      ] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        console.log('insertData', data);
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        modelRef.value.ifShow = data.canShow == 1 ? true : false;
        console.log('insertData', modelRef.value.ifShow);
        setFieldsValue({ ...data });
        updateSchema({
          field: 'liveRoomCapacities',
          // ifShow: modelRef.value.ifShow,
        });
      }
      const handleSubmit = async () => {
        const values = await validate();
        //TODO hack parameter
        console.log('values', values);
        const res = await updateLiveRoomInfoApi({
          id: values.id,
          canShow: values.canShow,
          liveRoomCapacities: values.liveRoomCapacities,
        });
        // let res = await checkUserAddAble({ phoneNum: values.managerPhone });
        console.log('insertData', res);
        emit('submit');
        closeModal();
        resetFields();
        createMessage.success(t('common.optSuccess'));
        // createMessage.success(t('routes.corporation.optSuccess'));
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

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