<template>
  <PageSpace>
    <template v-slot:default="{adjustHeight}">
      <BizTable
        ref="bt"
        :formatCol="formatCol"
        metaName="app_info"
        packageName="common"
        :tblheight="adjustHeight-120"
        isMulti
        :refvalidatemethod="validmethod"
        @onexec="onexec"
        :savebefore="beforesave"
        :editbefore="beforedit"
        :addbefore="beforeadd"
      ></BizTable>
    </template>
  </PageSpace>
</template>
  <script>
import BizTable from "../components/tables/biztables";
import PageSpace from "@/view/components/pagespace/page_space.vue";
export default {
  name: "appinfo_page",
  data() {
    return {};
  },
  components: {
    PageSpace,
    BizTable
  },
  methods: {
    beforeadd(row, cbk) {
      return cbk({ value: true, message: null });
    },
    beforedit(row, cbk) {
      return cbk({ value: true, message: null });
    },
    beforesave(key, row, cbk) {
      return cbk(row);
    },
    onexec(k, row) {
      if (k == "func") {
        this.$router.push({
          name: "functree_info",
          query: { appkey: row.appkey}
        });
      }
    },
    validmethod(rule, value, callback) {
      if (rule.field == "functionJSON") {
        try {
          let j = JSON.parse(value);
          console.log(j);
        } catch (e) {
          return callback(new Error(e));
        }
      }
      if (rule.field == "hosts") {
        let attrhosts = value.split(",");
        if (attrhosts.length == 0) {
          callback(new Error("多个域名需要以逗号分隔"));
        }
      }
      if (rule.field == "paths") {
        let attrpaths = value.split(",");
        if (attrpaths.length == 0) {
          callback(new Error("多个路径需要以逗号分隔"));
        } else {
          attrpaths.forEach(p => {
            if (!p.startsWith("/")) {
              return callback(new Error("路径必须以/开始"));
            }
          });
        }
      }
      return callback();
    },

    // onnew() {
    //   this.$refs.bt.setFormCtlVisable("userName", true);
    // },
    // onedit() {
    //   this.$refs.bt.setFormCtlVisable("userName", false);
    // },
    formatCol(row, key, index) {
      // if (key == "roleName") {
      //   let rolestr = row.Roles.map(item => item.name).join(",");
      //   row.roles = row.Roles.map(item => item.id);
      //   return `<span>${rolestr}</span>`;
      // }
      // if (key == "isAdmin" || key == "isSuper") {
      //   return `<span>${row[key] ? "是" : "否"}</span>`;
      // }
      if (key == "created_at") {
        return `<span>${new Date(row[key]).toLocaleString()}</span>`;
      }
      return row[key];
    }
  }
};
</script>
  <style>
</style>
