<template>
  <div
    class="ZTGlobal"
    style="width: 100%; padding: 1rem 1rem 0rem 1rem; color: white"
  >
    <el-row :gutter="10">
      <el-col :span="24">
        <el-row
          :gutter="10"
          v-for="DKZBDataList in MXDKZBDataList"
          :key="DKZBDataList"
        >
          <el-col :span="24">
            <el-row
              :gutter="10"
              v-for="DKZBData in DKZBDataList"
              :key="DKZBData"
            >
              <el-col :span="24">
                规划地块：{{ DKZBData.DKBM }}({{ DKZBData.MName }})&nbsp;&nbsp;
                <el-button
                  size="mini"
                  type="default"
                  @click="openZBinfo(DKZBData.infoData)"
                  icon="el-icon-view "
                  >详情</el-button
                >

                <el-table
                  :data="DKZBData.TableData"
                  :highlight-current-row="true"
                  style="width: 100%"
                >
                  <el-table-column label="指标" align="center">
                    <template #default="{ row }">
                      {{ row.zbmc }}
                    </template>
                  </el-table-column>
                  <el-table-column label="计算书指标" align="center">
                    <template #default="{ row }">
                      {{ row.tjfazb }}
                    </template>
                  </el-table-column>
                  <el-table-column label="自动核算指标" align="center">
                    <template #default="{ row }">
                      {{ row.fazbyg }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="人工核算指标"
                    align="center"
                    width="130"
                  >
                    <template #default="{ row }">
                      <el-input
                        v-model="row.rghszb"
                        size="small"
                        @blur="handleSave(row, DKZBData.TableData)"
                      ></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="控规要求" align="center">
                    <template #default="{ row }">
                      {{ row.kgyq }}
                    </template>
                  </el-table-column>
                  <el-table-column label="结论" align="center">
                    <template #default="{ row }">
                      <div v-if="row.jl != null">
                        {{ row.jl ? "合规" : "不合规" }}
                      </div>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import detailedInfo from "./detailedInfo.vue";
import { v4 as uuidv4 } from "uuid";
import {
  getZtProjectModelZb,
  addZtProjectModelZb,
  updateZtProjectModelZb,
} from "@/api/zt/ztApi.js";
export default {
  data() {
    return {
      //模型地块指标数组
      MXDKZBDataList: [],
    };
  },
  props: {
    info: {
      type: Object,
      default: () => {
        return {};
      },
    },
    layerid: {
      type: String,
      default: "",
    },
    lydata: {
      type: Object,
      default: () => {
        return {};
      },
    },
    lyoption: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      let that = this;
      that.MXDKZBDataList = [];
      debugger;
      for (let index = 0; index < this.info.tableDataList.length; index++) {
        const element = this.info.tableDataList[index];
        // this.info.tableDataList.forEach((element) => {
        let DKZBDataList = [];
        for (let Datai = 0; Datai < element.length; Datai++) {
          const Data = element[Datai];
          let DKZBData = {
            infoData: Data.infoData,
            MName: Data.Minfo.modelname,
            DKBM: Data.KG_Data.DKBM,
          };
          //人工核算指标

          let rgzbData =
            (await getZtProjectModelZb(Data.Minfo.id + "" + Data.KG_Data.DKBM))
              .data || {};
          let rgzbid = rgzbData.id
            ? rgzbData.id
            : Data.Minfo.id + "" + Data.KG_Data.DKBM;
          let rgzb = rgzbData.zbcs ? JSON.parse(rgzbData.zbcs) : {};
          let TableData = [];
          let YDMJrow = {};
          YDMJrow.zbmc = "用地总面积";
          YDMJrow.fazbyg = Number(Data.FA_Data.YDMJ).toFixed(2);
          YDMJrow.kgyq = Number(Data.KG_Data.YDMJ).toFixed(2);

          if (!rgzb.YDMJ) {
            rgzb.YDMJ = YDMJrow.fazbyg;
            YDMJrow.rghszb = YDMJrow.fazbyg;
            YDMJrow.rgzbid = rgzbid;
            YDMJrow.rgzbName = "YDMJ";
          } else {
            YDMJrow.rghszb = rgzb.YDMJ;
            YDMJrow.rgzbid = rgzbid;
            YDMJrow.rgzbName = "YDMJ";
          }
          if (YDMJrow.kgyq && YDMJrow.rghszb) {
            if (Number(YDMJrow.kgyq) >= Number(YDMJrow.rghszb)) {
              YDMJrow.jl = true;
            } else {
              YDMJrow.jl = false;
            }
          } else {
            YDMJrow.jl = null;
          }

          TableData.push(YDMJrow);

          let JZMJrow = {};
          JZMJrow.zbmc = "建筑面积";
          JZMJrow.tjfazb = Number(Data.TJFA_Data.JZMJ).toFixed(2);
          JZMJrow.fazbyg = Number(Data.FA_Data.JZMJ).toFixed(2);
          JZMJrow.kgyq = Number(Data.KG_Data.JZMJ).toFixed(2);

          if (!rgzb.JZMJ) {
            rgzb.JZMJ = JZMJrow.fazbyg;
            JZMJrow.rghszb = JZMJrow.fazbyg;
            JZMJrow.rgzbid = rgzbid;
            JZMJrow.rgzbName = "JZMJ";
          } else {
            JZMJrow.rghszb = rgzb.JZMJ;
            JZMJrow.rgzbid = rgzbid;
            JZMJrow.rgzbName = "JZMJ";
          }
          if (JZMJrow.kgyq && JZMJrow.rghszb) {
            if (Number(JZMJrow.kgyq) >= Number(JZMJrow.rghszb)) {
              JZMJrow.jl = true;
            } else {
              JZMJrow.jl = false;
            }
          } else {
            JZMJrow.jl = null;
          }

          TableData.push(JZMJrow);

          let JDMJrow = {};
          JDMJrow.zbmc = "基底面积";
          JDMJrow.fazbyg = Number(Data.FA_Data.JDMJ).toFixed(2);
          JDMJrow.kgyq = Data.KG_Data.JDMJ;
          if (!rgzb.JDMJ) {
            rgzb.JDMJ = JDMJrow.fazbyg;
            JDMJrow.rghszb = JDMJrow.fazbyg;
            JDMJrow.rgzbid = rgzbid;
            JDMJrow.rgzbName = "JDMJ";
          } else {
            JDMJrow.rghszb = rgzb.JDMJ;
            JDMJrow.rgzbid = rgzbid;
            JDMJrow.rgzbName = "JDMJ";
          }
          if (JDMJrow.rghszb && JDMJrow.kgyq) {
            if (Number(JDMJrow.kgyq) >= Number(JDMJrow.rghszb)) {
              JDMJrow.jl = true;
            } else {
              JDMJrow.jl = false;
            }
          } else {
            JDMJrow.jl = null;
          }

          TableData.push(JDMJrow);

          let JRJZMJrow = {};
          JRJZMJrow.zbmc = "计容建筑面积";
          JRJZMJrow.tjfazb = Number(Data.TJFA_Data.JRJZMJ).toFixed(2);
          JRJZMJrow.fazbyg = Number(Data.FA_Data.JRJZMJ).toFixed(2);
          debugger;

          if (Data.KG_Data.JRJZMJX && Data.KG_Data.JRJZMJD) {
            JRJZMJrow.kgyq = Data.KG_Data.JRJZMJX + "/" + Data.KG_Data.JRJZMJD;
          } else {
            JRJZMJrow.kgyq = "";
          }
          if (!rgzb.JRJZMJ) {
            rgzb.JRJZMJ = JRJZMJrow.fazbyg;
            JRJZMJrow.rghszb = JRJZMJrow.fazbyg;
            JRJZMJrow.rgzbid = rgzbid;
            JRJZMJrow.rgzbName = "JRJZMJ";
          } else {
            JRJZMJrow.rghszb = rgzb.JRJZMJ;
            JRJZMJrow.rgzbid = rgzbid;
            JRJZMJrow.rgzbName = "JRJZMJ";
          }

          if (JRJZMJrow.rghszb && JRJZMJrow.kgyq) {
            if (
              Number(JRJZMJrow.rghszb) >= Number(Data.KG_Data.JRJZMJX) &&
              Number(JRJZMJrow.rghszb) <= Number(Data.KG_Data.JRJZMJD)
            ) {
              JRJZMJrow.jl = true;
            } else {
              JRJZMJrow.jl = false;
            }
          } else {
            JRJZMJrow.jl = null;
          }

          TableData.push(JRJZMJrow);

          let RJLrow = {};
          RJLrow.zbmc = "容积率";
          RJLrow.fazbyg = Data.FA_Data.RJL;
          RJLrow.kgyq = Data.KG_Data.RJLX + "/" + Data.KG_Data.RJLD;

          if (!rgzb.RJL) {
            rgzb.RJL = RJLrow.fazbyg;
            RJLrow.rghszb = RJLrow.fazbyg;
            RJLrow.rgzbid = rgzbid;
            RJLrow.rgzbName = "RJL";
          } else {
            RJLrow.rghszb = rgzb.RJL;
            RJLrow.rgzbid = rgzbid;
            RJLrow.rgzbName = "RJL";
          }

          if (RJLrow.rghszb && RJLrow.kgyq) {
            if (
              Number(RJLrow.rghszb) >= Number(Data.KG_Data.RJLX) &&
              Number(RJLrow.rghszb) <= Number(Data.KG_Data.RJLD)
            ) {
              RJLrow.jl = true;
            } else {
              RJLrow.jl = false;
            }
          }

          TableData.push(RJLrow);

          let JZMDrow = {};
          JZMDrow.zbmc = "建筑密度";
          JZMDrow.fazbyg = Data.FA_Data.JZMD;
          JZMDrow.kgyq = Data.KG_Data.JZMDX + "/" + Data.KG_Data.JZMDD;
          if (!rgzb.JZMD) {
            rgzb.JZMD = JZMDrow.fazbyg;
            JZMDrow.rghszb = JZMDrow.fazbyg;
            JZMDrow.rgzbid = rgzbid;
            JZMDrow.rgzbName = "JZMD";
          } else {
            JZMDrow.rghszb = rgzb.JZMD;
            JZMDrow.rgzbid = rgzbid;
            JZMDrow.rgzbName = "JZMD";
          }
          if (JZMDrow.rghszb && JZMDrow.kgyq) {
            if (
              Number(JZMDrow.rghszb) >= Number(Data.KG_Data.JZMDX) &&
              Number(JZMDrow.rghszb) <= Number(Data.KG_Data.JZMDD)
            ) {
              JZMDrow.jl = true;
            } else {
              JZMDrow.jl = false;
            }
          } else {
            JZMDrow.jl = null;
          }

          TableData.push(JZMDrow);

          let JZXGDrow = {};
          JZXGDrow.zbmc = "建筑限高";
          JZXGDrow.fazbyg = Data.FA_Data.JZGD;
          JZXGDrow.kgyq = Data.KG_Data.JZXGX + "/" + Data.KG_Data.JZXGD;
          if (!rgzb.JZGD) {
            rgzb.JZGD = JZXGDrow.fazbyg;
            JZXGDrow.rghszb = JZXGDrow.fazbyg;
            JZXGDrow.rgzbid = rgzbid;
            JZXGDrow.rgzbName = "JZGD";
          } else {
            JZXGDrow.rghszb = rgzb.JZGD;
            JZXGDrow.rgzbid = rgzbid;
            JZXGDrow.rgzbName = "JZGD";
          }
          if (JZXGDrow.rghszb && JZXGDrow.kgyq) {
            if (
              Number(JZXGDrow.rghszb) >= Number(Data.KG_Data.JZXGX) &&
              Number(JZXGDrow.rghszb) <= Number(Data.KG_Data.JZXGD)
            ) {
              JZXGDrow.jl = true;
            } else {
              JZXGDrow.jl = false;
            }
          } else {
            JZXGDrow.jl = null;
          }

          if (!rgzbData.id) {
            rgzbData.id = rgzbid;
            rgzbData.zbcs = JSON.stringify(rgzb);
            //这里替换为后台添加表，现在纯前台，自动更新更新
            // window.project_model_zb.push(rgzb);

            await addZtProjectModelZb(rgzbData);
          }
          // else {
          //   //这里添加后台修改表，现在纯前台，自动更新更新
          // }
          TableData.push(JZXGDrow);
          DKZBData.TableData = TableData;
          DKZBDataList.push(DKZBData);
        }
        that.MXDKZBDataList.push(DKZBDataList);
        // });
      }
    },
    async handleSave(row, TableData) {
      debugger;
      let rgzb = {};
      TableData.forEach((data) => {
        rgzb[data.rgzbName] = data.rghszb;
      });
      await updateZtProjectModelZb({
        id: row.rgzbid,
        zbcs: JSON.stringify(rgzb),
      });
      this.init();
      // let rgzb = window.project_model_zb.find((c) => c.id == row.id);
      // if (rgzb) {
      //   rgzb[row.rgzbName] = row.rghszb;
      //   this.init();
      // }
    },
    /**
     * 弹窗显示详情
     * @param infoData
     */
    openZBinfo(infoData) {
      let w = document.body.offsetWidth;
      let h = document.body.offsetHeight;
      //左上角(left=弹窗宽/2;top=弹窗高/2+header高)
      //右上角 (left=w-弹窗宽+(弹窗宽/2);top=弹窗高/2+header高);
      let left = w - 430 + 430 / 2;
      let top = 430 / 2 + 60;
      this.$layer.iframe({
        id: "ZBdetailedInfo",
        content: {
          content: detailedInfo, //传递的组件对象
          parent: this, //当前的vue对象
          data: { info: { infoData } }, //props
        },
        // offset: [left, top], //left top
        area: ["64rem", "40rem"], //宽 高
        title: "详细信息",
        maxmin: false,
        shade: false, //是否显示遮罩
        shadeClose: false, //点击遮罩是否关闭
        cancel: () => {
          //关闭事件
        },
      });
    },
  },

  beforeDestroy() {},
};
</script>
<style lang="scss">
@import "@/../../zt.scss";
</style>
<style lang="scss" scoped>
.el-card {
  border: 0px solid #02a7f0;
}
.el-form-item {
  margin-bottom: 0;
}
</style>
