<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    @ok="handSubmit"
    title="提示"
    :min-height="250"
    @cancel="resetFields"
  >
    <div class="pt-3px pr-3px">
      <div style="position: relative;">
        <BasicForm @register="registerForm" />
      </div>
      <div style="padding-left: 100px; color: #9f9c9c; padding-bottom: 10px">
        禁用后，该平台无法打开，平台管理员身份将更改为普通警员一起分配到其他平台。
      </div>
      <span style="margin-left: 100px; color: #9f9c9c">注意：禁用后无法恢复，请谨慎操作。</span>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { encodeStr } from '/@/utils/encodeUtil';
  import { defineComponent, nextTick, ref, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { ReUcenterUserPassword } from '/@/api/staff/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { debounce } from 'lodash-es';
  import { platformallList } from '/@/api/account/index';
  import { disable } from '/@/api/jyUserPlatform/index'; //roleLIstApi

  const { createMessage, createConfirm } = useMessage();

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['ok'],
    setup(props, context) {
      const { t } = useI18n();
      const options = ref([]);
      let lastFetchId = 0;
      const fetchUser = debounce((value) => {
        console.log('fetching user', value);
        lastFetchId += 1;
        const fetchId = lastFetchId;
        options.value = [];
        fetch('https://randomuser.me/api/?results=5')
          .then((response) => response.json())
          .then((body) => {
            if (fetchId !== lastFetchId) {
              // for fetch callback order
              return;
            }
            const data = body.results.map((user) => ({
              label: `${user.name.first} ${user.name.last}`,
              value: user.login.username,
            }));
            options.value = data;
          });
      }, 300);
      const preventAutoFill = ref(true);
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          field: 'toPlatformId',
          component: 'ApiSelect',
          label: '平台',
          required: true,
          itemProps: {
            validateTrigger: 'blur',
          },
          colProps: {
            span: 20,
          },
          defaultValue: '',
          componentProps: {
            style: 'max-width: 285px',
            placeholder: '请选择平台',
            api: platformallList,
            labelField: 'platformName',
            listHeight: 150,
            valueField: 'id',
          },
        },
        // {
        //   field: 'password',
        //   component: 'ApiSelect',
        //   label: '平台',
        //   labelWidth: 100,
        //   required: true,
        //   componentProps: {
        //     placeholder: '请选择平台',
        //     api: platformallList,
        //     options: options.value,
        //     numberToString: true,
        //     labelField: 'name',
        //     valueField: 'value',
        //     immediate: true,
        //     search: fetchUser,
        //   },
        //   colProps: { span: 18 },
        // },
      ];
      const [
        registerForm,
        {
          setFieldsValue,
          validate,
          resetFields,
          // setProps
        },
      ] = useForm({
        labelWidth: 100,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        setFieldsValue({
          id: data.id,
        });
      }
      async function handSubmit() {
        console.log('submit');
        let param = await validate();
        console.log('submit', param);

        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: '是否确认停用迁移用户？',
          onOk: async () => {
            await disable(param);
            context && context.emit('ok');
            createMessage.success('平台已禁用');
            resetFields();
            closeModal();
          },
        });
      }
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        handSubmit,
        register,
        t,
        schemas,
        registerForm,
        handleVisibleChange,
        resetFields,
      };
    },
  });
</script>
