const template = html`
  <span>
    <h4>
      Repairs Queue:
      <span class="badge badge-info ng-binding">{{data.length}}</span>
      <jsontable
        :header="header"
        :data="data"
        :popoverHeader="popoverHeader"
      ></jsontable>
    </h4>
  </span>
`;
import jsontable from "../../generic/jsontable.js";

export default {
  data: function() {
    return {
      data: [],
      header: {
        ID: "id",
        Title: "title",
        client: "cname",
        contract: "contractType",
        "booked in": "created_at"
      },
      popoverHeader: {
        "Booked by": "t",
        Status: "test",
        prebook: "prebook",
        "booked in": "created_at",
        problem: "problem"
      },
      tags: []
    };
  },
  components: {
    jsontable
  },
  mounted() {
    this.load();
  },
  methods: {
    async load() {
      const raw = await fetch(
        "/api/v1/tasks/repairs?results=unassigned&client=true&results=own&user_id=176"
      );
      const json = await raw.json();
      const cl = [];
      this.data = json.data.map(el => {
        let cname = "";
        if (el.prebook == 1) {
          el.tag = "prebook-note";
        }
        if (el.client) {
          el.cname = el.client.name;
        }
        if (el.contract) {
          el.contractType = el.contract.name;
        }
        el.created_at = moment(el.created_at).fromNow();
        if (el.prebook) {
          cl.push("prebook-note");
          el.prebook = el.prebook_date;
        } else {
          el.prebook = "";
        }
        return el;
      });
      this.tags.push(cl.join(" "));
    }
  },
  template
};
