<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex">
        <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 class="font_normal_body">模糊查询</label>
                  <input type="text" class="input_search" style="width:60%"  v-model="model.searchQuery" placeholder='编号 名称 地址 电话 表号 备用电话 租户电话'
                         condition="(f_userinfo_code like '%{}%' OR f_user_name like '%{}%' OR f_user_phone like '%{}%' OR f_address like '%{}%' OR f_meternumber like '%{}%' OR f_rent_phone like '%{}%' OR f_zuhu_phone like '%{}%')">
                </div>
                <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 HH:mm:ss'"
                              :show-reset-button="true"
                              condition="f_operate_date >= '{}'">
                  </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 HH:mm:ss'"
                              :show-reset-button="true"
                              condition="f_operate_date <= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用户编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                         condition="f_userinfo_code = '{}' " placeholder="用户编号">
                </div>

                <div class="col-sm-2 form-group">
                  <label class="font_normal_body" title="旧客户编号">档案编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_olduserinfo_code"
                         condition="f_olduserinfo_code = '{}' " placeholder="档案编号">
                </div>

                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>

                  <export-excel :data="$parent.$parent.getCondition"
                                :field="$parent.$parent.getfield"
                                :header="$parent.$parent.other"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="pricechangeRecordQuery" template-name='调价记录导出'
                                :choose-col="true"
                                v-if="this.$login.r.includes('调价记录导出')"
                  ></export-excel>
                  <print-data :sum-field="$parent.$parent.getfield" :model="$parent.model" :field="$parent.$parent.getfield"
                              :defaultfield="$parent.$parent.defaultfield" titletable="调价记录"
                              :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">
                <res-select-group :initres="$parent.$parent.initres" @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户类型</label>
                  <v-select :value.sync="model.f_user_type"
                            :options='$parent.$parent.usertypes'
                            @change="$parent.$parent.userTypeChange()"
                            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>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_comments"
                         condition="f_comments like '%{}%' " placeholder="调价原因">
                </div>

                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">气表分类</label>
                  <v-select :value.sync="model.f_meter_classify"
                            v-model="model.f_meter_classify"
                            :options='$parent.$parent.metertypes' placeholder='请选择'
                            condition="f_meter_classify = '{}'"
                            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_meter_brand"
                            v-model="model.f_meter_brand"
                            :options='$parent.$parent.meterbrands' placeholder='请选择'
                            condition="f_meter_brand = '{}'"
                            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_meter_style"
                            v-model="model.f_meter_style"
                            :options='$parent.$parent.meterstyle[model.f_meter_brand]' placeholder='请选择'
                            condition="f_meter_style = '{}'"
                            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>-->
                  <data-order field="f_userinfo_code" name="客户编号" :order.sync="$parent.$parent.$parent.orderFields.f_userinfo_code"></data-order>
                </th>
                <th>
                  <nobr>客户姓名</nobr>
                </th>
                <th>
                  <nobr>档案编号</nobr>
                </th>
                <th>
                  <!--<nobr>客户地址</nobr>-->
                  <data-order field="f_address" name="客户地址" :order.sync="$parent.$parent.$parent.orderFields.f_address"></data-order>
                </th>
                <th>
                  <nobr>表号</nobr>
                </th>
                <th>
                  <nobr>用户类型</nobr>
                </th>
                <th>
                  <nobr>用气性质</nobr>
                </th>
                <th>
                  <nobr>原价格</nobr>
                </th>
                <th>
                  <nobr>新价格</nobr>
                </th>
                <th>
                  <!--<nobr>原价执行日期</nobr>-->
                  <data-order field="oldprice_performdate" name="原价执行日期" :order.sync="$parent.$parent.$parent.orderFields.oldprice_performdate"></data-order>
                </th>
                <th>
                  <!--<nobr>新价执行日期</nobr>-->
                  <data-order field="newprice_performdate" name="新价执行日期" :order.sync="$parent.$parent.$parent.orderFields.newprice_performdate"></data-order>
                </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>-->
                  <data-order field="f_operate_date" name="操作日期" :order.sync="$parent.$parent.$parent.orderFields.f_operate_date"></data-order>
                </th>
                <th><nobr>附件</nobr></th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">
                <nobr>{{row.f_userinfo_code}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_user_name}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_olduserinfo_code}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_address}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_meternumber}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_user_type}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_gasproperties}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_oldprice}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_newprice}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.oldprice_performdate}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.newprice_performdate}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_slice_area}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_comments}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_meter_classify}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_meter_brand}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_meter_style}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_operator}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_depname}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_operate_date}}</nobr>
              </td>

              <td style="text-align: center;"><nobr>
                <button class="button_search button_spacing width-60" @click.stop="$parent.$parent.$parent.view(row)">查看</button>
              </nobr></td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </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" v-ref:upload isupload="false" takeimg="false"  fusetype="批量调价" :isremark="false" style="width:auto" ></upload>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>

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

  let readySomething = async function (self) {
    self.$refs.paged.$refs.cri.model.startDate = self.data ? self.data.startDate : self.$login.toStandardDateString() + ' 00:00:00'
    self.$refs.paged.$refs.cri.model.endDate = self.data ? self.data.endDate : self.$login.toStandardDateString() + ' 23:59:59'
    // self.$refs.paged.$refs.cri.model.operateDate = self.$login.toStandardDateString() + ' 00:00:00'
    // self.$refs.paged.$refs.cri.model.performDate = self.$login.toStandardDateString() + ' 23:59:59'
    await self.$MagLoadParams.loadParam()
    self.defaultfield = [...self.defaultfield, ...self.config.defaultPrint]
    self.initParams()
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums;
  }
  export default {
    props:['data'],
    title: '调价记录查询',
    data() {
      return {
        other:[],
        footer:[],
        config: {
          defaultPrint: ['f_userinfo_code', 'f_user_name','f_address', 'f_user_type', 'f_gasproperties','f_oldprice','f_newprice','oldprice_performdate','newprice_performdate','f_operate_date','f_operator','f_depname']
        },
        //f_price_name:[],
        gasproperties:[],
        data: {},
        //排序
        orderitem:'f_operate_date desc',
        orderFields: {
          f_operate_date: 'no'
        },
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[]
        },
        model: new PagedList('api/af-revenue/sql/pricechangeRecordQuery', 20, {orderitem: this.orderitem?'"' + this.orderitem + ' "':`'f_operate_date desc'`} ),
        criteriaShow: false,
        orgCondtionStr: '',
        // 下拉框
        meterbrands: [],
        //合计数据
        defaultfield: [],
        sumsmodel: {},
        showupload:false,
        selected:{},
        f_filialeid: this.$login.f.f_orgid
      }
    },
    ready() {
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })

    },
    methods: {
      view(row){
        this.showupload = true
        this.selected= row
      },
      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]
      },

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


      selfSearch(args) {
        //小区查询条件
        if (this.data) {
          this.$parent.$parent.$parent.data.operateDate=this.$refs.paged.$refs.cri.model.operateDate
          this.$parent.$parent.$parent.data.performDate=this.$refs.paged.$refs.cri.model.performDate
        }
        let f_orgstr = this.orgCondtionStr
        args.condition = `${args.condition} ` + f_orgstr
        this.model.search(args.condition, args.model)
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums;

      },
      clear() {
        console.log('---------aaaa')
        //清空部门和人员
        this.$refs.paged.$refs.cri.$refs.sel.$children[1].selectres = []
        //部门和人员变为全选
        this.$refs.paged.$refs.cri.$refs.sel.$children[1].$children[0].isSelectAll = false
        this.$refs.paged.$refs.cri.$refs.sel.$children[2].$children[0].isSelectAll = false
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })

      },
      dealmsg(val) {
        val.model = this.model.model
        this.$dispatch('deal-msg', val)
      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      getRes(condition,obj) {
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
      },
      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: ''}]
        }
      },
      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:{

    },
    watch: {

      'orgCondtionStr'(val) {
        let res = val.match(/'(.*?)'/)
        console.log('正则提取:',res && res[1])
        if (res) {
          this.f_filialeid = res[1]
        }
      }
    },
    computed: {

      getCondition() {
        return {
          operateDate: this.$refs.paged.$refs.cri.model.operateDate,
          performDate: this.$refs.paged.$refs.cri.model.performDate,
          orderitem:this.orderitem,
          condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr}
      },
      getfield() {
        return exportConfig.pricechangeRecordConfig
      },
      metertypes() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气表类型')]
      },
      usertypes() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
    }
  }
</script>
