import HeaderMain from "../../components/HeaderMain";
import ShareSetting from "../../components/ShareSetting";
import api from '../../api/test'
import editorApi from '../../api/editor'
import timeFormat from '../../util/time'
export default {
  components: {
    HeaderMain, ShareSetting
  },
  data() {
    return {
      isCopy: false,
      isShareSetting: false,
      cities: [{
        value: 'Beijing',
        label: '北京'
      }, {
        value: 'Shanghai',
        label: '上海'
      }, {
        value: 'Nanjing',
        label: '南京'
      }, {
        value: 'Chengdu',
        label: '成都'
      }, {
        value: 'Shenzhen',
        label: '深圳'
      }, {
        value: 'Guangzhou',
        label: '广州'
      }],
      value: '',
      pageModel: "",
      loading: false,
      houseList: [],
      ownHouseName: "所属项目",
      orderList: [{ orderName: "按创建时间排序", orderType: "created" }, { orderName: "按更新时间排序", orderType: "updated" }],
      currOrderName: "更新时间",
      currOrderType: "",
      shareModel: ""
    }
  },
  computed: {
    noMore() {
      return this.pageModel.count == this.pageModel.total && this.pageModel.total > 1
    },
    disabled() {
      return this.loading || this.pageModel.count == this.pageModel.total
    }
  },
  watch: {
    isShareSetting: function (val) {
      var mo = function (e) { e.preventDefault(); };
      document.body.style.overflow = val ? 'hidden' : '';
      if (val) {
        document.addEventListener("touchmove", mo, false);
      } else {
        document.removeEventListener("touchmove", mo, false);
      }
    }
  },
  created() {
    this.houseList = [];
    editorApi.houseList().then((res) => {
      if (res.success) {
        this.houseList = res.list;
      }
    });
    this.getTestList(true);
  },
  methods: {
    dataformat(val) {
      return timeFormat.dateStr(val);
    },
    load() {
      this.getTestList(false);
    },
    getTestList(isFirst) {
      this.loading = !isFirst
      let data = {
        "orderType": this.currOrderType,
        "ownHouseName": this.ownHouseName == "所属项目" ? '' : this.ownHouseName,
        "pageNo": isFirst ? 0 : this.pageModel.count + 1,
      }
      api.testList(data).then((res) => {
        if (res.success) {
          this.pageModel = res.pageModel;
          console.log("success", this.pageModel);
        } else {
          console.log("error");
        }
        this.loading = false
      })
    },
    projectCommand(command) {
      if (this.houseList.indexOf(command) == -1) {
        console.log("不存在");
        this.ownHouseName = "所属项目";
      } else {
        console.log("存在", command.houseName);
        this.ownHouseName = command.houseName;
      }
      this.getTestList(true);
    },
    orderCommand(command) {
      console.log(command);
      this.currOrderName = command.orderName;
      this.currOrderType = command.orderType;
      this.getTestList(true);
    },
    createTest() {
      this.$router.push({ path: '/cncTestLists' });
    },
    deleteTest(item, index) {
      const h = this.$createElement;
      this.$msgbox({
        title: '删除',
        message: '删除后不可撤回，确定删除此套测题？',
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '执行中...';
            let data = {
              "id": item._id,
              "operator": "admin"
            }
            api.deleteTest(data).then((res) => {
              if (res.success) {
                instance.confirmButtonLoading = false;
                this.$message.success("删除成功");
                this.pageModel.resultSet.splice(index, 1);
                done();
              } else {
                this.$message.error("删除失败,请重试");
                done();
              }
            })
          } else {
            done();
          }
        }
      }).then(() => {
      }).catch(() => { });
    },
    copyTest(item) {
      let data = {
        "id": item._id,
        "operator": "admin"
      }
      api.copyTest(data).then((res) => {
        if (res.success) {
          this.$message({
            message: '复制成功',
            type: 'success'
          });
          this.pageModel.resultSet.unshift(res.single);
          this.isCopy = true;
          setTimeout(() => {
            this.isCopy = false;
          }, 2000);
        } else {
          this.$message.error('复制失败,请重试');
        }
      })

    },
    editTest(item) {
      this.$router.replace({ path: '/h5editor', query: { itemId: item._id, testcaseId: item.testcaseId } })
    },
    shareTest(item) {
      editorApi.getPageByThemeId(item._id).then((res) => {
        if (res.success) {
          this.shareModel = {
            id: item._id,
            shareTitle: res.single.shareTitle,
            shareContent: res.single.shareContent,
            shareImg: res.single.shareImg,
            shareUrl: res.single.shareUrl
          };
          this.isShareSetting = true;
        } else {
          this.$message.error('数据加载失败,请重试');
        }
      })
    },
    closeShare() {
      this.currItem = "";
      this.isShareSetting = false;
    }
  },
}