<template>
  <FormPage
    ref="formPage"
    :action-column="actionColumn"
    :left-tools="leftTools"
    :default-search-form="{createTimes:[undefined,undefined]}"
    :paging="paging">
    <template #searchFormItem="{ searchForm }">
      <ElFormItem label="产品分类">
        <GoodsCategoryDropTable v-model="searchForm.goodsCategoryId"/>
      </ElFormItem>
      <ElFormItem label="产品编码">
        <ElInput v-model="searchForm.sn" placeholder="产品编码"/>
      </ElFormItem>
      <ElFormItem label="产品名称">
        <ElInput v-model="searchForm.goodsName" placeholder="产品名称"/>
      </ElFormItem>
      <ElFormItem label="创建时间">
        <ADtPicker v-model="searchForm.createTimes" :change-handler="research"/>
      </ElFormItem>
    </template>
    <template #simpleSearchFormItem="{ searchForm }">
      <ElFormItem>
        <ElInput v-model="searchForm.sn" placeholder="产品编码"/>
      </ElFormItem>
      <ElFormItem>
        <ElInput v-model="searchForm.goodsName" placeholder="产品名称"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="产品编码" prop="sn"/>
      <ElTableColumn label="图片" width="100">
        <template #default="{ row }">
          <el-image :preview-src-list="[AppApi.fileUrl(row.picture)]" :src="AppApi.fileUrl(row.picture)" preview-teleported show-progress style="width: 32px; height: 32px"/>
        </template>
      </ElTableColumn>
      <ElTableColumn label="产品名称" prop="goodsName"/>
      <ElTableColumn label="规格" prop="specParams"/>

      <ElTableColumn label="是否可用" prop="canuse">
        <template #default="{row}">
          <ElSwitch v-model="row.canuse" @change="enableGoodsHandler($event as boolean,row.id)"/>
        </template>
      </ElTableColumn>
      <ElTableColumn label="备注" prop="memo"/>
      <ElTableColumn label="创建时间" prop="createTime" width="160"/>
    </template>
    <GoodsForm ref="goodsForm" :research="research"/>
  </FormPage>
</template>

<script lang="ts" setup>
import GoodsApi from '@/pages/gds/goods/goods-api.ts'
import GoodsForm from '@/pages/gds/goods/GoodsForm.vue'
import AppApi from '@/common/app/app-api.ts'
import FormPage from '@/components/page/FormPage.vue'
import type {
  ActionColumnType,
  ToolType,
} from '@/components/page/a-page-type.ts'
import type { ComponentExposed } from 'vue-component-type-helpers'
import ADtPicker from '@/components/a-dt-picker/ADtPicker.vue'
import GoodsCategoryDropTable from '@/pages/gds/goods-category/GoodsCategoryDropTable.vue'

const goodsFormIns = useTemplateRef<InstanceType<typeof GoodsForm>>('goodsForm')
const formPageIns = useTemplateRef<ComponentExposed<typeof FormPage>>('formPage')

const actionColumn = reactive<ActionColumnType<GoodsTypes.SearchGoodsResult>>({
  tableActions: [
    {
      tooltip: '编辑',
      icon: 'Edit',
      action({row}) {
        goodsFormIns.value?.open(row)
      },
    },
    {
      icon: 'Delete',
      loading: false,
      type: 'danger',
      tooltip: '删除',
      confirm: {
        title: '是否删除当前数据',
      },
      action({row}) {
        return GoodsApi.del([ row.id! ])
          .then(() => {
            ElMessage.success('删除成功')
            return true
          })
      },
    },
  ],
} as ActionColumnType<GoodsTypes.SearchGoodsResult>)
const leftTools: ToolType[] = [
  {
    icon: 'Plus',
    label: '新建',
    action() {
      goodsFormIns.value?.open()
    },
  },
]

function research() {
  formPageIns.value?.doSearch()
}

function paging(param: GoodsTypes.SearchGoodsParam) {
  return GoodsApi.paging({
    ...param,
    createTimeStart: param.createTimes[0],
    createTimeEnd: param.createTimes[1],
  })
}

function enableGoodsHandler(enable: boolean, id: string) {
  GoodsApi.enable({enable, id})
    .then(() => {
      ElMessage.success(enable ? '启用成功' : '禁用成功')
      research()
    })
}
</script>
