import { defineComponent, onMounted, ref } from 'vue'

import { CImgText } from '../../../components/CImgText/index'
import { CCard } from '@src/components/CCard'
import { CMyPage } from '@src/components/CMyPage'
import { CTitleBanner } from '@src/components/CTitleBanner'
import { CGridTableRec } from '@src/components/CGridTableRec'

import { colTableGestoreOrdini } from '@src/store/Modules/fieldsTable'
import MixinMetaTags from '@src/mixins/mixin-metatags'

export default defineComponent({
  name: 'Gestoreordini',
  components: { CImgText, CCard, CMyPage, CTitleBanner, CGridTableRec },
  setup() {

    const { setmeta } = MixinMetaTags()
    const filtroOrdini = ref(<any[]>[])
    const idGasordine = ref('')

    function mounted() {
      let queryord = []

      filtroOrdini.value = []

      if (idGasordine.value) {
        const gasordine = {
          $match: {
            idGasordine: idGasordine.value
          },
        }
        queryord.push(gasordine)
      }


      const query = [
        {
          $lookup: {
            from: 'products',
            localField: 'idProduct',
            foreignField: '_id',
            as: 'product',
          },
        },
        {
          $unwind: {
            path: '$product',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $lookup: {
            from: 'productinfos',
            localField: 'product.idProductInfo',
            foreignField: '_id',
            as: 'productInfo',
          },
        },
        {
          $unwind: {
            path: '$productInfo',
          },
        },
        {
          $lookup: {
            from: 'gasordines',
            localField: 'idGasordine',
            foreignField: '_id',
            as: 'gasordine',
          },
        },
        {
          $unwind: {
            path: '$gasordine',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $match: {
            $or: [
              {
                'gasordine.active': true,
              },
              {
                gasordine: {
                  $exists: false,
                },
              },
            ],
          },
        },
        {
          $match: {
            $or: [
              {
                quantity: {
                  $gt: 0,
                },
              },
              {
                quantitypreordered: {
                  $gt: 0,
                },
              },
            ],
          },
        },
        {
          $group: {
            _id: '$product._id',
            name: {
              $first: '$productInfo.name',
            },
            weight: {
              $first: '$productInfo.weight',
            },
            price_acquistato: {
              $first: '$product.price_acquistato',
            },
            totalQuantity: {
              $sum: {
                $add: [
                  '$quantity',
                  '$quantitypreordered',
                ],
              },
            },
            totalPrice_acquistato: {
              $sum: {
                $multiply: [
                  '$product.price_acquistato',
                  {
                    $add: [
                      '$quantity',
                      '$quantitypreordered',
                    ],
                  },
                ],
              },
            },
            count: {
              $sum: 1,
            },
          },
        },
        {
          $sort: {
            name: 1,
          },
        }
      ]

      queryord.push(query)

      filtroOrdini.value = queryord

    }

    onMounted(mounted)

    return {
      colTableGestoreOrdini,
      setmeta,
      filtroOrdini,
    }
  }
})
