<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    :title="title"
    @ok="handleOk"
    min-height="350"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, reactive } from 'vue';
  import { staffPermUpdate, bindStaffList } from '/@/api/rightsEnterprises/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';
  import dayjs from 'dayjs';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['ok', 'register'],
    setup(_, context) {
      const modelRef = ref({
        isSee: false,
      });
      const userStore = useUserStore();
      const userIdList = reactive({
        list: [],
      });
      const userinfo = computed(() => userStore.getUserInfo);
      const { companyId } = userinfo.value;
      console.log('companyId', companyId);
      const schemas: FormSchema[] = [
        {
          field: 'permName',
          label: t('routes.rightsEnterprises.permName'),
          slot: 'text',
          component: 'Input',
        },
        {
          field: 'expirationTime',
          component: 'DatePicker',
          label: '权益过期时间',
          colProps: {
            span: 22,
          },
          componentProps: {
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            format: 'YYYY-MM-DD HH:mm:ss',
            showTime: {
              defaultValue: dayjs('00:00:00', 'HH:mm:ss'),
            },
            disabledDate: (current) => {
              return current && current < dayjs().endOf('day');
            },
            // disabledDateTime:()=>{
            //   return {
            //     disabledHours: () => range(0, 24).splice(4, 20),
            //     disabledMinutes: () => range(30, 60),
            //     disabledSeconds: () => [55, 56],
            //   };
            // }
          },
        },
        {
          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);
      });
      async function onDataReceive(data) {
        // 方式1;
        let list = await bindStaffList({
          companyId: data.companyId,
          staffPermId: data.id,
        });
        userIdList.list = list.map((ele) => {
          return {
            ...ele,
            label: ele.staffName,
            value: ele.staffId,
            key: ele.staffPhone,
          };
        });
        console.log('handleDelete', userIdList.list);
        modelRef.value.isSee = data.userId ? true : false;
        title.value = data.userId ? '查 看' : '绑 定';
        setFieldsValue({
          ...data,
          userId: data.userId ? data.staffName : data.userId,
        });
        let setSchema = [
          {
            field: 'userId',
            required: !data.userId,
            slot: !!data.userId ? 'text' : false,
          },
          {
            field: 'userId',
            componentProps: {
              options: userIdList.list,
            },
          },
        ];
        updateSchema(setSchema);
      }
      async function handleOk() {
        // if (modelRef.value.isSee) {
        //   return closeModal();
        // }
        let data = await validate();
        let res = await staffPermUpdate(data);
        context && context.emit('update', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

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