<template>
  <div id="unit" class="flex-row">
    <div class="basic-main">
      <div class="flex" v-if="!show">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial >
          <div class="row">
            <div class="col-sm-2">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp;</label>
              <right-tree @re-res="$parent.$parent.getorg"
                          :initresid='$parent.$parent.org'></right-tree>
            </div>
            <div class="col-sm-2 ">
              <label for="f_gasproperties"class="font_normal_body">&nbsp;&nbsp;&nbsp;用气性质</label>
              <v-select id="f_gasproperties"
                        style="width:60%"
                        v-model="model.f_gasproperties"
                        placeholder='用气性质'
                        :value.sync="model.f_gasproperties"
                        :options='$parent.$parent.gasproperties'
                        condition="f_gasproperties = '{}'"
                        close-on-select >
              </v-select>
            </div>
            <div   v-if="$parent.$parent.meter_classify == '卡表'" class="col-sm-2 flex-row" style="white-space: nowrap">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;未购天数&nbsp;</label>
              <div class="row">
                <input type="text" style="width:30%" class="input_search" v-model="model.diffdaymin"
                       condition="diffday >= {} " placeholder="">
                <label   class="font_normal_body">  ≤ x ≤ </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.diffdaymax"
                       condition="diffday <= {} " placeholder="">
              </div>

            </div>
            <div v-if="$parent.$parent.meter_classify == '机表'" class="col-sm-2 flex-row" style="white-space: nowrap">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;未抄天数&nbsp;</label>
              <div class="row">
                <input type="text" style="width:30%" class="input_search" v-model="model.handdiffdaymin"
                       condition="diffday >= {} " placeholder="">
                <label   class="font_normal_body">  ≤ x ≤ </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.handdiffdaymax"
                       condition="diffday <= {} " placeholder="">
              </div>

            </div>
            <div class="col-sm-2 ">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;截止时间</label>
              <datepicker id="endDate" placeholder="截止时间"
                          v-model="model.endDate"
                          style="width:60%"
                          :value.sync="model.endDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="span" style="float:right;">
              <button class="btn button_search" @click="search()">查询</button>
              <button class="btn button_clear" @click="$parent.$parent.clear()">清空</button>
              <export-excel :data="$parent.$parent.getCondition"
                            :field="$parent.$parent.getfield"
                            sqlurl="rs/logic/exportfile"  :sql-name="$parent.$parent.sqlName" template-name='用户稽查导出'  :choose-col="true"></export-excel>
              <print-data :model="$parent.model" :field="$parent.$parent.getfield" :defaultfield="$parent.$parent.defaultfield" print-name="稽查查询打印" :sumsmodel="$parent.$parent.sumsmodel" ></print-data>
              <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()"></div>

            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="col-sm-2 ">
              <label for="f_meter_classify"class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;表类型</label>
              <v-select id="f_meter_classify"
                        v-model="$parent.$parent.meter_classify"
                        placeholder='表类型'
                        style="width:70%"
                        :value.sync="$parent.$parent.meter_classify"
                        :options='$parent.$parent.f_meter_classify'
                        condition="f_meter_type like '%{}'"
                        :value-single="true"
                        close-on-select >
              </v-select>
            </div>

            <div class="col-sm-2 ">
              <label for="f_table_state"class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;表状态</label>
              <v-select id="f_table_state"
                        style="width:60%"
                        v-model="model.f_table_state"
                        placeholder='表状态'
                        :value.sync="model.f_table_state"
                        :options='$parent.$parent.tablestate'
                        condition="f_table_state = '{}'"
                        close-on-select >
              </v-select>
            </div>
            <div v-if="$parent.$parent.meter_classify == '卡表'" class="col-sm-2 flex-row" style="white-space: nowrap" >
              <label   class="font_normal_body">&nbsp;&nbsp;&nbsp;总购次数&nbsp;</label>
              <div class="row">
                <input type="text" style="width:30%" class="input_search" v-model="model.cishumin"
                       condition="cishu >= {} " placeholder="">
                <label   class="font_normal_body">  ≤ x ≤ </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.cishumax"
                       condition="cishu <= {} " placeholder="">
              </div>
            </div>
            <div v-if="$parent.$parent.meter_classify == '机表'" class="col-sm-2 flex-row" style="white-space: nowrap" >
              <label  class="font_normal_body">&nbsp;&nbsp;&nbsp;抄表次数&nbsp;</label>

              <div class="row">
                <input type="text" style="width:30%" class="input_search" v-model="model.handmin"
                       condition="cishu >= {} " placeholder="">
                <label   class="font_normal_body">  ≤ x ≤ </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.handmax"
                       condition="cishu <= {} " placeholder="">
              </div>

            </div>
            <div v-if="$parent.$parent.meter_classify == '卡表'" class="col-sm-2 flex-row" style="white-space: nowrap" >
              <label  class="font_normal_body">&nbsp;&nbsp;&nbsp;总购气量&nbsp;</label>

              <div class="row">
                <input type="text" style="width:30%" class="input_search" v-model="model.pregasmin"
                       condition="pregas >= {} " placeholder="">
                <label   class="font_normal_body">  ≤ x ≤ </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.pregasmax"
                       condition="pregas <= {} " placeholder="">
              </div>


            </div>
            <div v-if="$parent.$parent.meter_classify == '机表'" class="col-sm-2 flex-row" style="white-space: nowrap" >
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;总用气量&nbsp;</label>
              <div class="row">
                <input type="text" style="width:30%" class="input_search" v-model="model.pregasusemin"
                       condition="pregas >= {} " placeholder="">
                <label   class="font_normal_body">  ≤ x ≤ </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.pregasusemax"
                       condition="pregas <= {} " placeholder="">
              </div>
            </div>


          </div>

        </div>
      </criteria>
      <data-grid :model="model" partial='list' class="list_area table_sy"  v-ref:grid>
        <template partial='head'>
          <tr>
            <th><nobr>序号</nobr></th>
            <th><nobr>客户编号</nobr></th>
            <th><nobr>表号</nobr></th>
            <th><nobr>客户名称</nobr></th>
            <th><nobr>地址</nobr></th>
            <th><nobr>用户类型</nobr></th>
            <th><nobr>用气性质</nobr></th>
            <th><nobr>表具状态</nobr></th>
            <th><nobr>气表品牌</nobr></th>
            <th><nobr>气价名称</nobr></th>
            <th><nobr>最后购气日期</nobr></th>
            <th v-show="$parent.$parent.$parent.meter_classify == '卡表'"><nobr>总购气次数</nobr></th>
            <th v-show="$parent.$parent.$parent.meter_classify == '机表'"><nobr>总抄表次数</nobr></th>

            <th v-show="$parent.$parent.$parent.meter_classify == '卡表'"><nobr>总购气量</nobr></th>
            <th v-show="$parent.$parent.$parent.meter_classify == '机表'"><nobr>总用气量</nobr></th>

            <th v-show="$parent.$parent.$parent.meter_classify == '卡表'"><nobr>累计未购气天数</nobr></th>
            <th v-show="$parent.$parent.$parent.meter_classify == '机表'"><nobr>累计未抄表天数</nobr></th>
            <th><nobr>组织机构</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center;"><nobr>{{$index+1}}</nobr></td>
          <td style="text-align: center;"><nobr>
            <span @click="$parent.$parent.$parent.showmsg(row)"><a>{{row.f_userinfo_code}}</a></span>
          </nobr> </td>
          <td style="text-align: center;"><nobr>{{row.f_meternumber}}</nobr></td>
          <th style="text-align: center;"><nobr>{{row.f_user_name}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_address}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_user_type}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_gasproperties}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_table_state}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_meter_brand}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_price_name}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.delivery_date}}</nobr></th>

          <th style="text-align: center;"><nobr>{{row.cishu}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.pregas}}</nobr></th>
<!--          <th style="text-align: center;"><nobr>{{row.sellcishu}}</nobr></th>-->
<!--          <th style="text-align: center;"><nobr>{{row.sellpregas}}</nobr></th>-->
          <th style="text-align: center;"><nobr>{{row.diffday}}</nobr></th>
          <th style="text-align: center;"><nobr>{{row.f_orgname}}</nobr></th>
        </template>
      </data-grid>
    </criteria-paged>
        <table class="table-hover">
          <tr style="position: relative" class="table-bordered">
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;color: #5CB95C;font-weight: bold">汇总信息</td>
            <td v-if="meter_classify == '卡表'" style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">总购气量合计:&emsp;&emsp;{{sumsmodel.pregas}}</td>
            <!--      <td v-if="meter_classify == '卡表'" style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">阶段购气量合计:&emsp;&emsp;{{sumsmodel.sellpregas}}</td>-->
            <td v-if="meter_classify == '机表'" style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">总抄表气量合计:&emsp;&emsp;{{sumsmodel.pregas}}</td>
            <!--      <td v-if="meter_classify == '机表'" style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">阶段抄表气量合计:&emsp;{{sumsmodel.sellpregas}}</td>-->
          </tr>
        </table>
  </div>
      <div v-if="show">
        <user-info-detail-manage :f_userinfo_id="rowdata.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage>
      </div>
    </div></div>
</template>
<script>
  /**
   *用户档案查询列表以及添加操作组件
   */
  import { PagedList } from 'vue-client'
  import Vue from 'vue'
  import plugin from 'system-clients/src/plugins/GetLoginInfoService'

  let readySomething = async function (self) {

    self.$refs.paged.$refs.cri.model.endDate =  self.$login.toStandardDateString()

    // await self.$refs.paged.$refs.cri.search()
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }
  export default {
    title: '用户稽查',
    data () {
      return {
        org:[this.$login.f.orgid],
        criteriaShow: false,
        meter_classify:'卡表',
        filialeNameStr: null,
        filialeCodeStr: `and f_orgid = ${this.$login.f.orgid}`,
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
        getfield:{"f_userinfo_code":"客户编号","f_meternumber":"表号","f_user_name":"客户名称","f_address":"地址","f_user_type":"用户类型",
          "f_gasproperties":"用气性质","f_table_state":"表具状态","f_meter_brand":"气表品牌","f_price_name":"气价名称",
          "delivery_date":"最后购气日期","cishu":"总购气次数","pregas":"总购气量",
          "diffday":"未购气总天数"},
        model: new PagedList('rs/sql/inspectList', 50,{endDate:'this.model.endDate'},{pregas: 0}),
        rows: null,
        //orgid:'',
        show:false,
        sqlName:'inspectList',
        rowdata:{},
        sumsmodel: {},
        config: {
          defaultPrint: ['f_userinfo_code','f_user_id']
        },
        defaultfield: [],
      }
    },
    watch:{
      meter_classify(val){
        if(val == '卡表') {
          this.url = 'rs/sql/inspectList'
          this.sqlName = 'inspectList'
          this.getfield = {"f_userinfo_code":"客户编号","f_meternumber":"表号","f_user_name":"客户名称","f_address":"地址","f_user_type":"用户类型",
            "f_gasproperties":"用气性质","f_table_state":"表具状态","f_meter_brand":"气表品牌","f_price_name":"气价名称",
            "delivery_date":"最后购气日期","cishu":"总购气次数","pregas":"总购气量",
            "diffday":"累计未购气天数"}
        }else{
          this.url = 'rs/sql/handinspectList'
          this.sqlName = 'handinspectList'
          this.getfield = {"f_userinfo_code":"客户编号","f_meternumber":"表号","f_user_name":"客户名称","f_address":"地址","f_user_type":"用户类型",
            "f_gasproperties":"用气性质","f_table_state":"表具状态","f_meter_brand":"气表品牌","f_price_name":"气价名称",
            "delivery_date":"最后抄表日期","cishu":"总抄表次数","pregas":"抄表气量",
            "diffday":"累计未抄表天数"}
        }
        this.clear()
      }
    },
    ready () {

      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      getorg (obj) {
        if(obj.resids.length>0){
          this.filialeCodeStr =" and f_orgid in " + plugin.convertToIn(obj.resids)
        }
        else{
          this.filialeCodeStr= " and f_orgid = " + this.$login.f.orgid
        }
      },

      cancel() {
        this.show = false
      },
      showmsg(obj){
        this.rowdata=obj
        this.show=true
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch (args) {
        this.model.url = this.meter_classify === '卡表' ? 'rs/sql/inspectList': 'rs/sql/handinspectList'
        args.condition = args.condition+ ` ${this.filialeCodeStr}`
        // console.log("拼接的condition", args.condition, this.model)
        this.model.search(args.condition, args.model)
      },
      hidden(){
        this.criteriaShow = !this.criteriaShow
      },
      clear(){
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          if(key != 'startDate' && key != 'endDate')
            this.$refs.paged.$refs.cri.model[key] = []
        })
        this.$refs.paged.$refs.cri.model.endDate =  this.$login.toStandardDateString()
      }
    },
    computed: {
      gasproperties() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用气性质')]
      },
      tablestate () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气表状态')]
      },
      f_meter_classify(){
        return [{label: '卡表', value: '卡表'},{label: '机表', value: '机表'}, ]
        // return [{label: '卡表', value: '卡表'}]
      },

      getCondition () {
        let result = {
          condition: '',
          diffDay:' 1=1 ',
          endDate: this.$refs.paged.$refs.cri.model.endDate,
        }
        result.condition = this.$refs.paged.$refs.cri.condition + this.filialeCodeStr

        return result
      }
    }
  }
</script>
<style media="screen">
  .divtext {
    min-width: 50px;
    max-width: 200px;
    border: 1px solid green;
    display: inline-block;
  }
</style>

