<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="提示"
    @cancel="resetFields"
    @ok="handleSubmit"
    ok-text="跳转到案件列表"
    :width="550"
    :min-height="0"
  >
    <div class="pt-2px pr-3px">
      <div class="pl-4">该场景已被纳入案件，请跳转到案件列表修改权限或将场景移出案件。</div>
      <!-- <BasicForm @register="registerForm" :model="model">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm> -->
      <BasicTable class="powesrTable" @register="registerTable" :dataSource="dataSource">
      </BasicTable>
      <detailModal @register="registerDetail" @update="reload" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { sceneMove } from '/@/api/operate';
  import { useModal } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  import detailModal from './detailModal.vue';
  import { usePermissionStore } from '/@/store/modules/permission';
  import { getAuthList, getAuthType, setAuthType, delAuth } from '/@/api/operate';
  import { BasicTable, useTable, TableAction, BasicColumn } from '/@/components/Table';
  import { useUserStore } from '/@/store/modules/user';
  import { useRouter } from 'vue-router'
  import dayjs from 'dayjs';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, BasicTable, TableAction, detailModal },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const dataSource = ref([]);
      const router = useRouter()
      const numRef = ref(null);
      const type = ref(0);
      const fileFlow = reactive({
        showList: true,
      });
      const [registerDetail, { openModal: openDetaileModal }] = useModal();
      const columns: BasicColumn[] = [
        {
          title: '案件名称',
          dataIndex: 'caseTitle',
          width: 300,
        },
      ];
      const permissionStore = usePermissionStore();
      const userStore = useUserStore();
      const userName = computed(() => userStore.getUserInfo?.userName);
      const roleId = computed(() => userStore.getUserInfo?.roleId);
      const { getCheckPerm } = permissionStore;
      const { createMessage, createConfirm } = useMessage();
      const [registerTable, { reload, getRawDataSource }] = useTable({
        // api: getAuthList,
        title: `已授权案件列表`,
        columns: columns,
        useSearchForm: false,
        immediate: false,
        showTableSetting: false,
        searchInfo: {
          num: numRef,
          authType: type,
        },
        // afterFetch: (T) => {
        //   let { authType } = getRawDataSource();
        //   setFieldsValue({
        //     authType,
        //   });
        //   fileFlow.showList = authType ? false : true;
        //   return T;
        // },
        tableSetting: {
          // 是否显示刷新按钮
          redo: false,
          // 是否显示尺寸调整按钮
          size: false,
          // 是否显示字段调整按钮
          setting: false,
          // 是否显示全屏按钮
          fullScreen: false,
        },
        showIndexColumn: false,
        canResize: true,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
      });
      const schemas: FormSchema[] = [
        {
          field: 'authType',
          component: 'RadioGroup',
          defaultValue: 0,
          componentProps: {
            options: [
              { label: '授权浏览', value: 0 },
              { label: '公开浏览', value: 1 },
            ],
            onChange: (value) => {
              type.value = value.target.value;
              fileFlow.showList = value.target.value ? false : true;
              reload();
            },
          },
          label: '授权方式',
          colProps: {
            span: 24,
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 80,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      async function onDataReceive(data) {
        modelRef.value = data;
        dataSource.value = data.list;
        numRef.value = data.num;
        // resetFields();
        // const { authType } = await getAuthType({ num: numRef.value });
        // fileFlow.showList = authType ? false : true;
        // type.value = authType;
        // setFieldsValue({
        //   type: data.sceneName,
        //   authType,
        // });
        // console.log(
        //   'roleId',
        //   roleId.value,
        //   userName.value != data.userName || roleId.value != 1 || roleId.value != 45 || roleId.value != 48,
        // );
        // let disabled = !(
        //   userName.value == data.userName ||
        //   roleId.value == 1 ||
        //   roleId.value == 45 ||
        //   roleId.value == 48
        // );
        // updateSchema({
        //   field: 'authType',
        //   componentProps: {
        //     options: [
        //       { label: '授权浏览', value: 0, disabled: disabled && authType == 1 },
        //       { label: '公开浏览', value: 1, disabled: disabled && authType == 0 },
        //     ],
        //   },
        // });
        // reload();
        // setTimeout(async () => {
        // }, 500);
      }
      const handleSubmit = async () => {
          router.push('/case/index')
      };
      function addPowes() {
        openDetaileModal(true, { num: modelRef.value.num, qxType: fileFlow.showList });
      }
      function handleEdit(record: Recordable) {
        openDetaileModal(true, record);
      }
      async function handleDelete(record: Recordable) {
        createConfirm({
          title: '删除',
          content: `确定要删除 ${record.ryNickName} 吗？`,
          onOk: async () => {
            await delAuth({ id: record.id });
            createMessage.success('删除成功');
            reload();
          },
        });
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
        getCheckPerm,
        handleEdit,
        handleDelete,
        registerTable,
        registerDetail,
        openDetaileModal,
        addPowes,
        userName,
        dataSource,
      };
    },
  });
</script>

<style lang="less" scoped>
  .powesrTable {
  }
</style>
