
<template>
  <div class="app-container" style="background-color: #fafafa; height:'calc(100vh - 350px)'">
    <form-com :form="form" :formface="fromFace" :tonyform="tonyform" />
    <foot-btns :btns="btns" :data="form"></foot-btns>
  </div>
</template>

<script>
import formCom from "../components/components-items/form-com";
import footBtns from "../components/components-items/footBtns";
import pageMintenancePrototype from "../components/js/page-maintenance-prototype";


import { createDb } from '../components/utils/CommonRequest'

const $metiiAPI = createDb();

export default {
  name: "mitee-form",
  data() {
    return {
      href: "",
      form: {},
      fromFace: [],
      tonyform: {},
      btns: []
    };
  },
  computed: {
    pages() {
      const pages = [];
      this.productArr.forEach((item, index) => {
        const page = Math.floor(index / 4);
        if (!pages[page]) {
          pages[page] = [];
        }
        pages[page].push(item);
      });
      return pages;
    },
    selectMap() {
      const arr = [];
      if (this.table.length > 0) {
        this.table.map(s => {
          arr.push({ name: s.name, id: s.id });
        });
        return arr;
      }
    }
  },
  watch: {
    $route(to, from) {
      this.getPageConfig();
    }
  },
  created() {
    this.getPageConfig();
  },
  methods: {
    // 获取路径的变量
    getField() {
      var that = this;
      that.href = [];
      var h = window.location.href;
      var st = "pname=";
      var st2 = "tablename=";
      var i = h.indexOf(st);
      var j = h.indexOf(st2);
      var L, s;
      if (i !== -1) {
        L = st.length;
        s = h.substr(i + L, j - L - i - 1);
        // console.log('s1', s)
        that.href.push(s);
      }
      if (j !== -1) {
        L = st2.length;
        s = h.substr(j + L);
        // console.log('s2', s)
        that.href.push(s);
      }
    },
    //  获取页面配置
    getPageConfig() {
      this.getField();
      this.switchSubmit = 0; // 0 是保存 1是修改
      pageMintenancePrototype.getPageConfig({}, this.href).then(response => {
        let { code, msg, result } = response;
        if (Object.is(code, 1)) {
          this.fromFace = result;
          this.fromFace.map(s => (s.enabled = true));
          let arr = this.fromFace.filter(s => {
            if (s.chinese === "操作") {
              return s;
            }
          });
          //处理下拉数据开始
          for (var key in this.fromFace) {
            var datatype = this.fromFace[key].datatype;
            if (datatype === "select") {
              if (this.fromFace[key].sourcedata.indexOf("http") === 0) {
                var list;
                $metiiAPI.get(this.fromFace[key].sourcedata)
                .then(res => {
                  list = res
                  let arr = [];
                  for (var k in list) {
                    arr.push(list[k]);
                  }
                  this.tonyform = list;
                })
              }
            }
          }
          //处理下拉数据结束
          this.btns = JSON.parse(arr[0].fsourcedata);
          console.log("this.btns", this.btns);
        } else {
          this.$message.error(msg);
        }
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    // 获取所有数据
    getList() {
      this.getField();
      pageMintenancePrototype.getList({}, this.href).then(response => {
        let { code, msg, result, count } = response;
        if (Object.is(code, 1)) {
          this.table = result;
          this.total = count;
        } else {
          this.$message.error(msg);
        }
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    }
  },
  components: {
    formCom,
    footBtns
  }
};
</script>
 