<template>

  <div class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' v-ref:criteria @condition-changed='$parent.selfSearch'>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="form-group col-sm-2">
              <label class="font_normal_body">组织机构</label>
              <res-select
                restype='organization'
                @res-select="$parent.$parent.getorg"
                is-mul="false"
                :initresid='$parent.$parent.curorgid'
              >
              </res-select>
            </div>
            <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.f_userinfo_code"
                     condition="f_userinfo_code = '{}'"
                     placeholder="用户编号"/>
            </div>
            <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.f_user_name"
                     condition="f_user_name = '{}'"
                     placeholder="用户姓名"/>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">调价状态</label>
              <v-select :value.sync="model.f_surp_state" v-model='model.f_surp_state' style="width:60%"
                        :options='$parent.$parent.surpState' placeholder='状态' close-on-select
                        condition="f_surp_state ='{}'"  >
              </v-select>
            </div>

            <div class="span col-sm-2 form-group button-range" style="float: right">
              <button class="button_search button_spacing" @click="search()" v-el:cx>查询</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.changePrice()">调价</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.cancelPrice()">冲正</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.SurpPrice()">查看补差记录</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
              <div style="float: right" class="button_spacing"
                   :class="{'button_shrink_top': criteriaShow,'button_shrink_bottom': !criteriaShow}"
                   @click="criteriaShow = !criteriaShow"></div>
            </div>
            <div class="row" v-show="criteriaShow">
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">用户类型</label>
                <v-select :value.sync="model.f_user_type" v-model='model.f_user_type' style="width:60%"
                          :options='$parent.$parent.usertypes' placeholder='用户类型' close-on-select
                          condition="f_user_type ='{}'" :value-single="true" :search="false">
                </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' style="width:60%"
                          :options='$parent.$parent.pricename' placeholder='气价名称' close-on-select
                          condition="f_price_name ='{}'" :value-single="true" :search="false">
                </v-select>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">收费时间</label>
                <datepicker placeholder='收费时间' style="width:60%"
                            v-model="model.startDate"
                            :value.sync="model.startDate"
                            :format="'yyyy-MM-dd 00:00:00'"
                            :show-reset-button="true"
                            condition="f_operate_date >= '{}'">
                </datepicker>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">&emsp;&emsp;&emsp;至</label>
                <datepicker placeholder='结束时间' style="width:60%"
                            v-model="model.endDate"
                            :value.sync="model.endDate"
                            :format="'yyyy-MM-dd 23:59:59'"
                            :show-reset-button="true"
                            condition="f_operate_date <= '{}'">
                </datepicker>
              </div>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' class="list_area table_sy" v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox" @click="$parent.$parent.$parent.addOrRemove($parent.$parent.$parent.selectAll)"
                     :checked="$parent.$parent.$parent.selectAll">
            </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>
              <nobr>操作人</nobr>
            </th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">
              <input type="checkbox" v-model="$parent.$parent.$parent.selecteds" :value="row">
            </td>
            <td style="text-align:center">{{row.f_userinfo_code}}</td>
            <td style="text-align:center">{{row.f_user_name}}</td>
            <td style="text-align:center">{{row.f_user_phone}}</td>
            <td style="text-align:center">{{row.f_address}}</td>
            <td style="text-align:center">{{row.f_user_type}}</td>
            <td style="text-align:center">{{row.f_gasproperties}}</td>

            <td style="text-align:center">{{row.f_price_name}}</td>
            <td style="text-align:center">{{row.f_price}}</td>
            <td style="text-align:center">{{row.f_pregas}}</td>
            <td style="text-align:center">{{row.f_preamount}}</td>
            <td style="text-align:center">{{row.f_surp_state}}</td>
            <td style="text-align:center">{{row.f_operate_date}}</td>
            <td style="text-align:center">{{row.f_operator}}</td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal :show.sync="priceshow" v-ref:modal large backdrop="false" :width="900">

    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="priceshow = false"><span>&times;</span></button>
      <h4 class="modal-title">调价内容</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <validator name='v'>
      <div class="auto select-overspread form-horizontal">
        <div class="row auto">
          <div class="col-sm-6 form-group form-input-group" :class="[$v.f_new_price.required ? 'has-error' : '']">
            <label class="control-label">新气价:</label>
            <input class="form-control col-sm-2" v-validate:f_new_price='{required: true }'
                   v-model="row.f_new_price" placeholder="单价">
          </div>
        </div>
        <div class="row auto">
          <div class="col-sm-6 form-group form-input-group">
            <label class="control-label">备注:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_comments" placeholder="备注">
          </div>
        </div>
      </div>
      </validator>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn button_search" :disabled="!row.f_new_price"
              @click='confirm'>确认调价
      </button>
    </footer>

  </modal>
</template>

<script>
  import {PagedList} from 'vue-client'

  export default {
    data () {
      return {
        curorgid: [this.$login.f.orgid],
        model: new PagedList('rs/sql/queryCardSelling', 20, {}),
        selecteds: [],
        priceshow: false,
        usertypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : [],
        surpState:[{label: '全部', value: ''},{label:'已调价',value:'已调价'},{label:'未调价',value:'未调价'}],
        row: {},
        pricename:[{label: '全部', value: ''}]
      }
    },
    ready () {
      this.$refs.paged.$refs.criteria.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.criteria.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
      this.$refs.paged.$refs.criteria.model.f_surp_state=['未调价']
    },
    methods: {
      SurpPrice(){
        this.$dispatch('person')
      },
    async  cancelPrice(){
        if(this.$refs.paged.$refs.criteria.model.f_surp_state!='已调价'){
          return this.$showAlert('调价条件不等于已调价，请重新选择','warning',3000)
        }
        //调价补差冲正
        let param = {
          selecteds: this.selecteds,
          f_operator: this.$login.f.name,
          f_operatorid: this.$login.f.id,
          f_orgid: this.$login.f.orgid,
          f_orgname: this.$login.f.orgs,
          f_depid: this.$login.f.depids,
          f_depname: this.$login.f.deps,
          f_start_date : this.$refs.paged.$refs.criteria.model.startDate,
          f_end_date : this.$refs.paged.$refs.criteria.model.endDate
        }
        await  this.$resetpost('rs/logic/cardSurpSellingRush', {data: param}, {
          resolveMsg: '冲正成功',
          rejectMsg: '冲正失败'
        }).then((res) => {
          if (res.data) {
            this.$dispatch('cancel', this.selecteds),
              this.search()
          }
        })
      },
      async confirm () {
        this.priceshow = false
        if(!this.$refs.paged.$refs.criteria.model.startDate){
          return this.$showAlert('请选择收费时间','error',3000)
        }
          let param = {
            selecteds: this.selecteds,
            f_new_price:this.row.f_new_price,
            f_operator: this.$login.f.name,
            f_operatorid: this.$login.f.id,
            f_orgid: this.$login.f.orgid,
            f_orgname: this.$login.f.orgs,
            f_depid: this.$login.f.depids,
            f_depname: this.$login.f.deps,
            f_start_date : this.$refs.paged.$refs.criteria.model.startDate,
            f_end_date : this.$refs.paged.$refs.criteria.model.endDate
          }
        await  this.$resetpost('rs/logic/cardsurpDetail', {data: param}, {
            resolveMsg: '生成补差记录成功',
            rejectMsg: '生成补差记录失败'
          }).then((res) => {
            if (res.data) {
              this.$dispatch('person', this.selecteds),
                this.search()
            }
          })
      },
      changePrice () {
        this.row.f_price=this.selecteds[0].f_price
        this.priceshow = true
      },
      getorg (val) {
        this.f_filialeid = val[0]
      },
      search () {
        this.$refs.paged.$refs.criteria.search()
      },
      async selfSearch (args) {

        args.condition = `${args.condition} and f_orgid in (${this.curorgid})`
         await this.model.search(args.condition, args.model)
        let arr2 = this.model.rows.filter((value, index, array) => {
          return array.findIndex(item => item.key === value.key && item.f_price_name === value.f_price_name) === index
        })
        let rs = [{label: '全部', value: ''}]
        arr2.forEach(function (item) {
          console.log('item', item.f_price_name)
          let temp = {
            label: item.f_price_name,
            value: item.f_price_name
          }
          rs.push(temp)
        })
        this.pricename = rs
      },
      addOrRemove (is) {
        console.log('this,', is)
        this.selecteds = []
        if (!is) { // 添加本页所有数据到ids中
          this.model.rows.forEach((row) => {
            this.selecteds.push(row)
          })
          this.selectAll = true
        } else { // 从ids中取消所有的数据
          this.selecteds = []
          this.selectAll = false
        }
        console.log('selectdde', this.selecteds)
      },
      clear () {
        this.$refs.paged.$refs.criteria.model = {}
      }
    },
    watch: {
      selecteds () {
        console.log('框框的长度', this.selecteds.length)
      }
    }
  }
</script>
<style lang="less">
</style>
