<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex" v-if="!show">
        <criteria-paged :model="model" v-ref:paged  @sort="sort">
          <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 form-group">
                  <label for="startDate" class="font_normal_body">开始日期</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd 00:00:00'"
                              :show-reset-button="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body">结束日期</label>
                  <datepicker id="endDate" placeholder="结束日期" style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd 23:59:59'"
                              :show-reset-button="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp; </label>
                  <right-tree @re-res="$parent.$parent.getRes"></right-tree>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;部门&nbsp;&nbsp;&nbsp; </label>
                  <res-select restype='department'
                              @res-select="$parent.$parent.getdep"
                              :parentresid="$parent.$parent.depresid"
                              :initresid='$parent.$parent.depid'>
                  </res-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;人员&nbsp;&nbsp;&nbsp; </label>
                  <res-select restype='user'
                              is-mul="false"
                              @res-select="$parent.$parent.getuser"
                              :parentresid="$parent.$parent.userresid"
                              :initresid='$parent.$parent.operatorid'>
                  </res-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">气表类型</label>
                  <v-select :value.sync="model.f_meter_type" multiple
                            :options='$parent.$parent.metertypes' placeholder='请选择' v-model="model.f_meter_type"
                            condition="meterType in {}"
                            close-on-select></v-select>
                </div>
                <div class="span" style="float:right;">
                  <!--<read-idcard></read-idcard>-->
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <export-excel :data="$parent.$parent.getCondition" :footer="$parent.$parent.footer"
                                :field="$parent.$parent.getfield" :header="$parent.$parent.other"
                                :defaultselect="$parent.$parent.defaultexport"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="chargeQuery" template-name='收费查询导出'
                                :choose-col="true">
                  </export-excel>
                  <print-data :sum-field="$parent.$parent.getfield" :model="$parent.model" :field="$parent.$parent.getfield"
                              :defaultfield="$parent.$parent.defaultfield" print-name="收费查询"
                              :sumsmodel="$parent.$parent.sumsmodel"></print-data>
                </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>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">
                <nobr>{{row.f_price}}</nobr>
              </td>
              <td style="text-align: center;"><nobr>
                <nobr>{{row.f_gas}}</nobr>
              </nobr> </td>
              <td style="text-align: center;">
                <nobr>{{row.f_money}}</nobr>
              </td>
            </template>
            <template partial='foot'></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 style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              售气量合计:&emsp;{{sumsmodel.f_gas}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              金额合计:&emsp;{{sumsmodel.f_money}}
            </td>
          </tr>
        </table>
      </div>
    </div>
    <modal :show.sync="showupload" v-if="showupload" width="80%" style="width:auto;"  v-ref:modal middle backdrop="false">
      <article slot="modal-body">
        <upload :blodid="selected.f_userinfo_id" :businessid="selected.id" v-ref:upload isupload="false" takeimg="false"  fusetype="%收费" :isremark="false" style="width:auto" ></upload>
      </article>
      <footer slot="modal-footer" class="modal-footer">
      </footer>
    </modal>
  </div>
</template>




<script>
  import {HttpResetClass, PagedList} from 'vue-client'
  import exportConfig from '../config/exportConfig'


  let readySomething = async function (self) {
      self.$refs.paged.$refs.cri.model.startDate = self.$login.toStandardDateString() + ' 00:00:00'
      self.$refs.paged.$refs.cri.model.endDate = self.$login.toStandardDateString() + ' 23:59:59'
      self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }
  export default {
      title: '收费类型日售气量',
      props:['data'],
     data() {
      return {
          depresid: [],
          userresid: [],
          f_orgid: this.$login.f.orgid,
          f_depid: this.$login.f.depids,
          f_operatorid: this.$login.f.id,
          operatorid: [],
          depid: [],
          orgname: '',
          depname: [],
          operatorname: '',
          orgCondtionStr: '1=1',
          startDate:"",
          endDate:"",
        resshow:['company','department','operator','slicearea'],
        other:[],
        footer:[],
        orderitem:'f_operate_date desc',
        orderFields: {
          f_operate_date: 'no'
        },
        gasproperties:[],
        showinfo: false,
        data: {},
        show:false,
        rowdata:{},
          model: new PagedList('api/af-revenue/sql/priceTypeGasMoney', 20, {startDate: 'this.model.startDate', endDate: 'this.model.endDate', f_orgid: 'this.model.f_orgid'},{
              f_price: 0,
              f_gas: 0,
              f_money: 0,
          }),
        criteriaShow: false,
        // 下拉框
        meterbrands: [],
        pricenames: [],
        prices: [],
        area: [],
        //合计数据
        defaultfield: [],
          //合计数据
          sumsmodel: {},
          pageSum: {
              'f_gas': '0.00',
              'f_money': '0.00'
          },

        f_filialeid: this.$login.f.f_orgid,
        showupload:false,
        selected:{},
        inputtores:[{label: '全部',value: ''}],
        allorgid:[],
        tempfalg :false,
        lastorgstr:'',
      }
    },
    ready() {
        readySomething(this).then(() => {
            this.$emit('ready')
        }).catch((error) => {
            this.$emit('error', error)
        })

    },
    methods: {
      getotherfooter () {
          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);
      },
      search(){

        this.$refs.paged.$refs.cri.search()
      },

      selfSearch(args) { // 调用sql文件
          let f_dep=this.f_depid
          let orgcondition = ' s.f_orgid = ' + this.f_orgid
          if (f_dep[0]) {
              orgcondition += " and s.f_depid in('"+f_dep[0]+"'"
              for( let i=1;i<f_dep.length;i++){
                  orgcondition += ",'"+f_dep[i]+"'"
              }
              orgcondition +=')'
          }
          if(this.f_operatorid[0]) {
              orgcondition += ' and s.f_operatorid = ' + this.f_operatorid
          }
          this.$refs.paged.$refs.cri.model.f_orgid = orgcondition
          console.log("condition:" + args.condition)

          this.startDate=this.$refs.paged.$refs.cri.model.startDate.substring(0,10)
          this.endDate=this.$refs.paged.$refs.cri.model.endDate.substring(0,10)
              this.model.search(args.f_orgid, args.model, args.condition)
          this.sumsmodel = this.$refs.paged.$refs.grid.model.sums

      },
        getRes (obj) {
            this.orgname = obj.res[0]
            this.depresid = obj.resids
            this.f_orgid = obj.resids
        },
        getdep (obj, val) {
            this.depname = val
            this.userresid = obj
            this.f_depid = obj
        },
        getuser ( obj, val) {
            this.operatorname = val[0]
            this.f_operatorid = obj
        },
      dealmsg(val,startDate,endDate) {
        val.model = this.model.model
        startDate=this.startDate1
        endDate=this.endDate1
        this.$dispatch('deal-msg', val,startDate,endDate)
      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      async getRes(condition,obj) {
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]


      },
      async updateParams() {
        await this.$MagLoadParams.loadParam(this.f_filialeid)
        this.inputtores = [];
        this.inputtores.push({label: "全部", value: ""})
        if(this.allorgid!=null){
          this.allorgid.forEach((res)=>{
            this.inputtores = this.inputtores.concat(this.$MagGetSaleParam.getresinputtor(res));
          })
        }
        this.initParams()
      },
      sort (field, rule) {
        // 将所有排序方式设为不排序，实现相互排斥
        for (let key in this.orderFields) {
          if (key === field) {
            this.orderFields[key] = rule
          } else {
            this.orderFields[key] = 'no'
          }
        }
        // 如果新规则不排序，还原为默认排序
        if (rule === 'no') {
          this.model.paramSource.orderitem = `'${this.orderitem}'`
        } else {
          this.model.paramSource.orderitem = `'${field} ${rule}'`
        }

        this.search()
      }

    },
    events:{
      'getidcard'(IdCard){
        // console.log(param)
        this.$refs.paged.$refs.cri.model.f_info_idnumber=IdCard.strID
      }
    },
    watch: {
      'model.rows'(val) {
        if(val.length === 0){
          this.sumsmodel = {}
        }
      },
      async 'orgCondtionStr'(val) {
        //更新抄表册
        this.meterbooks = [{label: '全部',value: ''}];
        if(this.lastorgstr != val){
          this.tempfalg = true;
          this.lastorgstr = val;
        }
        if(this.tempfalg && val != null && val!=''  && val.indexOf("(")>-1){
          this.tempfalg = false;
          let a = val.substring(val.indexOf("(")+2)
          await this.initmeterbook(a.substring(0,a.indexOf(")")-1));
        }
        let res = val.match(/'(.*?)'/)
        console.log('正则提取:',res && res[1])
        if (res) {
          this.f_filialeid = res[1]
          if(res.input !=null){
            let resorgid = res.input.substring(res.input.indexOf("'"));
            if(resorgid!= null && resorgid!=''){
              resorgid = resorgid.substring(0,resorgid.lastIndexOf("'")+1);
             // resorgid = resorgid.replaceAll("'","");
              resorgid = resorgid.replace(/'/g,"");
              this.allorgid = resorgid.split(",")
            }
          }
          this.updateParams()
        }
      },
      sumsmodel:{
        handler: function(val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    computed: {

         chooserow(){
             let aaa= true
             if(this.$login.r && this.$login.r.includes('打印项选择')){
                 aaa =  false
             }
             return aaa
         },
      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.f_filialeid,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.f_filialeid,
              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
        }
      },
      getCondition() {
        return {
          startDate: this.$refs.paged.$refs.cri.model.startDate,
          endDate: this.$refs.paged.$refs.cri.model.endDate,
          condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
          orderitem: this.orderitem?`${this.orderitem}`: " f_operate_date desc"
        }
      },
      getfield() {
        return exportConfig.pricetypeGasConfig
      },
      defaultexport() {
          return exportConfig.priceGasexportConfig
      },

      metertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('气表类型')]
      }
    }
  }
</script>
