import Vue from "vue";
import { base } from "@/configue/base";
import { axios, serverName } from "@/configue/http";
import { isImage,isZip, isTypeBySend, cloneObject, isNull } from "@/util/index.js";

import { del, applyDetail, apply } from "@/configue/api";

Vue.prototype.$base = base;
Vue.prototype.$bus = new Vue();
Vue.prototype.$http = axios;
Vue.prototype.isImage = isImage;
Vue.prototype.isZip = isZip;
Vue.prototype.isTypeBySend = isTypeBySend;

Vue.prototype.INPUTLENGTH = 20;
Vue.prototype.PAGESIZES = [25, 50, 75, 100];
Vue.prototype.UNITS = [{
  name: '第一层',
  id: '1'
}, {
  name: '第二层',
  id: '2'
}]

Vue.prototype.loading = {
  close() { },
};

Vue.prototype.loadOption = {
  lock: true,
  text: "上传中",
  spinner: "el-icon-loading",
  background: "rgba(0, 0, 0, 0.7)",
};

Vue.prototype.statusStr = {
  all: "全部",
  0: "草稿中",
  1: "待审核",
  2: "审核不通过",
  3: "审核通过",
};

Vue.prototype.statusArr = [
  {
    id: "all",
    name: "全部",
  },
  {
    id: 0,
    name: "草稿中",
  },
  {
    id: 1,
    name: "待审核",
  },
  {
    id: 2,
    name: "审核不通过",
  },
  {
    id: 3,
    name: "审核通过",
  },
];

Vue.prototype.sysRole = [
  {
    name: "系统管理员",
    id: "sys_admin",
  },
  {
    name: "高级管理员",
    id: "sys_high",
  },
  {
    name: "普通用户",
    id: "sys_normal",
  },
  {
    name: "-",
    id: "sys_visitor",
  },
];

Vue.prototype.collectType = {
  1: "专题图库",
  2: "视频文件",
  3: "模型文件",
};

Vue.prototype.juese = {
  sys_admin: "系统管理员",
  sys_content: "内容管理员",
  sys_audit: "审核管理员"
};

Vue.prototype.sex = {
  0: "男",
  1: "女",
};



Vue.prototype.$serverName = serverName; // 挂载到Vue实例上面

let g_political = [{
  val: '群众'
}, {
  val: '团员'
}, {
  val: '党员'
}, {
  val: '预备党员'
}]

let g_expert = [{
  val: '一级',
  id: 1
}, {
  val: '二级',
  id: 2
}, {
  val: '三级',
  id: 3
}, {
  val: '院士',
  id: 4
}, {
  val: '英模代表专家',
  id: 6
}, {
  val: '其他',
  id: 5
}]

import MainTop from "@/components/main-top";
import search from "@/components/search";
import accordion from "@/components/accordion";
import status from "@/components/status";
import vtable from "@/components/table";
import dialog from "@/components/dialog";
import editcon from "@/components/editcomponent/editcon";
import editdialog from "@/components/editcomponent/editdialog";

Vue.mixin({
  components: {
    MainTop,
    accordion,
    search,
    vtable,
    status,
    editcon,
    editdialog,
    vdialog: dialog,
  },
  data() {
    return {
      g_political,
      g_expert
    }
  },
  methods: {
    cloneObject,
    isNull,
    validate(cb) {
      let child = this.$refs.child;
      child.$refs[this.type].validate(async (valid) => {
        if (valid) {
          if (status == 2) {
            this.$confirm("请再次确认是否进行此操作？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(async () => {
                cb();
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消",
                });
              });
          } else {
            cb();
          }
        }
      });
    },
    confirm(msg, res) {
      this.$confirm(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          res();
        })
        .catch(() => { });
    },
    backList() {
      let name = this.$route.meta.index;
      let { type } = this.$route.params;
      this.$router.replace({
        name: name,
        params: {
          type,
        },
      });
    },
    goto(item) {
      let { type } = this.$route.params;
      this.$router.push({
        name: "edit-" + this.$route.name,
        params: {
          id: item.id,
          type: type,
        },
      });
    },
    del(data) {
      let { item, type } = data;
      this.confirm("删除后，信息将无法恢复，是否继续？", () => {
        console.log(data);
        del(
          type,
          {
            id: item.id,
          },
          () => {
            this.$alert("删除成功", "提示", {
              confirmButtonText: "确定",
              callback: () => {
                let tmp = this.getList ? "getList" : "backList";
                this[tmp]();
              },
            });
          }
        );
      });
    },
    audit(item) {
      let { type } = this.$route.params;
      this.$router.push({
        name: "edit-" + this.$route.name,
        params: { id: item.id, type: type },
        query: { isAudit: true },
      });
    },
    applyDetail(item, type) {
      applyDetail(type, { id: item.id }, (res) => {
        this.showApplyDetail = true;
        this.alDetail = res;
      });
    },
    submit(data, hadTwoParam = null) {
      let item = ''
      let type = ''
      if (hadTwoParam) {
        item = data
        type = hadTwoParam
        this.confirm("提交后，信息将保存并且进入审核，是否继续？", () => {
          this.save('submit', () => {
            apply(type, { id: item.id }, () => {
              this.$alert("提交成功", "提示", {
                confirmButtonText: "确定",
                callback: () => {
                  let tmp = this.getList ? "getList" : "backList";
                  this[tmp]();
                },
              });
            });
          })
        });
      }
      else {
        item = data.item
        type = data.type
        this.confirm("提交后，信息将进入审核，是否继续？", () => {
          apply(type, { id: item.id }, () => {
            this.$alert("提交成功", "提示", {
              confirmButtonText: "确定",
              callback: () => {
                let tmp = this.getList ? "getList" : "backList";
                this[tmp]();
              },
            });
          });
        });
      }

    },
    determine() {
      let { isAudit } = this.$route.query;
      if (this.status === 1) {
        if (isAudit) {
          this.showAudit = true;
          return;
        }
      }
      this.backList();
    },
    getAuth(menu) {
      if (!menu) return;
      let auth = {};
      menu.children && menu.children.forEach((item) => {
        auth[item.name] = item.authority;
      });
      this.$store.commit("SetActiveSub", auth);

    },
  },
  watch: {
    page: {
      deep: true,
      handler: function () {
        this.getList && this.getList();
      },
    }
  },
});
