import apollo from "../../lib/apollo.js";

async function getTransaction(token = "") {
  var res = await apollo.query(
    `
    query {
      transactions{
        id
        order_id
        cust_name
        cust_telp
        cust_address
        product_img{
          url
        }
        product_name
        product_color
        product_quantity
        product_courier
        product_price
      }
    }`,
    token
  );
  return res;
}

async function getTransactionUnpaid(token = "") {
  var res = await apollo.query(
    `
    query {
      transactions(where: { status: "1" }){
        id
        order_id
        cust_name
        cust_telp
        cust_address
        product_img{
          url
        }
        product_name
        product_color
        product_quantity
        product_courier
        product_price
      }
    }`,
    token
  );
  return res;
}

async function getTransactionPrepared(token = "") {
  var res = await apollo.query(
    `
    query {
      transactions(where: { status: "2" }){
        id
        order_id
        cust_name
        cust_telp
        cust_address
        product_img{
          url
        }
        product_name
        product_color
        product_quantity
        product_courier
        product_price
      }
    }`,
    token
  );
  return res;
}

async function getTransactionSending(token = "") {
  var res = await apollo.query(
    `
    query {
      transactions(where: { status: "3" }){
        id
        order_id
        cust_name
        cust_telp
        cust_address
        product_img{
          url
        }
        product_name
        product_color
        product_quantity
        product_courier
        product_price
      }
    }`,
    token
  );
  return res;
}

async function getTransactionFinished(token = "") {
  var res = await apollo.query(
    `
    query {
      transactions(where: { status: "4" }){
        id
        order_id
        cust_name
        cust_telp
        cust_address
        product_img{
          url
        }
        product_name
        product_color
        product_quantity
        product_courier
        product_price
      }
    }`,
    token
  );
  return res;
}

async function newTransaction(content, token="") {
    var res = await apollo.mutation(
        `
        mutation($input : TransactionInput!){
            createTransaction( input:{data:$input} )
            {
                transaction{
                    id
                }
            }
        }
        `,
        token,
		{
		input: content,
		}
    );
	return res;
}

module.exports = {
	newTransaction: newTransaction,
    getTransaction:getTransaction,
    getTransactionUnpaid:getTransactionUnpaid,
    getTransactionPrepared:getTransactionPrepared,
    getTransactionSending:getTransactionSending,
    getTransactionFinished:getTransactionFinished,
};