<template>
  <div class="companyInfo">
    <headNav title="单位信息" @rightClick="rightClick" iconName="edit"></headNav>

    <!-- 基础信息 -->
    <div class="basicInfo">
      <div class="headTip FontColorInfo">基础信息</div>

      <div class="basicInfo_cardInfo">
        <van-cell title="单位名称" :value="info.enforce_obj_name" />
        <van-cell title="统一社会信用代码" :value="info.credit_code" />
        <van-cell title="单位地址" center :value="info.enforce_obj_address">
          <template #right-icon>
            <van-icon
              name="location"
              class="search-icon"
              color="#0066cc"
              @click="getCenter(info)"
            />
          </template>
        </van-cell>
        <van-cell title="成立日期" :value="info.register_date" />
        <van-cell title="营业期限" :value="info.operating_date" />
        <van-cell title="单位法人" :value="info.legal_person" />
        <van-cell title="联系方式" :value="info.legal_person_phone" />
        <van-cell title="经营状态" :value="info.manage_status">
          <template #right-icon>
            <span class="success icon-mark" v-if="info.manage_status == '正常'"></span>
            <span class="warning icon-mark" v-else-if="info.manage_status == '冻结'"></span>
            <span class="error icon-mark" v-else-if="info.manage_status == '注销'"></span>
          </template>
        </van-cell>

        <!-- <van-cell
          :value="companyInfo.unitlegalPerson"
          is-link
          @click="gotoPersonnalInfo(companyInfo.id)"
        >
          <template #title>
            <span class="custom-title">单位法人</span>
          </template>
        </van-cell> -->
        <!-- <van-cell title="消防安全责任人" :value="companyInfo.fireSafetyPerson + ' ' + companyInfo.contactPhone" />
				<van-cell title="管辖机构" :value="companyInfo.fireControl" />
				<van-cell title="所属辖区" :value="companyInfo.jurisdiction" /> -->
        <!-- alue="一般单位"
        /> -->
      </div>
    </div>
    <!-- 消防信息 -->
    <div class="relateDoc">
      <div class="headTip FontColorInfo">消防信息</div>
      <van-cell title="所属辖区" :value="info.enforce_org_name" />
      <van-cell
        v-if="info.enforce_obj_type === '1'"
        title="单位规模"
        value="火灾高危单位"
      />
      <van-cell
        v-if="info.enforce_obj_type === '2'"
        title="单位规模"
        value="消防安全重点单位"
      />
      <van-cell
        v-if="info.enforce_obj_type === '3'"
        title="单位规模"
        value="一般单位"
      />
      <van-cell
        v-if="info.enforce_obj_type === '4'"
        title="单位规模"
        value="其他单位"
      />
      <van-cell title="场所类型" :value="info.placeType" />
      <van-cell title="所在建筑" :value="info.architectureType"/>
      <van-cell title="消防安全" :value="info.holeResponsibility" />
      <van-cell title="面积大小" :value="info.area"/>
      <van-cell title="单位体量" :value="info.obj_mass"/>
    </div>
    <!-- 消防联系人信息 -->
    <div class="relateDoc">
      <div class="headTip FontColorInfo">消防联系人</div>
      <van-cell title="消防安全责任人" :value="info.charge_person" />
      <van-cell title="联系方式" :value="info.charge_person_phone" />
      <van-cell title="消防安全管理人" :value="info.manage_person" />
      <van-cell title="联系方式" :value="info.manage_person_phone" />
    </div>
    <!-- 相关文件 -->
    <div class="relateDoc">
      <div class="headTip FontColorInfo">相关文件</div>

      <div v-if="info.certificates">
        <!-- <div class="nameOfDoc FontColorNormal OPoint FontColorOther">
          {{ fileInfo.fileName }}
        </div>
        <div class="docInfo df jfsb FontColorInfo fz12 mt10">
          <div>
            文件编号：<span>{{ fileInfo.docNum }}</span>
          </div>
          <div>
            上传时间：<span v-if="fileInfo">{{
              fileInfo.createTime.split(" ")[0]
            }}</span>
          </div>
        </div> -->
        <div v-for="item in info.certificates" :key="item.id">
          <van-cell title="文件编号" :value="item.cer_no" />
          <van-cell title="消防设施" :value="item.facility" />
          <van-cell v-if="item.file_ids" title="证件文件">
            <van-image v-for="(url, urlInd) in item.file_ids" :key="urlInd" width="60" height="60" :src="url" @click="previewImage(item.file_ids, urlInd)"/>
          </van-cell>
        </div>
      </div>

      <div v-else class="Doc df fb jfc FontColorOther">暂无相关文件</div>

      <div class="nearError">
        <van-cell
          is-link
          @click="hiddenDanger"
          title="近期检查隐患"
          :value="`${problem_num}项隐患`"
        />
      </div>

      <!-- 			<div class="farMonitor">
				<van-cell is-link title="远程监控信息" />
			</div>

			<div class="maintainRecord">
				<van-cell is-link title="维护记录" />
			</div> -->
    </div>

    <!-- 遮罩层 -->
    <v-mask :showOverLay="showOverLay"></v-mask>
    <!-- 弹出地图展示 -->
    <van-popup v-model="showMap" position="bottom" :style="{ height: '450px' }">
      <div v-if="showMap">
        <baiduMap :center="center" :coor="coor" :zoom="zoom"></baiduMap>
      </div>
    </van-popup>
  </div>
</template>

<script>
import headNav from "@/components/headNav.vue";
import { get } from "@/utils/http.js";
import mask_overLay from "@/components/overLay_xy.vue";
import baiduMap from "@/components/baiduMap.vue";
import { ImagePreview } from 'vant';
import { handleProblemImgs } from '@/utils/index';

export default {
  name: "companyInfo",
  components: {
    headNav,
    baiduMap,
    "v-mask": mask_overLay,
  },
  data() {
    return {
      companyInfo: "",
      showOverLay: false,
      fileInfo: "",
      hasFile: false,
      recentTime: "",
      info: {},
      showMap: false,
      center: "重庆市",
      zoom: 15,
      coor: null,
      problem_num: '0',
      placeTypes: [],
      problemList: [],
    };
  },
  computed: {
    objId() {
      return this.$route.params.ID
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.showOverLay = true;
      const res = await get('/v2/api/dictionary/place_type/items');
      if (res && res.length > 0) {
        this.placeTypes = res;
      }
      this.getCompanyInfoApi();
      this.recentTime = this.$store.state.taskInfo.taskInfo.executeTime;
    },
    rightClick() {
      if (this.objId) {
        this.$router.push(`/companyEdit/${this.objId}`);
      }
    },
		previewImage(list, index) {
			ImagePreview({
				images: list,
				startPosition: index
			});
		},
    // 获取单位信息
    getCompanyInfoApi() {
      this.showOverLay = true;
      this.$http.get(`/v2/api/enforce-obj/${ this.objId }`).then(res => {
      	if (res) {
      		const obj = res.data;
          obj.architectureType = '';
          if (obj.architecture_type) {
            obj.architectureType = obj.architecture_type == 1 ? '高层建筑' : '非高层建筑';
          }
          obj.holeResponsibility = '';
          if (obj.hole_responsibility) {
            obj.holeResponsibility = obj.hole_responsibility == 1 ? '场所负责整栋建筑消防安全' : '场所负责部分建筑消防安全';
          }
          if (obj.place_type && this.placeTypes) {
            const place = this.placeTypes.filter(item => obj.place_type == item.code);
            if (place.length > 0) {
              obj.placeType = place[0].name;
            }
          }
          if (obj.certificates) {
            obj.certificates.map(item => {
              let arr = [];
              if (item.file_ids) {
                item.file_ids.split(',').map(fileId => {
                  arr.push(handleProblemImgs(fileId));
                });
              }
              item.file_ids = arr;
            });
          }
          const manageStatus = {
            '0': '正常',
            '1': '冻结',
            '2': '注销'
          }
          obj.manage_status = manageStatus[obj.manage_status] || ''
          this.info = obj;
          this.getRecentItems()
      	}
        this.showOverLay = false;
        // else {
      	// 	this.$toast.fail(`${ res.msg }`);
      	// }
      });
      // this.problem_num = this.$route.params.ID;
      // this.info = this.$route.params;
    },
    getRecentItems() {
      this.$http.get(`/v2/api/enforce-job/item?page=1&size=100000&query=check_result==2;enforce_obj_id==${this.objId}`).then(res => {
        if (res.data) {
          this.problem_num = res.data.total + ''
          this.problemList = res.data.data || []
        }
      })
    },
    async getFileInfoApi() {
      await get("/cdenforceobjI/listByCondition", {
        enforceObjId: this.$route.params.ID,
      }).then((res) => {
        if (res.success === true) {
          if (res.data.length > 0) {
            this.fileInfo = res.data[0];
            this.hasFile = true;
          } else {
            this.hasFile = false;
          }
        } else {
          this.$toast.fail(`${res.msg}`);
          this.hasFile = false;
        }
      });
    },

    backPage() {
      this.$router.go(-1);
    },
    // 获取地图中心位置
    getCenter(info) {
      if (info) {
        if (window.BMap) {
          this.center = window.BMap.Point(Number(info.longitude), Number(info.latitude))
        }
        this.coor = {
          lng: info.longitude,
          lat: info.latitude,
        }
        this.showMap = true;
      }
    },
    // 人员信息页
    gotoPersonnalInfo(id) {
      this.$router.push(`/personnelInfo/${id}`);
    },

    // 文件详情页
    turnToFileDetail() {
      this.$router.push(`/fileDetail/${this.$route.params.ID}`);
    },

    // 跳转隐患详情页面
    hiddenDanger() {
      this.$router.push(`/history/${this.objId}`);
      // let taskId = this.$store.state.taskInfo.taskInfo.taskId;
      // this.$router.push(`/hiddenDanger/${this.$route.params.id}/${taskId}`);
      // this.$router.push(`/newHiddenDanger/${this.$route.params.ID}`);
      // this.$router.push({
      //   name: 'historyItems',
      //   params: {
      //     list: this.problemList
      //   }
      // })
    },
  },
};
</script>

<style lang="scss" scoped>
.companyInfo {
  font-size: 14px;
  height: 100vh;
  overflow: auto;

  .headTip {
    margin-bottom: 10px;
    padding: 0 15px;
  }

  .basicInfo {
    margin-top: 57px;
  }

  .relateDoc {
    margin-top: 10px;

    .Doc {
      background-color: #fff;
      padding: 20px 15px;

      .nameOfDoc {
        width: 100%;
      }
    }

    .nearError,
    .farMonitor,
    .maintainRecord {
      margin-top: 20px;
    }
  }
}
</style>

<style lang="scss">
.companyInfo {
  .van-cell {
    font-size: 13px;

    .van-cell__title {
      color: #4f4f4f;
      flex: 2;
    }

    .van-cell__value {
      flex: 3;
      white-space: normal;
    }
  }
}
.basicInfo_cardInfo {
  .van-cell {
    align-items: center;
    .icon-mark {
      width: 12px;
      height: 12px;
      border-radius: 50%;
      margin-left: 5px;
      &.success {
        background-color: #4BD863;
      }
      &.warning {
        background-color: #FE9400;
      }
      &.error {
        background-color: #FF3A2F;
      }
    }
  }
  .van-cell__value {
    white-space: normal;
  }
}
</style>
