<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    :title="title"
    @ok="handleOk"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed } from 'vue';
  import { checkUserApi, saveAddApi, updateApi, getRoleListByParam } from '/@/api/staff/list'; //roleLIstApi
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useUserStore } from '/@/store/modules/user';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['ok'],
    setup(_, context) {
      const modelRef = ref({});
      const userStore = useUserStore();
      const userinfo = computed(() => userStore.getUserInfo);
      const preventAutoFill = ref(true);
      const { companyId } = userinfo.value;
      const schemas: FormSchema[] = [
        {
          field: 'nickName',
          component: 'Input',
          label: '姓名',
          colProps: {
            span: 20,
          },
          componentProps: {
            maxLength: 15,
          },
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                var reg_tel = /^[a-zA-Z\u4e00-\u9fa5]+$/;
                // var reg = /\S+@\S+\.\S+/;
                if (!value) {
                  return Promise.reject('请输入姓名');
                }
                if (!reg_tel.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject('请输入正确的姓名');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
        },
        {
          field: 'userName',
          component: 'Input',
          label: '账号',
          required: true,
          colProps: {
            span: 20,
          },
          componentProps:{
            placeholder:"请输入手机号",
            maxLength: 15,
            readonly:preventAutoFill.value
          },
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                console.log('value',value)
                const regPos = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/; // 非中文
                if (!value) {
                  return Promise.reject('请输入正确的手机号');
                }
                if (!regPos.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject('请输入正确的手机号');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
        },
        {
          field: 'password',
          component: 'StrengthMeter',
          label: '密码',
          required: true,
          colProps: {
            span: 20,
          },
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                console.log('value',value)
                const regPos = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; // 非中文
                if (!value) {
                  return Promise.reject('请输入8-16位数字、字母大小写组合');
                }
                if (!regPos.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject('请输入8-16位数字、字母大小写组合');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          componentProps:{
            placeholder:"请输入8-16位数字、字母大小写组合",
            maxLength: 16,
            readonly:preventAutoFill.value,
            minLength: 8,
            autoComplete:'off',
          }
        },
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
      ];
      const title = ref('新增账号');
      const { createMessage } = useMessage();
      const [registerForm, { setFieldsValue, validate, updateSchema, resetFields }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        console.log('onDataReceive',data)
        // 方式1;
        setTimeout(()=>{
          preventAutoFill.value = false
          updateSchema([{
            field: 'userName',
            componentProps:{
              readonly:false,
            }
          },{
            field: 'password',
            componentProps:{
              readonly:false,
            }
          }])
          // updateSchema(schemas);
        },500)
      }
      function companyIdChange(companyId) {
        // resetFields(['permList'])
        updateSchema([
          {
            field: 'permList',
            componentProps: {
              params: {
                companyId: companyId,
              },
            },
          },
        ]);
        setFieldsValue({
          permList: [],
        });
      }
      async function handleOk() {
        let data = await validate();
        const requestApi =  saveAddApi; //data.id ? updateApi :
        let res = await requestApi({
          ...data,
          // userName: data.phone,
          phone: data.phone,
          nickName: data.nickName,
          roleId: data.roleId,
          id: data.id,
        });
        context && context.emit('ok', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

      return {
        register,
        title,
        preventAutoFill,
        schemas,
        registerForm,
        modelRef,
        handleOk,
        resetFields,
      };
    },
  });
</script>
