<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    :title="title"
    @ok="handleOk"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm">
        <template #permList="{ model, field }">
          <a-select
            :options="permListOptions"
            mode="multiple"
            v-model:value="model[field]"
            allowClear
          />
        </template>
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, reactive } from 'vue';
  import { bindRights, 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';
  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 permListOptions = computed(() => {
        return [
          {
            label: '带看',
            value: '1',
          },
          {
            label: '拍摄',
            value: '2',
          },
        ];
      });
      const schemas: FormSchema[] = [
        {
          field: 'permName',
          component: 'Input',
          label: '权益',
          colProps: {
            span: 18,
          },
          required: false,
          slot: 'text',
        },
        {
          field: 'userId',
          label: '员工名称',
          component: 'Select',
          itemProps: {
            validateTrigger: 'blur',
          },
          colProps: {
            span: 22,
          },
          required: true,
          // ifShow: getCheckRole('plat_admin'),
          componentProps: {
            options: userIdList.list,
            onChange: userIdChange,
          },
        },
        {
          field: 'staffPhone',
          component: 'Input',
          label: '手机号',
          colProps: {
            span: 22,
          },
          componentProps: {
            maxLength: 15,
          },
          slot: 'text',
        },
        {
          field: 'activationTime',
          component: 'Input',
          label: '权益激活时间',
          colProps: {
            span: 22,
          },
          defaultValue: 8,
          slot: 'text',
        },
        {
          field: 'expirationTime',
          component: 'Input',
          label: '权益过期时间',
          colProps: {
            span: 22,
          },
          defaultValue: 8,
          slot: 'text',
        },
        {
          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);
      }
      function userIdChange(val, item) {
        console.log('handleDelete', val, item);
        setFieldsValue({
          ...item,
        });
      }
      async function handleOk() {
        if (modelRef.value.isSee) {
          return closeModal();
        }
        let data = await validate();
        let res = await bindRights({
          userId: data.userId,
          id: data.id,
        });
        context && context.emit('update', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

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