<template>
  <div style="width: 400px; padding: 5px; height: 100%">
    <el-row>
      <span style="color: white">{{ info.billboardType }}</span>
    </el-row>
    <el-row
      ><span style="color: aqua">{{ info.billboardName }}</span></el-row
    >
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index"> </el-table-column>
      <el-table-column prop="ReviewContent" label="审查项"> </el-table-column>
      <el-table-column prop="ReviewMethod" label="审查方式"> </el-table-column>

      <el-table-column fixed="right" label="结果" width="100">
        <template slot-scope="scope">
          <el-radio
            :disabled="shenchazhaugntai('符合', scope.row)"
            v-model="scope.row.ruleResult"
            label="true"
            >符合</el-radio
          >
          <el-radio
            :disabled="shenchazhaugntai('不符合', scope.row)"
            v-model="scope.row.ruleResult"
            label="false"
            >不符合</el-radio
          >
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <el-input
        v-model="ReviewComments"
        type="textarea"
        placeholder="请输入审查意见"
      ></el-input>
    </el-row>
    <el-row>
      <el-button size="mini" type="primary" @click="setReview(true)"
        >通过</el-button
      >
      <el-button size="mini" @click="setReview(false)">不通过</el-button>
    </el-row>
  </div>
</template>
<script>
import {
  cartesian3ToWGS84,
  mapQuery,
  flatten,
  mercator2lonLat,
  undergroundMode,
} from "@/utils/MapHelper/MapHelper.js";
import { pointToLineDistance, point, lineString } from "@turf/turf";
export default {
  data() {
    return {
      tableData: [],
      ReviewComments: "",
    };
  },
  props: {
    info: {
      type: Object,
      default: () => {
        return {};
      },
    },
    layerid: {
      type: String,
      default: "",
    },
    lydata: {
      type: Object,
      default: () => {
        return {};
      },
    },
    lyoption: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      let that = this;
      debugger;

      let ltData = await this.getloutiData(that.info);
      //过滤规则
      let billboardReviewList = flatten(window.window.billboardReviewList);
      //获取当前广告牌类型的规则
      let billboardReviewTree = billboardReviewList.find(
        (c) => c.billboardType == that.info.billboardType
      );

      if (billboardReviewTree) {
        let billboardReviews = flatten([billboardReviewTree]);
        //临时加
        //默认
        let mr = billboardReviewList.find((c) => c.code == "001");
        billboardReviews.splice(0, 0, mr);
        billboardReviews.forEach((billboardReview) => {
          if (billboardReview.ReviewItems) {
            billboardReview.ReviewItems.forEach((ReviewItem) => {
              //获取到的单个规则，并对规则判断是否合规
              if (ReviewItem.ReviewMethod == "自动审查") {
                let ruleResult = "true";
                switch (ReviewItem.code) {
                  case "001234": //筑高度不超过24米的多层建筑墙面和高层建筑裙楼的主体墙面
                    if (ltData.length > 0) {
                      //多层高
                      let MHEIGHT = ltData[0].data.find(
                        (c) => c.label == "MHEIGHT"
                      );
                      if (MHEIGHT && MHEIGHT.value) {
                        if (Number(MHEIGHT.value) > 24) {
                          ruleResult = "false";
                        } else {
                          ruleResult = "true";
                        }
                      }
                    }

                    break;
                  case "0010101": //户外广告上沿不得突出墙面（包括女儿墙）的外轮廓线
                    //广告上沿高度
                    let ggsygd = that.info.z + that.info.yL / 2;
                    //楼高
                    if (ltData.length > 0) {
                      //多层高
                      let MHEIGHT = ltData[0].data.find(
                        (c) => c.label == "MHEIGHT"
                      );
                      if (MHEIGHT && MHEIGHT.value) {
                        if (ggsygd > Number(MHEIGHT.value)) {
                          ruleResult = "false";
                        } else {
                          ruleResult = "true";
                        }
                      }
                    }
                    break;
                  case "0010104": //广告设施宽度应与墙面相协调，周围不应超出墙面外轮廓线，垂直方向突出墙面距离不宜大于0.5米
                    debugger;
                    let points = [];
                    if (ltData.length > 0) {
                      ltData[0].geometry.points.forEach((point) => {
                        points.push([point.x, point.y]);
                      });
                      var pt = point([this.info.x, this.info.y]);
                      var line = lineString(points);
                      var distance = (
                        pointToLineDistance(pt, line, {
                          units: "kilometers",
                        }) * 1000
                      ).toFixed(4);
                      let jl = Number(distance) + that.info.zL / 2;
                      if (jl > 0.5) {
                        ruleResult = "false";
                      } else {
                        ruleResult = "true";
                      }
                    }

                    break;
                  case "0010105": //户外广告下端距地面净高不得低于3米，且不得防碍行人安全；
                    //广告下沿高度
                    let ggxygd = that.info.z - that.info.yL / 2;
                    if (ggxygd > 3) {
                      ruleResult = "true";
                    } else {
                      ruleResult = "false";
                    }
                    break;
                  case "001020102": //宽度应以建筑开间为单元,且广告高度不得大于3米
                    let gggd = that.info.xL;
                    if (gggd > 3) {
                      ruleResult = "false";
                    } else {
                      ruleResult = "true";
                    }
                    break;
                  case "00102020102": //一层建筑层高≤4.5m并且广告高度≤1.5m 或者一层建筑层高＞4.5m并且广告高度小于等于2m
                    if (ltData.length > 0) {
                      //层高
                      let FLOORH = ltData[0].data.find(
                        (c) => c.label == "FLOORH"
                      );
                      //广告上沿高度
                      let ggsygd = that.info.z + that.info.yL / 2;
                      if (FLOORH && FLOORH.value) {
                        if (Number(FLOORH.value) > ggsygd) {
                          if (Number(FLOORH.value) <= 4.5) {
                            if (that.info.yL <= 1.5) {
                              ruleResult = "true";
                            } else {
                              ruleResult = "false";
                            }
                          } else if (Number(FLOORH.value) > 4.5) {
                            if (that.info.yL <= 2) {
                              ruleResult = "true";
                            } else {
                              ruleResult = "false";
                            }
                          }
                        } else {
                          ruleResult = "false";
                        }
                      }
                    }

                    break;
                  case "00102020104": //户外广告外表面距离墙面不得超过0.3米（霓虹灯户外广告则外表面距离墙面不得超过0.5米）
                    debugger;
                    var points1 = [];
                    if (ltData.length > 0) {
                      ltData[0].geometry.points.forEach((point) => {
                        points1.push([point.x, point.y]);
                      });
                      var pt1 = point([this.info.x, this.info.y]);
                      var line1 = lineString(points1);
                      var distance1 = (
                        pointToLineDistance(pt1, line1, {
                          units: "kilometers",
                        }) * 1000
                      ).toFixed(4);

                      var jl1 = Number(distance1) + that.info.zL / 2;
                      if (jl1 > 0.3) {
                        ruleResult = "false";
                      } else {
                        ruleResult = "true";
                      }
                    }

                    break;
                  default:
                    break;
                }
                ReviewItem.ruleResult = ruleResult;
              } else {
                ReviewItem.ruleResult = "";
              }

              that.tableData.push(ReviewItem);
            });
          }
        });
      }
    },
    /**
     * 审核状态提交
     */
    setReview(isPass) {
      let item = window.billboardModelList.find((c) => c.id == this.info.id);
      item.ReviewComments = this.ReviewComments;
      if (isPass) {
        item.ReviewStatus = "已通过";
      } else {
        item.ReviewStatus = "未通过";
      }
      this.lyoption.cancel();
      this.$layer.close(this.layerid);
    },
    /**
     * 获取楼体数据
     */
    async getloutiData(info) {
      let layerDataList = [];
      //获取模型信息
      let queryBySQLParameters = {
        getFeatureMode: "BUFFER",
        datasetNames: [
          window.billboardModelLT.dataSourceName +
            ":" +
            window.billboardModelLT.datasetName,
        ],
        geometry: {
          parts: [1],
          points: [{ y: info.y, x: info.x }],
          type: "POINT",
        },
        bufferDistance: 0.000005,
        hasGeometry: true,
      };
      let e = await mapQuery(
        window.billboardModelLT.dataurl +
          "/featureResults.json?returnContent=true",
        queryBySQLParameters
      );
      if (e && e.totalCount > 0) {
        e.features.forEach((feature) => {
          let layerData = { data: [], geometry: feature.geometry };
          feature.fieldNames.forEach((fieldName, i) => {
            let Field = e.datasetInfos
              ? e.datasetInfos[0].fieldInfos.find(
                  (c) =>
                    c.name && c.name.toUpperCase() == fieldName.toUpperCase()
                )
              : null;
            if (Field) {
              layerData.data.push({
                label: fieldName,
                labelCN: Field ? Field.caption : fieldName,
                value: feature.fieldValues[i],
              });
            }
          });
          layerDataList.push(layerData);
        });
      }

      return layerDataList;
    },
    /**
     * 审查状态禁止选中
     */
    shenchazhaugntai(zt, row) {
      if (zt == "符合") {
        if (row.ReviewMethod != "自动审查") {
          return false;
        } else if (row.ReviewMethod == "自动审查" && row.ruleResult == "true") {
          return false;
        } else if (
          row.ReviewMethod == "自动审查" &&
          row.ruleResult == "false"
        ) {
          return true;
        }
      } else {
        if (row.ReviewMethod != "自动审查") {
          return false;
        } else if (
          row.ReviewMethod == "自动审查" &&
          row.ruleResult == "false"
        ) {
          return false;
        } else if (row.ReviewMethod == "自动审查" && row.ruleResult == "true") {
          return true;
        }
      }
    },
  },
};
</script>
<style scoped>
div
  .el-row
  .el-table
  .el-row
  .el-table-column
  .el-radio__label
  .el-table-column {
  font-size: 10px;
}
</style>
