<template>
  <!--安检综合查询主界面-->
  <div id="unit" class="flex-row" :class="{'binary':showItem || showItemUser || showFdItemUser}">
    <div :class="{'basic-main':!(showItem || showItemUser ||showFdItemUser ),'binary-left':showItem || showItemUser|| showFdItemUser}">
      <!--展示查询界面-->
      <check-book-list class="flex" :tempval="hidden" :style="style" :style2="style2" :showtype="showtype"  @select-changed="selected"
                         @canel-main="close"
                         @addbook="addbook"
                         :needprops="needProps"
                         @showdetails="showdetails"
                         @showfddetails="showfddetails"
                       v-ref:checkbook></check-book-list>
    </div>
    <div class="binary-right" v-if="showItemUser || showItem || showFdItemUser">
      <div class="flex">
        <check-book-user style="height: 100%" :row="rowData" v-if="showItemUser && rowData.f_check_book_type==='用户'"></check-book-user>
        <check-book-area style="height: 100%"    @showuser="showuser"  :row="rowData" :needprops="needProps" v-if="showItemUser && rowData.f_user_type==='民用'"></check-book-area>
        <check-book-company style="height: 100%"    @showuser="showusercompany"  :row="rowData" :needprops="needProps" v-if="showItemUser && rowData.f_user_type==='非民用' && rowData.f_check_book_type!='非民未下发' "></check-book-company>
        <check-book-company-user style="height: 100%"  :checkbookid="checkbookid" v-if="showFdItemUser"></check-book-company-user>
        <check-book-details style="height: 100%" :row="rowData" :showtype="showtype" v-if="showItem"></check-book-details>
      </div>
    </div>
  </div>

</template>

<script>
import {HttpResetClass} from "vue-client";

export default {
  title: '片区管理',
  name: 'CheckPaperSearchUser',
  props:['showtype'],
  data () {
    return {
      style: 'col-sm-2 form-group',
      style2: 'col-sm-4 form-group',
      showItemUser: false,
      showFdItemUser:false,
      hidden: true,
      barwidth: '100%',
      showItem: false,
      rowData: {},
      checkbookid:'',
      needProps:{
        meter_gasbrand:[],
        checkBooks:[],
        entry_status:[],
        userTypes:[],
        userStates:[],
        gasproperties:[],
        areaList:[],
        sliceArea:[],
        cbc:[],
        meterbooks:[],
        checker:[],
      },
    }
  },
  ready(){
    this.init()
  },
  methods: {
    init(){
      this.getCheckBook()
      this.setConList()
      this.loadpage()
      this.getGasbrand()
      this.searchChecker()
    },
    //查询安检员selectChanged
    async searchChecker(){
      let res=await new HttpResetClass().load('POST', '/rs/search', {
        source: 'tool.getChildrenOfResName($安检员$)',
        userid: this.$login.f.id
      }, {resolveMsg: null, rejectMsg: null})
      this.needProps.checker=[]
      res.data.forEach((resRow) => {
        if(resRow.orgid==this.$login.f.orgid){
          this.needProps.checker.push({label:resRow.name,value:resRow})
        }
      })
    },
    getGasbrand(){
      this.needProps.meter_gasbrand=[]
      new HttpResetClass().load('POST', `/rs/sql/safe_singleTable_GroupBy`, {
        data: {
          items: 'id,f_meter_brand',
          tablename: 't_gasbrand',
          condition: `f_orgid = '${this.$login.f.orgid}'`,
          groupitem: 'id,f_meter_brand'
        }
      }, {resolveMsg: null, rejectMsg: null}).then(res => {
        this.needProps.meter_gasbrand.push({label: '全部', value: ''})
        //console.log('res=', JSON.stringify(res))
        res.data.forEach(ress => {
          //console.log('ress=', JSON.stringify(ress))
          this.needProps.meter_gasbrand.push({
            label: ress.f_meter_brand,
            value: ress.id
          })
        })
      })
    },
    getCheckBook(){
      this.needProps.checkBooks=[]
      new HttpResetClass().load('POST', `/rs/sql/safe_singleTable_OrderBy`, {
        data: {
          items: 'id,f_check_book_name,f_check_book_type',
          tablename: 't_check_book',
          condition: `f_orgid = '${this.$login.f.orgid}'`,
          orderitem: 'id desc'
        }
      }, {resolveMsg: null, rejectMsg: null}).then(res => {
        this.needProps.checkBooks.push({label: '全部', value: ''})
        this.needProps.checkBooks.push({label: '未在片区内', value: '111111'})
        res.data.forEach(ress => {
          this.needProps.checkBooks.push({
            label: ress.f_check_book_name,
            value: `${ress.id}-${ress.f_check_book_type}`
          })
        })
      })
    },
    loadpage(){
      this.select_search()
      this.Mreadibook()
      this.getSliceArea()
    },
    select_search(text, type){
      //tag
      let pcdText=text?`   and f_residential_area like '%${text}%'`:''
      if (type == 'pqId'){
        if(text.length==0){
          pcdText=text?`   and 1=1`:''
        }else {
          pcdText =text?`and f_check_book_id in (${text})`:''
        }

      }
      new HttpResetClass().load("POST", `${this.$androidUtil.getProxyUrl()}/rs/sql/safe_singleTable_OrderBy`, {data:{items:"*",tablename:"t_area",orderitem:"id",condition:`f_orgid = '${this.$login.f.orgid}' ${pcdText} `}}, {resolveMsg: null, rejectMsg: null}).then((res)=>{
        //tag)
        this.needProps.areaList=[]
        this.needProps.areaList.push({label:"全部",value:""})
        res.data.forEach((result)=>{
          this.needProps.areaList.push({label:result.f_residential_area,value:result.f_residential_area})
          //console.log('this.areaList=', JSON.stringify(this.areaList))
        })
        //tag)
      })


    },
    //获取抄表册
    async Mreadibook(){
      this.needProps.cbc=[]
      this.needProps.sliceArea=[]
      let http = new HttpResetClass()
      let getMeterReadingBooks = await http.load('POST','rs/logic/getMeterBooksData',
        {data: {f_orgid: "('"+this.$login.f.orgid+"')"}},
        {resolveMsg: null, rejectMsg: '获取失败！！！'})
      this.needProps.cbc = [{label: '全部', value: ''}]
      //tag
      //tag)
      //tag
      this.needProps.meterbooks=[]
      this.needProps.meterbooks=getMeterReadingBooks.data.meterbooks
      this.needProps.meterbooks.forEach((res) => {
        this.needProps.cbc.push({label: res.f_book_name, value: res.id})
        /*if(res.f_book_slice_area){
          if(this.sliceArea.indexOf(res.f_book_slice_area)<0){
            this.sliceArea.push(res.f_book_slice_area)
          }
        }*/
      })

    },
    //片区获取
    async getSliceArea()  {
      let http = new HttpResetClass()
      let getAllArea = await http.load('POST', '/rs/search', {
        source: 'this.getParentByType($organization$).getAllChildrens().where(row.getType() == $zone$)',
        userid: this.$login.f.id
      }, {resolveMsg: null, rejectMsg: '获取片区出错！！！'})
      this.needProps.sliceArea = [{label: '全部', value: ''}]
      //tag
      //tag
      //tag
      getAllArea.data.forEach((res) => {
        if(res.parentid==this.$login.f.orgid){
          this.needProps.sliceArea.push({label:res.name,value:res.name})
        }
      })
      //tag
    },
    setConList(){
      this.needProps.entry_status=this.$appdata.getParam('安检状态') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('安检状态')] : [{label: '全部', value: ''}]

      this.needProps.userTypes=this.$appdata.getParam('用户类型') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('用户类型')] : [{label: '全部', value: ''}]
      this.needProps.userStates=this.$appdata.getParam('安检用户状态') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('安检用户状态')] : [{label: '全部', value: ''}]
      this.needProps.gasproperties=this.$appdata.getParam('用气性质') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('用气性质')] : [{label: '全部', value: ''}]
      // this.meter_classifys=this.$appdata.getParam('气表类型') ? [{
      //   label: '全部',
      //   value: ''
      // }, ...this.$appdata.getParam('气表类型')] : [{label: '全部', value: ''}]
    },
    show () {
      this.showItem = this.showItem ? false : true
    },
    showfddetails(row){
      this.checkbookid = row
      this.showItem = false
      this.showItemUser = false
      this.showFdItemUser = true
    },
    showdetails (row) {
      console.log('showdetails',row)
      this.rowData = row
      this.showItem = false
      this.showItemUser = true
      this.showFdItemUser = false
    },
    showusercompany(row){
      console.log('showuser',row)
      this.rowData = row
      this.showItem = false
      this.rowData.f_check_book_type='用户'
      this.rowData.f_check_book_type1='单位'
      this.showItemUser = true
      this.showFdItemUser = false
    },
    showuser(row){
      console.log('showuser',row)
      this.rowData = row
      this.showItem = false
      this.rowData.f_check_book_type='用户'
      this.rowData.f_check_book_type1='小区'
      this.showItemUser = true
      this.showFdItemUser = false
    },
    addbook(){
      this.rowData= {
        f_is_auto:'否',
        f_check_book_type:"用户",
      }
      this.showItem = true
    },
    selected (row) {
      this.showItem = true
      this.showFdItemUser = false
      this.barwidth = '50%'
      //tag
      this.rowData = row.val
      this.hidden = false
      //tag
    },

    close () {
      this.showItem = false
      this.showFdItemUser = false
      this.barwidth = '100%'
      this.hidden = true
    }
  },
  computed:{
    showItemAll(){
      return this.showItem && this.showItemUser
    }
  },
  watch: {
    'showFdItemUser'(val){
      if(val){
        this.showItem = false
        this.showItemUser = false
      }
      if (val || this.showItem || this.showItemUser) {
        this.style = 'col-sm-3 form-group'
        this.style2 = 'col-sm-6 form-group'

      } else {
        this.style = 'col-sm-2 form-group'
        this.style2 = 'col-sm-4 form-group'
      }
    },
    'showItemUser' (val) {
      //tag
      console.log("showItemUser",val)
      if(val){
        this.showItem = false
        this.showFdItemUser = false
      }
      if (val || this.showItem || this.showFdItemUser) {
        this.style = 'col-sm-3 form-group'
        this.style2 = 'col-sm-6 form-group'

      } else {
        this.style = 'col-sm-2 form-group'
        this.style2 = 'col-sm-4 form-group'
      }
    },
    'showItem' (val) {
      console.log("showItem",val)
      if(val){
        this.showItemUser = false
        this.showFdItemUser = false
      }
      if (val || this.showItemUser || this.showFdItemUser ) {
        this.style = 'col-sm-3 form-group'
        this.style2 = 'col-sm-6 form-group'

      } else {
        this.style = 'col-sm-2 form-group'
        this.style2 = 'col-sm-4 form-group'
      }
    }
  }
}

</script>

