<template>
  <Page :left="leftFuns">
    <IxForm :control="searchForm" class="search-form" layout="inline" @submit.prevent="searchHandler">
      <IxFormItem :gutter="[0, 10]" label="企业名称">
        <IxInput control="tenantName" placeholder="请输入企业名称"/>
      </IxFormItem>
      <button style="display: none" type="submit"/>
    </IxForm>
    <IxTable
        v-model:selectedRowKeys="selectedRowKeys"
        :columns="columns"
        :dataSource="datasource"
        :pagination="false"
        :scroll="{height:'50rem'}"
        :spin="tableSpin"
        class="list-table">
      <template #action="{record}">
        <IxButtonGroup :gap="16" mode="link">
          <IxButton @click="lookup(record)">查看</IxButton>
          <IxButton @click="modify(record)">修改</IxButton>
          <IxPopconfirm :title="'是否删除：【' + record.productName + '】？'" placement="top" @ok="del(record)">
            <IxButton>删除</IxButton>
          </IxPopconfirm>
        </IxButtonGroup>
      </template>
    </IxTable>
    <IxRow justify="end">
      <IxPagination v-model:pageIndex="pagination.current" v-model:pageSize="pagination.size" :total="pagination.total"
          class="pagination"
          show-quick-jumper
          show-size-changer show-total @change="pagingChangeHandler"/>
    </IxRow>
    <IxModal v-model:visible="visibleDialog" title="删除提醒" type="confirm" @ok="confirmDelHandler">
      <p>是否删除已选数据？</p>
    </IxModal>
  </Page>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { VKey } from '@idux/cdk/utils'
import { TableColumn } from '@idux/components/table'
import Page from '@/components/page/Page.vue'
import { useFormGroup } from '@idux/cdk'
import nav from '@/common/router/nav.ts'
import { TreeSelectNode } from '@idux/components/tree-select/src/types'
import Toast from '@/components/toast'
import TenantApi from '@/pages/sys/tenant/tenant-api.ts'
import { useTenantDetailStore } from '@/pages/sys/tenant/tenant-detail/tenant-detail-store.ts'

const detailStore = useTenantDetailStore()

const leftFuns = computed<PageTypes.Fun[]>(() => {
  return [
    {
      title: '删除',
      mode: 'text',
      icon: 'delete',
      danger: true,
      disabled: selectedRowKeys.value.length === 0,
      onClick() {
        visibleDialog.value = true
      }
    },
    {
      title: '新建',
      mode: 'text',
      icon: 'plus',
      onClick() {
        nav.open('tenant-create')
      }
    },
    {
      title: '搜索',
      mode: 'text',
      icon: 'search',
      onClick() {
        pageList()
      }
    },
    {
      title: '重置',
      mode: 'text',
      icon: 'search',
      onClick() {
        searchForm.reset()
        searchHandler()
      }
    },
    {
      title: '刷新',
      mode: 'text',
      icon: 'reload',
      onClick() {
        searchHandler()
      }
    },
  ]
})

const searchForm = useFormGroup<TenantTypes.Tenant>({
  tenantName: [ undefined ],
})
const visibleDialog = ref(false)
const tableSpin = ref(false)
const selectedRowKeys = ref<VKey[]>([])
const pagination = reactive<G.Pagination>({
  current: 1,
  size: 100,
  total: 0,
})
const datasource = ref<TreeSelectNode[]>()

const columns: TableColumn<TenantTypes.Tenant>[] = [
  {
    type: 'selectable',
  },
  {
    title: '单位识别号',
    dataKey: 'sn',
  },
  {
    title: '产品名称',
    dataKey: 'tenantName',
  },
  {
    title: '状态',
    dataKey: 'status'
  },
  {
    title: '操作',
    key: 'action',
    customCell: 'action',
  },
]

function searchHandler() {
  pageList()
}

function confirmDelHandler() {
  const recode = selectedRowKeys.value.map(it => ({id: it as string}))
  del(...recode)
}

function pageList() {
  tableSpin.value = true
  TenantApi.paging({...searchForm.getValue(), ...pagination})
      .then(res => {
        pagination.current = res.data.current
        pagination.size = res.data.size
        pagination.total = res.data.total
        datasource.value = res.data.records.map(it => {
          return {
            key: it.id,
            ...it
          }
        })
      })
      .finally(() => {
        tableSpin.value = false
      })
}

function pagingChangeHandler() {
  pageList()
}

function del(...recode: { id: string }[]) {
  const toastId = Toast.loading('正在提交')
  TenantApi.del(recode.map(it => it.id))
      .then(_ => {
        Toast.success('删除成功')
        pageList()
      })
      .finally(() => {
        Toast.close(toastId)
      })
}

function modify(recode: TenantTypes.Tenant) {
  detailStore.$patch({
    id: recode.id,
    edit: true
  })
  nav.open('product-detail')
}

function lookup(recode: TenantTypes.Tenant) {
  detailStore.$patch({
    id: recode.id,
    edit: false
  })
  nav.open('product-detail')
}

onMounted(() => {
  pageList()
})

</script>

<style lang="stylus" scoped>
.search-form {
  padding 1rem
  margin 1rem 0
}

.list-table {
  margin 1rem

}


.pagination {
  margin-right: 1rem;
}
</style>
