<template>
  <div class="scjg">
    <div class="downloadDiv">
      <div>
        <span class="xmmc">
          项目名称：
          <span class="text">{{ scjgObj.xmmc }}</span>
        </span>
        <div
          class="eicon"
          :class="isshowAll ? 'eyes' : 'close_eyes'"
          @click="allChange('all')"
        ></div>
      </div>
      <div>
        分析面积：
        <span class="text">{{ compute(scjgObj.fxmj) }}亩</span>
        <span class="export" @click="download">导出报告</span>
      </div>
    </div>

    <div v-for="(eitem, i) in echarts" :key="i" class="echars">
      <div class="echartTitle">
        <div class="block-title">{{ eitem.label }}</div>
        <div
          class="eicon"
          :class="eitem.iseyes ? 'eyes' : 'close_eyes'"
          @click="eyesChaneg(i)"
        ></div>
        <div
          class="eicon"
          :class="eitem.isshow ? 'eshow' : 'eclose'"
          @click="eitem.isshow = !eitem.isshow"
        ></div>
      </div>
      <scjgContent
        :ref="`contentRef${eitem.id}`"
        v-show="eitem.isshow"
        :litem="eitem"
        :piseyes="eitem.iseyes"
        @mapview="changeDataSources"
      ></scjgContent>
      <el-collapse v-if="eitem.lchildren.length" v-show="eitem.isshow">
        <collRecursiveTree
          :data="eitem.lchildren"
          :piseyes="eitem.iseyes"
          @addrefs="addrefs"
          @mapview="changeDataSources"
        ></collRecursiveTree>
      </el-collapse>
    </div>
    <!-- <div class="bottomBtns">
      <span></span>
      <span class="sureBtn" @click="download">导出报告</span>
    </div> -->
  </div>
</template>

<script>
import scjgContent from "./scjgContent.vue";
import collRecursiveTree from "./collRecursiveTree.vue";
import { GetFxjg } from "@/api/ghss/hgxfx.js";
// import hgxfx from "../../../../static/data/ghss/data.js";
import parse from "wellknown";
let dataSourceList = {};
let colors = [
  "#62ADED",
  "#DFE15A",
  "#6EDC8D",
  "#00A42E",
  "#F9B447",
  "#7F4FE5",
  "#FF6969",
  "#27CED9",
  "#DF56F5",
  "#DCFFAF",
];
export default {
  components: { scjgContent, collRecursiveTree },
  props: {
    scjgObj: {
      type: Object,
    },
  },
  data() {
    return {
      echarts: [],
      ruleForm: {
        name: "",
        xzmj: "",
        fileList: [],
      },
      tempdataSourcesId: null,
      fileid: 1,
      reflist: {},
      isshowAll: true,
    };
  },
  mounted() {
    // this.initData();
  },
  methods: {
    compute(mj) {
      return mj ? (mj * 0.0015).toFixed(2) : 0;
    },
    addrefs(key, ref) {
      this.reflist[key] = ref;
    },
    initData() {
      this.echarts = [];
      this.$emit("updateParent", "loading", true);
      this.addPolygon(this.$props.scjgObj.geom, "all", "#ff0000", true);
      GetFxjg({ bsm: this.$props.scjgObj.bsm }).then((res) => {
        if (res.success) {
          res.data.forEach((e) => {
            let c = e.dataList || [];
            c.map((ci) => {
              ci.name = ci.yslx_name || ci.scxname;
              // ci.value = this.compute(ci.mj);
              ci.value = ci.mj;
              ci.geom = ci.geom || "";
              // ci.label = ci.mc_name;
            });
            this.echarts.push({
              id: e.scxbsm,
              label: e.scxname,
              dataList: e.dataList,
              lchildren: e.children || [],
              iseyes: false,
              isshow: false,
              scxstyle: e.scxstyle,
            });
            if (e.scxstyle == 0) this.setEchart(c, e.scxbsm);
            if (e.children) this.lForEach(e.children, "echart");
          });
          this.$emit("updateParent", "loading", false);
        }
      });
    },
    allChange(id) {
      this.isshowAll = !this.isshowAll;
      if (dataSourceList[id]) {
        dataSourceList[id].show = this.isshowAll;
      } else {
        this.addPolygon(this.$props.scjgObj.geom, "all", "#ff0000", true);
      }
    },
    eyesChaneg(i) {
      this.echarts[i].iseyes = !this.echarts[i].iseyes;
      // if (i == 0 || i == 2) {
      //   this.changeDataSources(this.echarts[i]);
      // } else {
      let iseyes = this.echarts[i].iseyes;
      if (this.echarts[i].dataList)
        this.echarts[i].dataList.forEach((child, ci) => {
          this.changeDataSources({ ...child, iseyes }, colors[ci]);
        });
      this.lForEach(this.echarts[i].lchildren, "sources", iseyes);
      // }
      // emit("eyesChaneg");
    },
    lForEach(data, fun, iseyes) {
      data.forEach((child) => {
        if (child.dataList) {
          child.dataList.forEach((ld, li) => {
            if (fun == "sources")
              this.changeDataSources({ ...ld, iseyes }, "#62ADED");
            else {
              ld.name = ld.yslx_name;
              ld.value = ld.mj;
            }
          });
        }
        if (child.children) {
          this.lForEach(child.children, fun, iseyes);
        }

        if (
          fun == "echart" &&
          child.scxstyle == 0 &&
          child.scxbsm.indexOf("GGFWSS") >= 0
        ) {
          child.id = child.scxbsm;
          setTimeout(() => {
            this.setEchart(child.dataList, child.id, child.id);
          }, 200);
        }
      });
    },
    changeDataSources({ geom, id, iseyes, iszoom }, color) {
      if (iszoom) {
        this.zoomTo(id, geom);
      } else if (dataSourceList[id]) {
        dataSourceList[id].show = iseyes;
        this.tempdataSourcesId = null;
      } else if (geom) {
        this.addPolygon(geom, id, color);
      }
    },
    zoomTo(id, geom) {
      let zid = "zoom" + id;
      if (!dataSourceList[zid]) {
        this.tempdataSourcesId = zid;
        this.addPolygon(geom, zid, "#ff0000");
      }
    },
    setEchart(data, id, childid) {
      this.$nextTick(() => {
        let type = data.length <= 6 ? "horizontal" : "vertical";
        if (!childid) this.$refs[`contentRef${id}`][0].setEchart(data, type);
        else {
          this.reflist[`contentRef${id}`].setEchart(data, type, childid);
        }
      });
    },
    // 加载GeoJSON数据
    addPolygon(geom, id, colors, fillreset) {
      let geojson = parse(geom);
      let _this = this;
      // viewer.entities.removeAll();
      let scolor = colors
        ? Cesium.Color.fromCssColorString(colors)
        : Cesium.Color.RED;
      let fcolor =
        colors && !fillreset
          ? Cesium.Color.fromCssColorString(colors)
          : Cesium.Color.WHITE;

      let polygon = Cesium.GeoJsonDataSource.load(geojson, {
        clampToGround: true,
        stroke: scolor,
        fill: fcolor.withAlpha(0.3), //注意：颜色必须大写，即不能为blue
        strokeWidth: 3,
      });
      polygon.then(function (dataSource) {
        // dataSource.id = id;
        // 将数据源添加到Cesium Viewer
        viewer.dataSources.add(dataSource);
        viewer.zoomTo(dataSource);
        dataSourceList[id] = dataSource;
        // 可以获取实体并进行操作
        // dataSource.entities.values.forEach((entity) => {
        //   // 你可以在这里设置实体的属性，例如位置、颜色等
        //   // console.log(entity);
        // });
      });
    },

    download() {
      // window.open(this.$props.scjgObj.fxbg); //this.$props.scjgObj.task.fxbg
      window.open(
        window.axiosURI +
          "/analyse/fzss/DownloadReport?filePath=" +
          `${this.$props.scjgObj.fxbg}`
      );
    },
    viewReport() {
      window.open(this.$props.scjgObj.fxbg.replace(".docx", ".pdf"), "_blank");
    },
    reset() {
      if (Object.keys(dataSourceList).length) viewer.dataSources.removeAll();
      dataSourceList = {};
    },
  },

  // watch(
  //     () => prop.scjgObj,
  //     (newValue, oldValue) => {
  //       this.jcfwlist[0].value = newValue.task.fxmj + "m²";
  //       nextTick(() => {
  //         this.setEcharts(newValue);
  //       });
  //     }
  //   );
  watch: {
    // scjgObj(newValue) {
    //   this.initData();
    // },
    //监听区域id的变化,用于删除上一次点击生成的geojson区域面
    tempdataSourcesId(newVal, oldVal) {
      if (oldVal) {
        viewer.dataSources.remove(dataSourceList[oldVal]);
        dataSourceList[oldVal] = undefined;
      }
    },
  },
};
</script>

<style lang="scss">
.scjg {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 10px;
  line-height: 40px;
  .downloadDiv {
    position: relative;
    .xmmc {
      width: calc(100% - 60px);
      display: inline-block;
    }
    .export {
      width: 95px;
      height: 28px;
      background: #0f7ac8;
      text-align: center;
      line-height: 32px;
      display: inline-block;
      position: absolute;
      right: 2px;
      bottom: 6px;
    }
    .text {
      color: #cddeeb;
    }
  }

  .eicon {
    width: 20px;
    height: 20px;
    margin-top: 10px;
    background-size: 100% 100%;
    display: inline-block;
    cursor: pointer;
  }
  .eyes {
    background-image: url("/static/images/ghzc/eyes.png");
  }
  .close_eyes {
    background-image: url("/static/images/ghzc/close_eyes.png");
  }

  .echars {
    margin-bottom: 10px;
    .echartTitle {
      width: 100%;
      height: 40px;
      display: flex;
      justify-content: space-between;
      // background: rgba(38, 38, 38, 0.9);
      border-bottom: 1px solid #166cad;
      margin-right: 10px;
      line-height: 40px;

      .block-title {
        width: calc(100% - 50px);
      }
      .eshow {
        background-image: url("/static/images/ghzc/to_bottom.png");
      }
      .eclose {
        background-image: url("/static/images/ghzc/to_right.png");
      }
    }
    .echart {
      width: 380px;
      height: 220px;
    }
    .echart_vertical {
      width: 380px;
      height: 370px;
    }
  }
}
</style>
