<template>
  <div id="print1" v-show="true">
    <div id="print2">
      <style id="style1">
        #print2 td{
          height:28px;
          width: 100px;
          text-align: center;
        }
        #print2 table{
          margin: auto;
        }
        #print2 h1{
          text-align: center;
        }
        #print2 p{
          text-align: center;
        }
        .border-btn{
          border-bottom: #000000 solid 1px;
        }
      </style>
      <h1>{{loginorg}}</h1>
      <p>安检时间:{{userinfo.f_offsite_time}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </p>
      <table border="1" cellspacing="0">
        <tr>
          <td><b>用户编号</b></td><td>{{userinfo.f_userinfo_code}}</td><td ><b>用户姓名</b></td><td >{{userinfo.f_user_name}}</td><td><b>联系电话</b></td>
          <td>{{userinfo.f_user_phone}}</td>
        </tr>
        <tr>
          <td><b>地址</b></td><td colspan="5">{{userinfo.f_address}}
        </tr>
        <tr align="left" v-if="userinfo.f_entry_status=='入户'">
          <td><b>安检信息</b></td>
          <td colspan="5" style="padding: 0px">
            <table border="0" cellspacing="0" width="100%"  style="margin: 0px">
              <tr v-for="devi in deviceconfig2" :key="devi.index">
                <td style="border-right:#000000 solid 1px;width: 20%!important;" :class="$index == devices.length-1?'':'border-btn'">
                  {{devi.checkname}}
                </td>
                <!--<td>{{devi.value[0].name}}</td>-->
                <td colspan="4" style="padding: 0px;width: 80%!important;">
                  <table border="0" cellspacing="0" width="100%" style="margin: 0px">
                    <tr v-for="deviinfo in devi.items" :key="deviinfo.index">
                      <td style="border-right:#000000 solid 1px;width: 70%!important" :class="($parent.$index == devices.length-1 && $index == devi.value.length-1)?'':'border-btn'">
                        {{deviinfo.lname}}
                      </td>
                      <td style="width: 30%!important" :class="($parent.$index == devices.length-1 && $index == devi.value.length-1)?'':'border-btn'">
                        <label>{{getDeviceValue(devi.index,deviinfo.lname)}}</label>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <tr style="height: 80%">
          <td><b>安检结果</b></td>
          <td colspan="5">
            {{defectCount}}
          </td>
        </tr>
        <tr>
<!--          <td><b>用户签字</b></td><td colspan="2 "><img-self :src='userinfo.f_sign_path' alt="未签名"></img-self></td>-->
          <td><b>安检员</b></td><td colspan="5">{{data.f_checker_name}}</td>
        </tr>
      </table>
    </div>
  </div>
  <modal :show.sync="show" v-ref:modal :backdrop="isBack">
    <div slot="modal-header" class="modal-header">
      <span class="modal-title font">
        安检单打印
      </span>
    </div>
    <div slot="modal-body" class="modal-body">
      确定打印安检单？
    </div>
    <div slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-cancel" @click="cancel()">取消</button>
      <print-element @click="getHidden" class="btn btn-success width-80" id="print1" styleid='style1' top='40' left='60' width='100%' height='100%' v-ref:printelement>打印</print-element>
    </div>
  </modal>
</template>

<script>

import co from 'co'
import { PagedList,HttpResetClass } from 'vue-client'
import * as Util from '../Util'
import ObjConfig from '../../utils/GerJsonObjectConfig'
import {jsonSort} from '../../utils/LdapHelper'
import Vue from "vue";
let asyncReady = async function (self) {
  // 获取配置信息
  try {
    let res = await Vue.resetget(`/api/af-safecheck/vue`, {data: {}}, {resolveMsg: null, rejectMsg: null})
    self.delConfigKey(res.data)
    Vue.config = res.data
  } catch (error) {
    // 忽略704,文件找不到异常R
    if (error.status !== 704) {
      throw error
    }
  }
}
export default {
  title: '安检信息',
  data () {
    return {
      imgs:[],
      normalDevices:{},//设备信息
      device:[],//表具信息
      devices:[],//所有表具信息
      userinfo:[],
      deviceInfo:{},//具体表具信息
      deviceInfos:{},//每个表具的所有信息
      drowspan:0,
      erowspan:0,//设备信息个数
      hrowspan:0,//隐患数
      f_defect_content:'', //安检结果
      equipmentmsgs:[],//所有设备信息
      Hinvestigations:[], //所有隐患信息
      Hinvestigation:{},
      defect:{},//隐患信息
      normal:{},
      loginorg:this.$login.f.orgs+'安检单',
      normalDevice:[],
      Hiddenchecks:[],
      hiddenvalues:[],
      HazardDevice:[],
      HazardDevices:{},
      printtime:'',
      print_id:'',
      showAdd:true,
      defectCount:'',
      deviceconfig:{},
      deviceconfig2:[],
      config: {
        "燃气表": {
          "checkmust": true,
          "index": 1,
          "items": {
            //"燃气表照片":{"index":1,"type":"picture","remark":"(拍摄清晰的表具编号、字轮数、剩余气量等)"},
            "基表编号": { "index": 1, "type": "string", "readonly": true },
            "气表型号": { "index": 2, "type": "string", "readonly": true },
            "气表品牌": { "index": 3, "type": "string", "readonly": true },
            "进气方向": { "index": 4, "type": "string", "readonly": true },
            // "初始表底":{"index":5,"type":"number","isshow":false,"readonly":true},
            "上期表读数": { "index": 5, "type": "number", "readonly": true, isshow: false },
            "户累购气量": { "index": 6, "type": "number", "readonly": true },
            "累购气量": { "index": 7, "type": "number", "readonly": true },
            "已用气量": { "index": 8, "type": "number", "checkmust": true },//基表数
            "剩余气量": { "index": 9, "type": "number", "checkmust": true },//累购-已用
            //显示屏数  --实际剩余气量
            "气量异常差值": { "index": 10, "type": "number", "readonly": true },
            "差值备注": { "index": 12, "type": "string", "readonly": false }
          }
          // 注意，配置项的index必须与上面的固定配置保持'顺序'
        }
      },
      //金额
      config2: {
        "燃气表": {
          "checkmust": true,
          "index": 1,
          "items": {
            //"燃气表照片":{"index":1,"type":"picture","remark":"(拍摄清晰的表具编号、字轮数、剩余气量等)"},
            "基表编号": { "index": 1, "type": "string", "readonly": true },
            "气表型号": { "index": 2, "type": "string", "readonly": true },
            "气表品牌": { "index": 3, "type": "string", "readonly": true },
            "进气方向": { "index": 4, "type": "string", "readonly": true },
            // "初始表底":{"index":5,"type":"number","isshow":false,"readonly":true},
            "上期表读数": { "index": 5, "type": "number", "readonly": true, isshow: false },
            "户累购金额": { "index": 6, "type": "number", "readonly": true },
            "累购金额": { "index": 7, "type": "number", "readonly": true },
            "已用金额": { "index": 8, "type": "number", "checkmust": true },
            //显示屏数  --实际剩余气量
            "剩余金额": { "index": 9, "type": "number", "checkmust": true },
            "金额异常差值": { "index": 10, "type": "number", "readonly": true },
            "差值备注": { "index": 12, "type": "string", "readonly": false }
          }
        }
      },
      isBack:false
    }
  },
  props: ['data','show'],
  ready () {
    console.log("登录信息",this.$login)
    asyncReady(this).then(() => {

      this.$resetpost(`/api/af-safecheck/logic/path_getCheckPaper`, { id: this.data.id }).then((res)=>{
        console.log('res',res.data)
        if(res.data.length>0){
          this.userinfo = res.data[0]
          console.log("88888888888888888")
          console.log(JSON.stringify(this.userinfo))
          if(this.userinfo.f_sign_path != ''){
            this.userinfo.f_sign_path = 'rs/image/file/' + this.userinfo.f_sign_path
          }
        }else{
          console.log('没找到')
          this.$showMessage('未查询到该用户安检单信息')
          this.$parent.showcheckmsg = false
        }
        console.log(Vue.config)
        if (this.userinfo.f_check_type=='民用'){
          this.deviceconfig=Vue.config.DevicesConfig
         // console.log("deviceconfig1==>"+JSON.stringify(this.deviceconfig))
        }else if (this.userinfo.f_check_type=='非民用'){
          this.deviceconfig=Vue.config.DevicesSetConfig
          //console.log("deviceconfig2==>"+JSON.stringify(this.deviceconfig))
        }
        if (this.userinfo.f_collection_type=='按气量'){
          this.deviceconfig["燃气表"]=this.config["燃气表"]
          //console.log("deviceconfig3==>"+JSON.stringify(this.deviceconfig))
        }else if (this.userinfo.f_collection_type=='按金额'){
          this.deviceconfig["燃气表"]=this.config2["燃气表"]
          //console.log("deviceconfig4==>"+JSON.stringify(this.deviceconfig))
        }
       // console.log("dddd1"+JSON.stringify(this.deviceconfig))
        this.deviceconfig2=ObjConfig.getObjConfig(this.deviceconfig)
        //console.log("dddd2"+JSON.stringify(this.deviceconfig))
        this.deviceconfig2[0].items=jsonSort(this.deviceconfig2[0].items,"index")
        //console.log("dddd3"+JSON.stringify(this.deviceconfig2))
        this.deviceconfig2[1].items=jsonSort(this.deviceconfig2[1].items,"index")
        //console.log("dddd4"+JSON.stringify(this.deviceconfig2))
        this.deviceconfig2=jsonSort(this.deviceconfig2,"index")
        //console.log("dddd5"+JSON.stringify(this.deviceconfig2))
        this.defectcontent()
      })

    }).catch(
      (respanse) => {
        alert('获取配置文件失败')
      })
      },
  methods: {
    delConfigKey(obj) {
      // 从琉璃中获取的配置中会包含 $开头的一些额外描述新的key。为了兼容v3的代码逻辑在此函数中去掉
      Object.keys(obj).forEach(key => {
        Object.keys(obj[key]).forEach(col => {
          // 检查键是否以$开头
          if (col.startsWith('$')) {
            // 使用delete操作符删除键
            delete obj[key][col];
          }
        })
      })
    },
    cancel(){
      this.$parent.showcheckmsg = false
    },
    getDeviceValue(val1,val2){

        for (let i = 0; i < this.userinfo.f_devices[0].f_items.length; i++) {
          if (this.userinfo.f_devices[0].f_items[i].f_item_name==val2){
            return this.userinfo.f_devices[0].f_items[i].f_item_lists[0].f_item_value
          }
        }
        for (let i = 0; i < this.userinfo.f_devices[1].f_items.length; i++) {
          if (this.userinfo.f_devices[1].f_items[i].f_item_name==val2){
            return this.userinfo.f_devices[1].f_items[i].f_item_lists[0].f_item_value
          }
        }
      for (let i = 0; i < this.userinfo.f_devices[1].f_items.length; i++) {
        if (this.userinfo.f_devices[2].f_items[i].f_item_name==val2){
          return this.userinfo.f_devices[2].f_items[i].f_item_lists[0].f_item_value
        }
      }


    },
    defectcontent(){
      let val1=this.userinfo.f_devices[1].f_items
      let val2=this.userinfo.f_devices[0].f_items

      for (let i = 0; i < val1.length; i++) {
      if (val1[i].f_item_lists[0].f_is_defect=='true'){
        this.defectCount='有隐患'
        return
      }
      }
      for (let i = 0; i < val2.length; i++) {
        if (val2[i].f_item_lists[0].f_is_defect=='true'){
          this.defectCount='有隐患'
          return
        }
      }
      this.defectCount='正常'
      return '正常'
    },
    //获取当前时间
    getNowFormatDate() {
      var date = new Date(this.userinfo.f_check_date);
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var currentdate = date.getFullYear() +'年'+ month + '月' + strDate
        + "日" + date.getHours() + "时" + date.getMinutes()+'分'
      return currentdate;
    },
    // getId(){
    //   console.log('返回值:')
    // },
    //获取隐患信息
    getHidden(){
      setTimeout(()=> {
        console.log('关闭打印提示框')
        this.$parent.showcheckmsg = false
      }, 1000)
    },
  }
}
</script>
<style media="screen" scoped>
  .divtext {
    min-width: 50px;
    max-width: 200px;
    border: 1px solid green;
    display: inline-block;
  }
  .bg-info{
    border-left: 5px solid #5989be;
  }
  .bg-cus-body {
    margin-left: 5px;
  }

</style>
