<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    :title="title"
    @ok="handleOk"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="modelRef" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed } from 'vue';
  import { checkUserApi, saveApi, 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 { companyId } = userinfo.value;
      console.log('companyId', companyId);
      const permListOptions = computed(() => {
        return [
          {
            label: '带看',
            value: '1',
          },
          {
            label: '拍摄',
            value: '2',
          },
        ];
      });
      const schemas: FormSchema[] = [
        {
          field: 'nickName',
          component: 'Input',
          label: '姓名',
          colProps: {
            span: 18,
          },
          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: 'roleId',
          component: 'ApiSelect',
          label: '角色',
          required: true,
          itemProps: {
            validateTrigger: 'blur',
          },
          colProps: {
            span: 18,
          },
          defaultValue: 8,
          componentProps: {
            options: [
              {
                label: '公司员工',
                value: 8,
                key: 8,
              },
            ],
          },
        },
        {
          field: 'phone',
          component: 'Input',
          label: '账号',
          required: true,
          colProps: {
            span: 18,
          },
          componentProps:{
            autoComplete:'off',
          }
        },
        {
          field: 'password',
          component: 'StrengthMeter',
          label: '密码',
          required: true,
          colProps: { span: 18 },
          componentProps:{
            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) {
        // 方式1;
        console.log('useModalInner', data);
        setFieldsValue({
          ...data,
          roleId: data.roleId != 2 ? data.roleId : '',
        });
        let setSchema = [
          {
            field: 'roleId',
            component: 'ApiSelect',
            componentProps: {
              disabled: data.roleId == 2 ? false : data.id ? true : false,
              api: getRoleListByParam,
              labelField: 'roleName',
              valueField: 'id',
              params: {
                type: data.roleId ? 1 : 0,
                roleId: data.roleId,
              },
            },
          },
          {
            field: 'phone',
            componentProps: {
              // disabled: getCheckRole('plat_admin') ? false : data.id ? true : false,
            },
          },
          {
            field: 'password',
            ifShow:data.id ? false:true,
          },
          {
            field: 'companyId',
            componentProps: {
              disabled: data.id ? (data.roleId != 2 ? true : false) : false,
            },
          },
          {
            field: 'permList',
            componentProps: {
              disabled: data.roleId == 2 ? false : data.id ? true : false,
              params: {
                companyId: data.companyId || companyId,
              },
            },
          },
        ];
        title.value = data.id ? '编辑' : '新增';
        updateSchema(setSchema);
      }
      function companyIdChange(companyId) {
        // resetFields(['permList'])
        updateSchema([
          {
            field: 'permList',
            componentProps: {
              params: {
                companyId: companyId,
              },
            },
          },
        ]);
        setFieldsValue({
          permList: [],
        });
      }
      async function handleOk() {
        let data = await validate();
        const requestApi = data.id ? updateApi : saveApi;
        let res = await requestApi({
          ...data,
          userName: data.phone,
          phone: data.phone,
          nickName: data.nickName,
          roleId: data.roleId,
          id: data.id,
        });
        console.log('res', res);
        context && context.emit('ok', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

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