<template>
  <PageWrapper contentBackground>
    <template #footer>
      <a-tabs v-model:activeKey="searchInfo.type" @change="tabChange">
        <a-tab-pane :key="1" :tab="t('routes.product.type.1')" />
        <a-tab-pane :key="9" :tab="t('routes.product.type.2')" />
        <a-tab-pane :key="10" :tab="t('routes.product.type.3')" />
      </a-tabs>
    </template>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTimeTable">
        <template #toolbar>
          <a-button
            type="primary"
            v-if="getCheckPerm('firmware-add')"
            @click="
              () => {
                openAddModal(true, searchInfo.type);
              }
            "
            >{{ t('routes.product.addVstive') }}</a-button
          >
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '编辑',
                //icon: 'ep:edit',
                ifShow: getCheckPerm('firmware-update'),
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                //icon: 'ic:outline-delete-outline',
                ifShow: getCheckPerm('firmware-delete'),
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
    </div>
    <AddModal @update="reload" @register="registerAddModal" />
    <EditModal @register="registerEditModal" @update="reload" />
  </PageWrapper>
</template>
<script lang="ts">
import { defineComponent, reactive, h } from 'vue';
import { BasicTable, useTable, FormProps, TableAction, BasicColumn } from '/@/components/Table';
import { PageWrapper } from '/@/components/Page';
import { Divider, Card, Empty, Descriptions, Steps, Tabs } from 'ant-design-vue';
import { CameraList, DelAndUpload, EditAndUpload } from '/@/api/product';
import { useModal } from '/@/components/Modal';
import { useI18n } from '/@/hooks/web/useI18n';
import AddModal from './AddModal.vue';
import EditModal from './EditModal.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { Switch } from 'ant-design-vue';
import { Time } from '/@/components/Time';
import { usePermissionStore } from '/@/store/modules/permission';
export default defineComponent({
  components: {
    BasicTable,
    AddModal,
    EditModal,
    TableAction,
    PageWrapper,
    [Divider.name]: Divider,
    [Card.name]: Card,
    Empty,
    [Descriptions.name]: Descriptions,
    [Descriptions.Item.name]: Descriptions.Item,
    [Steps.name]: Steps,
    [Steps.Step.name]: Steps.Step,
    [Tabs.name]: Tabs,
    [Tabs.TabPane.name]: Tabs.TabPane,
  },
  setup() {
    const { t } = useI18n();
    const permissionStore = usePermissionStore();
    const { getCheckPerm } = permissionStore;
    const searchInfo = reactive<Recordable>({
      type: 1,
    });
    const [registerAddModal, { openModal: openAddModal }] = useModal();
    const [registerEditModal, { openModal: openEditModal }] = useModal();
    const { createMessage } = useMessage();

    const refundTimeTableSchema: BasicColumn[] = [
      {
        title: '版本号',
        width: 150,
        dataIndex: 'version',
      },
      {
        title: '版本更新说明',
        width: 150,
        dataIndex: 'description',
      },
      {
        title: '最低版本号',
        width: 150,
        dataIndex: 'minVersion',
      },
      {
        title: '创建人',
        width: 150,
        dataIndex: 'createName',
      },
      {
        title: '创建时间',
        width: 150,
        dataIndex: 'createTime',
        customRender: ({ record }) => {
          return (
            record.createTime &&
            h(Time, {
              value: record.createTime,
              mode: 'datetime',
            })
          );
        },
      },
      {
        title: '状态',
        dataIndex: 'status',
        width: 80,
        ifShow: getCheckPerm('firmware-enable'),
        customRender: ({ record }) => {
          if (!Reflect.has(record, 'pendingStatus')) {
            record.pendingStatus = false;
          }
          return h(Switch, {
            checked: record.status === 'A',
            checkedChildren: '启用',
            unCheckedChildren: '禁用',
            loading: false,
            onChange: async (checked: boolean) => {
              record.pendingStatus = true;
              const newStatus = checked ? 'A' : 'I';
              await EditAndUpload({ ...record, status: newStatus });
              if (checked) {
                Reflect.set(record, 'status', newStatus);
              } else {
                Reflect.set(record, 'status', newStatus);
              }
              reload()
              createMessage.success(t('common.optSuccess'));
            },
          });
        },
      },
    ];
    const searchForm: Partial<FormProps> = {
      labelWidth: 100,
      autoSubmitOnEnter:true,
      schemas: [
        {
          field: 'version',
          label: '版本号',
          component: 'Input',
          componentProps: {
            maxLength: 100,
          },
          colProps: {
            xl: 6,
            xxl: 6,
          },
        },
      ],
    };
    const [registerTimeTable, { reload }] = useTable({
      api: CameraList,
      title: '固件列表',
      columns: refundTimeTableSchema,
      useSearchForm: true,
      formConfig: searchForm,
      showTableSetting: true,
      rowKey: 'id',
      fetchSetting: {
        pageField: 'pageNum',
        sizeField: 'pageSize',
        listField: 'list',
        totalField: 'total',
      },
      searchInfo: searchInfo,
      // beforeFetch:(T)=>{
      //   T.type = searchInfo.type
      //   console.log('beforeFetch',T,searchInfo)
      //   return T
      // },
      actionColumn: {
        width: 180,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
      },
      canResize: true,
    });
    function tabChange(val: string) {
      console.log('tabChange', val);
      reload();
    }
    async function handleDelete(record: Recordable) {
      console.log('点击了删除', record);
      await DelAndUpload({ id: record.id });
      createMessage.success(t('common.optSuccess'));
      reload();
    }
    function handleOpen(record: Recordable) {
      console.log('点击了启用', record);
    }
    function handleEdit(record: Recordable) {
      console.log('record', record);
      openEditModal(true, {
        ...record,
        type:searchInfo.type
      });
    }
    return {
      registerTimeTable,
      handleDelete,
      handleOpen,
      tabChange,
      reload,
      registerAddModal,
      registerEditModal,
      openAddModal,
      handleEdit,
      getCheckPerm,
      t,
      searchInfo,
    };
  },
});
</script>
<style lang="less" scoped>
.desc-wrap-BasicTable {
  background-color: #f0f2f5;
  .vben-basic-table-form-container {
    padding: 0;
  }
}
</style>
