<template>
    <BasicTable @register="registerTable" @row-click="handlerowClick" @expand="handleExpanded">
      <template #toolbar>
        <a-button type="primary" @click="exportExcel"> 导出</a-button>
      </template>
      <template #expandedRowRender="{ record }">
        <div>
          <a-descriptions title="商品信息" :column="2">
            <!-- <a-descriptions-item label="商品名称">{{record.shipName}} </a-descriptions-item>
            <a-descriptions-item label="角色码"> 1234567 </a-descriptions-item> -->
            <a-descriptions-item label="套餐"> {{record.goodsName}}</a-descriptions-item>
            <a-descriptions-item label="顾客姓名"> {{record.shipName}} </a-descriptions-item>
            <a-descriptions-item label="数量"> {{record.goodsCount}} </a-descriptions-item>
            <a-descriptions-item label="电话"> {{record.shipMobile}} </a-descriptions-item>
            <a-descriptions-item label="商品单价"> {{record.goodsPrice}} </a-descriptions-item>
            <a-descriptions-item label="收货地址"> {{record.shipAreaPath}}{{record.shipAddress}} </a-descriptions-item>
            <a-descriptions-item label="" :span="1">
              <TableImg style="margin: 0"
                :size="120"
                :simpleShow="true"
                :imgList="[record.goodsPic || 'http://zfb-4dkankan.oss-cn-shenzhen.aliyuncs.com/sceneLogo/1653644220202_3ee8b3c006e74012a82f2b286b2f4914.png']"
              />
            </a-descriptions-item>
          </a-descriptions>
        </div>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '删除',
                icon: 'ic:outline-delete-outline',
                onClick: handleDelete.bind(null, record),
              },
            ]"
            :dropDownActions="[
              {
                label: '启用',
                popConfirm: {
                  title: '是否启用？',
                  confirm: handleOpen.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, h ,reactive } from 'vue';
  import { BasicTable, useTable, TableAction, BasicColumn, TableImg, FormProps } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { Time } from '/@/components/Time';
  import { Descriptions } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Switch } from 'ant-design-vue';
  import {  downloadByData, } from '/@/utils/file/download';
  import { CameraList,CameraItem, CameraExport } from '/@/api/order'

  export default defineComponent({
    components: { 
      BasicTable, 
      TableAction, 
      PageWrapper,
      TableImg,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
    },
    setup() {
      const { t } = useI18n();
      const { createConfirm } = useMessage();
      const expandedItem = reactive({

      })
      const columns: BasicColumn[] = [
        {
          title: '下单时间',
          dataIndex: 'orderTime',
          width: 120,
          customRender: ({ record }) => {
            return (
              record.orderTime &&
              h(Time, {
                value: record.orderTime,
                mode: 'datetime',
              })
            );
          },
        },
        {
          title: '订单号',
          dataIndex: 'orderSn',
          ellipsis: false,
          width: 180,
        },
        {
          title: '用户账号',
          dataIndex: 'userName',
          ellipsis: false,
          width: 120,
        },
        {
          title: '订单金额(元)',
          dataIndex: 'goodsAmount',
          ellipsis: false,
          width: 120,
        },
        {
          title: '支付方式',
          dataIndex: 'paymentTypeName',
          width: 80,
        },
        {
          title: '交易号',
          dataIndex: 'tradeNum',
          width: 220,
        },
        {
          title: '订单状态',
          dataIndex: 'paymentStatus',
          // slots: { customRender: 'orderStatus' },
          width: 80,
        },
      ];
      const searchForm: Partial<FormProps> = {
        labelWidth: 80,
        schemas: [
          {
            field: 'sceneName',
            label: '下单时间',
            component: 'RangePicker',
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
          {
            field: 'type',
            label: '订单号',
            component: 'Input',
            colProps: {
              xl: 6,
              xxl: 6,
            },
            componentProps: {
              // api: brandTypeListApi,
              resultField: 'list',
              labelField: 'name',
              valueField: 'brandType',
              params: {
                page: 1,
                limit: 1000,
              },
            },
          },
          {
            field: 'livestreamStatus',
            component: 'Input',
            label: '用户账号',
            colProps: {
              xl: 6,
              xxl: 6,
            },
            componentProps: {
              options: [
                {
                  label: t('common.all'),
                  value: '',
                  key: '0',
                },
                {
                  label: t('common.yes'),
                  value: 1,
                  key: '1',
                },
                {
                  label: t('common.no'),
                  value: 0,
                  key: '2',
                },
              ],
            },
          },
        ],
      };
      const [registerTable] = useTable({
        api: CameraList,
        title: '相机列表',
        // titleHelpMessage: ['已启用expandRowByClick', '已启用stopButtonPropagation'],
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showIndexColumn:false,
        showTableSetting: true,
        canResize: false,
        expandRowByClick: true,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
      });
      function handleDelete(record: Recordable) {
        console.log('点击了删除', record);
      }
      function handleOpen(record: Recordable) {
        console.log('点击了启用', record);
      }
      function handleExpanded(_,record: Recordable) {
        handlerowClick(record)
      }
      function exportExcel() {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', '确定导出的订单记录?'),
          onOk: async () => {
            CameraExport({}).then(res => {
              console.log('handlerowClick',res)
              downloadByData(res.data,'相机订单.xls')
            })
          },
        });
      }
      function handlerowClick(record: Recordable){
        CameraItem({id:record.id}).then(res => {
          if(res.length){
            let item = res[0]
            Reflect.set(record, 'goodsCount', item.goodsCount);
            Reflect.set(record, 'goodsName', item.goodsName);
            Reflect.set(record, 'goodsPic', item.goodsPic);
            Reflect.set(record, 'goodsPrice', item.goodsPrice);
          }
        })
      }
      return {
        registerTable,
        handleDelete,
        handleOpen,
        exportExcel,
        handlerowClick,
        expandedItem,
        handleExpanded,
      };
    },
  });
</script>