<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="权益详情"
    :width="800"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
  >
    <div class="pt-3px pr-3px">
      <div class="table_list">
        <BasicTable @register="registerSubtable" >
          <template #action="{ record }">
            <TableAction
              :actions="[
                 {
                   label: '续费',
                   onClick: handleDelete.bind(null, record, 'edit'),
                 },
              ]"
            />
          </template>
        </BasicTable>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, computed,h } from 'vue';
  import { BasicTable, useTable, BasicColumn,TableAction } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm } from '/@/components/Form/index';
  import { Time } from '/@/components/Time';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { GetCameraDetailApi,UnbindCameraApi } from '/@/api/account';
  import { otherInfoStore } from '/@/store/modules/other'
  import { DetailsApi,IncrementDelayApi } from '/@/api/account';
  import { usePermissionStore } from '/@/store/modules/permission';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, BasicTable,TableAction },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const otherInfo = otherInfoStore();
      const overviewInfo = computed(() => otherInfo.getOverviewInfo);
      const { createMessage, createConfirm } = useMessage();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      console.log('overviewInfo',overviewInfo)
      // const overviewInfo = getOverviewInfo() || {}
      const fileFlow = reactive({
        file:null,
        id:'',
        type:'down',//down-下载，equity-权益
      })
            const columns: BasicColumn[] = [
        {
          title: '会员权益ID',
          dataIndex: 'id',
          width: 80,
        },
        {
          title: '授权相机S/N吗',
          width: 160,
          dataIndex: 'snCode',
        },
        {
          title: '购买日期',
          dataIndex: 'incrementStartTime',
          width: 180,
          customRender: ({ record }) => {
            return record.incrementStartTime
              ? h(Time, {
                  value: record.incrementStartTime,
                  mode: 'date',
                })
              : '-';
          },
        },
        {
          title: '到期日期',
          dataIndex: 'incrementEndTime',
          width: 180,
          customRender: ({ record }) => {
            return record.incrementEndTime
              ? h(Time, {
                  value: record.incrementEndTime,
                  mode: 'date',
                })
              : '-';
          },
        },
        {
          title: t('common.operating'),
          ifShow:getCheckPerm('account-equityRenew'),
          dataIndex: '',
          slots: { customRender: 'action' },
          width: 80,
        },
      ];
      const [
        registerSubtable,
        {
          reload,
        },
      ] = useTable({
        // title: `查看相机详情`,
        searchInfo:{
          id:overviewInfo.value?.id
        },
        api: DetailsApi,        
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        columns: columns,
        showIndexColumn: false,
        bordered: true,
        canResize: true,
        // pagination: false,
      });

      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        reload()
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        console.log('onDataReceive',data)
        fileFlow.id = data.id
      }
      async function handleDelete (record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '续费提示'),
          content: () => h('span', `您确定给权益ID${record.id}续期${record.validTimeType == 4?'1月':'1年'}！`),
          onOk: async () => {
            await await IncrementDelayApi({id:record.id,year:1})
            createMessage.success(t('common.optSuccess'));
            reload()
          },
        });
      }
      const handleSubmit = async () => {
          closeModal();
          emit('update');
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        registerSubtable,
        reload,
        handleDelete,
        t,
      };
    },
  });
</script>
