<template>
  <div v-if="form">
    <main-top></main-top>
    <editcon
      :key="type"
      ref="child"
      :type="type"
      :rule="rule"
      :form="form"
      @cancel="backList"
      @save="save"
      @submit="submit({ id }, val)"
      @determine="determine"
    >
      <template slot-scope="{ form }" slot="content">
        <component
          :data="form"
          :is="cp"
          :ageList="ageListAll"
          :museumList="museumList"
          :textureList="textureListAll"
          :filelist="filelist"
        />
      </template>
    </editcon>

    <editdialog
      :type="val"
      :show="showAudit"
      @close="showAudit = false"
      :id="id"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";

import vmodel from "./componets/model";
import vimg from "./componets/img";
import vvideo from "./componets/video";
import vaudio from "./componets/audio";

import { formKey, rules } from "./data";
import {
  save,
  getDetail,
  getDictList,
  getMuseumAllList,
  getDictListAll,
} from "@/configue/api";

import { randomWord } from "@/util/index.js";

let ran = randomWord(true, 8, 8);

export default {
  components: {
    vmodel,
    vimg,
    vvideo,
    vaudio,
  },
  data() {
    let { type, id } = this.$route.params;
    return {
      val: "goods",
      type,
      id: String(id),
      form: "",
      filelist: [],
      ageList: [],
      textureList: [],
      museumList: [],
      showAudit: false,
      ageListAll:[],
      textureListAll:[]
    };
  },
  computed: {
    ...mapGetters({
      activeMenu: "activeMenu",
    }),
    cp() {
      return "v" + this.type;
    },
    rule() {
      return rules[this.type];
    },
  },
  mounted() {
    this.getDetail();
    this.getlist("age");
    this.getlist("texture");
    this.getMuseumAllList();

    getDictListAll("age", (data) => {
      console.log('------------',data.data);
      this.ageListAll = data.data;
    });
    getDictListAll("texture", (data) => {
      this.textureListAll = data.data;
    });
  },
  methods: {
    save(submit = "", cb = null) {
      this.validate(() => {
        let fileIds = this.filelist.map((item) => item.id).join(",");
        let params = {
          code: ran,
          description: "",
          fileIds: fileIds,
          fileName: "",
          filePath: "",
          id: this.id == "none" ? null : this.id,
          name: "",
          thumb: "",
          type: this.type,
          ...this.form,
        };
        save(this.val, params, () => {
          if (submit) {
            cb && cb();
          } else {
            this.$alert("编辑成功", "提示", {
              confirmButtonText: "确定",
              callback: () => {
                this.backList();
              },
            });
          }
        });
      });
    },

    getlist(type) {
      getDictList(type, (data) => {
        if (type == "age") {
          this.ageList = data.data.records;
        } else {
          this.textureList = data.data.records;
        }
      });
    },

    getMuseumAllList() {
      getMuseumAllList((data) => {
        this.museumList = data.data;
      });
    },

    async getDetail() {
      if (this.id == "none") {
        this.form = this.cloneObject(formKey[this.$route.params.type]);
      } else {
        await getDetail(this.val, { id: this.id }, (res) => {
          this.form = { ...this.form, ...res.data.entity };
          this.form["display"] = Boolean(this.form["display"]);
          this.filelist = res.data.file;
        });
      }
    },
  },
};
</script>
