<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">&emsp;&emsp;公司</label>
              <right-tree @re-res="$parent.$parent.getorg"
                          :initresid='$parent.$parent.org'></right-tree>
            </div>
            <div   v-if="$parent.$parent.meter_classify == '卡表'" class="col-sm-2 flex-row" style="white-space: nowrap">
              <label class="font_normal_body">未购天数大于</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">  小于 </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">未抄天数大于</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"> 小于 </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">起始时间</label>
              <datepicker id="startDate" placeholder="起始时间"
                          v-model="model.startDate"
                          style="width:60%"
                          :value.sync="model.startDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="col-sm-2 ">
              <label class="font_normal_body">截止时间</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"
                            :footer="$parent.$parent.footer"
                            :header="$parent.$parent.other"
                            sqlurl="api/af-revenue/logic/openapi/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 form-group">
              <label class="font_normal_body">气表品牌</label>
              <v-select :value.sync="model.f_meter_brand"
                        v-model="model.f_meter_brand"
                        :options='$parent.$parent.meterbrands' placeholder='请选择'
                        condition="f_meter_brand = '{}'"
                        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">抄表次数</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">总购气量大于</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">  小于 </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">总用气量</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 class="col-sm-2 form-group">
              <label class="font_normal_body">客户类型</label>
              <v-select :value.sync="model.f_user_type"
                        @change="$parent.$parent.userTypeChange()"
                        :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.f_user_type"
                        condition="f_user_type = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">用气性质</label>
              <v-select :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                        :options='$parent.$parent.gasproperties' placeholder='请选择'
                        condition="f_gasproperties = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">气价类型</label>
              <v-select :value.sync="model.pricetype" v-model="model.pricetype"
                        placeholder='气价类型' :options="$parent.$parent.pricetypes"
                        close-on-select v-ref:type>
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">气价名称</label>
              <v-select :value.sync="model.f_price_name"
                        v-model="model.f_price_name"
                        :options="$parent.$parent.getPricenames"
                        condition="f_price_name = '{}'"
                        close-on-select></v-select>
            </div>

            <div class="col-sm-2 form-group">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;小区&nbsp;&nbsp;&nbsp;</label>
              <v-select :value.sync="model.f_residential_area"
                        class="select_list select"
                        v-model="model.f_residential_area"
                        style="width: 60%"
                        multiple
                        condition="f_residential_area in {}"
                        :options='$parent.$parent.residentialArea' placeholder='选择小区'
                        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">总购次数大于</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">  小于 </label>
                <input type="text" style="width:30%" class="input_search" v-model="model.cishumax"
                       condition="cishu <= {} " placeholder="">
              </div>
            </div>
            <div class="col-sm-2" >
              <label  for="f_user_state" class="font_normal_body">客户状态</label>
              <v-select :value.sync="model.f_user_state"
                        v-model="model.f_user_state"
                        :options='$parent.$parent.userstate' placeholder='请选择'
                        condition="f_user_state = '{}'"
                        close-on-select></v-select>
            </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><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_user_phone}}</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_residential_area}}</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;font-weight: bold">总购气量合计:&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;font-weight: bold">阶段购气量合计:&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;font-weight: bold">总抄表气量合计:&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;font-weight: bold">阶段抄表气量合计:&emsp;{{sumsmodel.sellpregas}}</td>-->
          </tr>
        </table>
  </div>
      <div v-if="show">
        <user-info-detail-manage-new :f_userinfo_id="rowdata.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage-new>
      </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
    await self.$MagLoadParams.loadParam()
    self.initParams()
  }
  export default {
    title: '用户稽查',
    data () {
      return {
        other:[],
        footer:[],
        org:[this.$login.f.orgid],
        criteriaShow: false,
        meter_classify:'卡表',
        gasproperties:[],
        prices: [],
        f_filialeid: this.$login.f.f_orgid,
        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_user_phone":"用户电话","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('api/af-revenue/sql/inspectList_weinan', 50,{endDate:'this.model.endDate',startDate:'this.model.startDate'},{pregas: 0}),
        rows: null,
        residentialArea: [],
        //orgid:'',
        meterbrands:[],
        show:false,
        sqlName:'inspectList_weinan',
        rowdata:{},
        sumsmodel: {},
        config: {
          defaultPrint: ['f_userinfo_code','f_user_id']
        },
        defaultfield: [],
      }
    },
    watch:{
      meter_classify(val){
        if(val == '卡表') {
          this.url = 'api/af-revenue/sql/inspectList_weinan'
          this.sqlName = 'inspectList_weinan'
          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 = 'api/af-revenue/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()
      },
      sumsmodel:{
        handler: function(val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    ready () {

      this.getaddress()
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      async  getaddress(){
        var data = await this.$resetpost('api/af-revenue/sql/manage_getarealist?pageNo=1&pageSize=99999', {
          data: {
            condition: `1=1 and s.f_orgid = '${this.$login.f.orgid}'`
          },
        }, {resolveMsg: null, rejectMsg: '获取小区失败!'})
        console.log('小区',data)
        let house = [{label: '全部', value: ''}]
        for (let row of data.data){
          house.push({label: row.f_residential_area, value: row.f_residential_area})
        }
        this.residentialArea = house
      },
      initParams() {
        // 初始化气表品牌
        let brandArr = []
        this.$MagGetSaleParam.getGasbrand().forEach((item) => {
          let temp = {}
          temp.label = item.label
          temp.value = item.value.f_meter_brand
          brandArr.push(temp)
        })
        this.meterbrands = [{label: '全部', value: ''}, ...brandArr]
        //初始化气表价格
        this.prices = this.$MagGetSaleParam.getPrices();
      },
        getotherfooter(){
          // this.$refs.paged.$refs.cri.$refs.exports.otherData=[];
          // this.$refs.paged.$refs.cri.$refs.exports.footerData=[];
          this.other=[];
          this.footer=[];
          let exportdata = this.getCondition;
          let otherInData=[];
          otherInData.push(`导出时间: ${this.$login.toStandardTimeString()}`);
          let footerData=[],exportfield=this.getfield;
          footerData.push("合计");
          let self = this;
          for(var field in self.sumsmodel){
            footerData.push(`${exportfield[field]}合计:${self.sumsmodel[field]}`);
          }
          this.footer.push(footerData);
          this.other.push(otherInData);
        },
      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
        }
      },

      userTypeChange () {
        this.gasproperties=[]
        if(this.$refs.paged.$refs.cri.model !==null) {
          this.$refs.paged.$refs.cri.model.f_gasproperties=''
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.cri.model.f_user_type[0])
        }
        else{
          this.gasproperties =[{label: '全部', value: ''}]
        }
      },

      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 === '卡表' ? 'api/af-revenue/sql/inspectList_weinan': 'api/af-revenue/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: {
      usertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      pricetypes() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气价类型')]
      },
      getPricenames() {
        let f_user_type = this.$refs.paged.$refs.cri.model.f_user_type;
        let f_gasproperties = this.$refs.paged.$refs.cri.model.f_gasproperties;
        let pricetype = this.$refs.paged.$refs.cri.model.pricetype;
        console.log("打印一下:",f_user_type,f_gasproperties,pricetype,this.$login.f.orgid,this.prices)
        if(this.$refs.paged.$refs.cri.model !==null) {
          let rs = []
          let priceArr = []
          if ( pricetype.length >0) {
            let params = {
              f_price_type: pricetype[0],
              filter: this.$login.f.orgid,
              prices: this.prices
            }
            this.$MagGetSaleParam.getPrice(params).forEach((item) => {
              let temp = {}
              temp.label = item.label
              temp.value = item.value.f_price_name
              priceArr.push(temp)
            })
            rs = [{label: '全部', value: ''}, ...priceArr]
          }
          if (rs.length === 0) {
            console.log('rs读出来是空')
            this.$refs.paged.$refs.cri.model.f_price_name = ''
          }
          return rs
        }
      },
      tablestate () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气表状态')]
      },
      f_meter_classify(){
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('表类型')]
        // return [{label: '卡表', value: '卡表'}]
      },
      userstate () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('客户状态')]
      },
      getCondition () {
        let result = {
          condition: '',
          diffDay:' 1=1 ',
          endDate: this.$refs.paged.$refs.cri.model.endDate,
          startDate: this.$refs.paged.$refs.cri.model.startDate,
        }
        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>

