<template>
  <div @keyup.enter="search" class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria @condition-changed='$parent.selfSearch' partial='criteria' v-ref:cri>
        <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
          <div class="row">
            <div  :class="{'col-sm-2 form-group':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">组织机构</label>
              <res-select :initresid='$parent.$parent.curorgid'
                          @res-select="$parent.$parent.getorg"
                          class="select select_list"
                          restype='organization'
                          style="width: 60%">
              </res-select>
            </div>
            <div :class="{'col-sm-2 form-group':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">气价名称</label>
              <input class="input_search" condition="f_price_name like '%{}%'" placeholder='气价名称' style="width:60%" type="text"
                     v-el:qjmc v-model="model.f_price_name" v-next-el="cx"
              >
            </div>
            <!--<div class="col-sm-4">-->
              <!--<label class="font_normal_body">生效时间大于</label>-->
              <!--<datepicker :format="'yyyy-MM-dd'"-->
                          <!--:value.sync="model.performDate"-->
                          <!--placeholder="生效日期"-->
                          <!--condition="performDate > '{}'"-->
                          <!--v-model="model.performDate" style="width: 55%">-->
              <!--</datepicker>-->
            <!--</div>-->
            <div class="form-group col-sm-6 button-range" style="margin-right: 10px" >
              <button @click="$parent.$parent.batchAdjust()" class="button_new button_spacing">批量调价</button>
              <button @click="$parent.$parent.search()" class="button_search button_spacing" v-el:cx>查询</button>
              <button @click="$parent.$parent.add()" class="button_new button_spacing">新增</button>

              <div :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                @click="$parent.$parent.criteriaShow=!$parent.$parent.criteriaShow"
                class="button_spacing"
                style="float: right"></div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div :class="{'col-sm-2 form-group':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">气价编号</label>
              <input type="number" class="input_search"  style="width: 60%" v-model="model.f_user_name" placeholder='气价编号'
                     condition="f_price_id = '{}'" >
            </div>
            <div :class="{'col-sm-2 form-group':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">气价来源</label>
              <v-select :value.sync="model.f_custom" v-model="model.f_custom"
                        placeholder='气价来源' :options="$parent.$parent.custom"
                        condition="custom = '{}'"
                        close-on-select v-ref:type>
              </v-select>
            </div>
            <div :class="{'col-sm-2 form-group':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <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':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <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':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">气价类型</label>
              <v-select :value.sync="model.pricetype" v-model="model.pricetype"
                        placeholder='气价类型' :options="$parent.$parent.pricetypes"
                        condition="f_price_type = '{}'"
                        close-on-select v-ref:type>
              </v-select>
            </div>
            <div :class="{'col-sm-2 form-group':!$parent.$parent.$parent.isdetail,'col-sm-3 form-group':$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">气价状态</label>
              <v-select :value.sync="model.f_price_state" v-model="model.f_price_state"
                        placeholder='气价状态' :options="$parent.$parent.price_states"
                        condition="f_price_state = '{}'"
                        close-on-select v-ref:type>
              </v-select>
            </div>
          </div>
        </div>

      </criteria>
      <data-grid :model="model" class="list_area table_sy"  partial='list'  v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <nobr>序号</nobr>
            </th>
            <th>
              <nobr>气价编号</nobr>
            </th>
            <th>
              <nobr>气价ID</nobr>
            </th>
            <th v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">
              <nobr>气价名称</nobr>
            </th>
            <th v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">
              <nobr>客户类型</nobr>
            </th>
            <th v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">
              <nobr>用气性质</nobr>
            </th>
            <th v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">
              <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-if="$parent.$parent.$parent.config.audit">
              <nobr>操作</nobr>
            </th>
            <th>
              <nobr>历史记录</nobr>
            </th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{$index + 1}}</td>
            <td style="text-align:center">{{row.f_price_id}}</td>
            <td style="text-align:center">{{row.id}}</td>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)"><nobr>{{row.f_price_name}}</nobr></td>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">{{row.f_user_type}}</td>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">{{row.f_gasproperties}}</td>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.$parent.usershow && !($parent.$parent.$parent.$parent.isdetail && !$parent.$parent.$parent.$parent.usershow)">{{row.f_price_type}}</td>
            <td style="text-align:center">{{row.new_version}}</td>
            <td style="text-align:center"><nobr>{{row.f_value}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_filiale_name}}</nobr></td>
            <td style="text-align:center"><button @click.stop="$parent.$parent.$parent.operate(row)" class="btn btn-link"><b>{{row.usercount}}</b></button></td>
            <td style="text-align:center"><b>{{row.usercountnew}}</b></td>
            <td style="text-align:center">{{row.f_price_state}}</td>
            <td style="text-align:center" v-if="$parent.$parent.$parent.config.audit">
              <button :disabled="$login.r.includes('气价审核')" @click.stop="$parent.$parent.$parent.passPrice(row)"
                      class="btn btn-link" v-if="row.f_state === '待审核'">通过
              </button>
            </td>
            <td style="text-align: center;">
              <button type="button" name="button" class="button_search-2 button_spacing width-60"
                      @click.stop="$parent.$parent.$parent.showhistory(row)">查看</button>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
    <modal :backdrop="false" :show.sync="show" v-if="show" width="1000px">
      <header class="modal-header" slot="modal-header">
        <button @click="show=!show" class="close" type="button"><span>&times;</span></button>
        <h4 class="modal-title">历史记录</h4>
      </header>
      <article class="modal-body"  slot="modal-body"  style="height:100%;width:100%"  title="历史记录">
        <price-change-history :row="row"  :show.sync="show"  @select-changed="historySeleted" v-on:toggle="close"></price-change-history>
      </article>
      <footer class="modal-footer" slot="modal-footer">

      </footer>
    </modal>
  </div>
</template>

<script>
  /**
   *阶梯气价查询列表
   */
  import {PagedList} from 'vue-client'
  import Vue from 'vue'

  let passGen = async function (self, row) {
    let param = {
      id: row.id,
      userid: self.$login.f.id
    }
    await self.$resetpost('rs/logic/priceNextFlow', param, {
      warnMsg: `确定要通过气价名称为: ${row.f_price_name}的气价吗？`,
      resolveMsg: '审核通过',
      rejectMsg: '流程进行出错！！！'
    })
    self.$dispatch('success')
  }

  export default {
    data () {
      return {
        model: new PagedList('rs/sql/getGasPriceInfo', 20, {performDate: 'this.model.performDate', orderitem: `'${this.orderitem}'`}),
        show: false,
        criteriaShow: false,
        // 公司下拉
        curorgid: [this.$login.f.orgid],
        row: {},
        f_orgid: '',
        gasproperties: [],
        custom: [{label: '全部', value: ''}, {label: '正常气价', value: '正常气价'}, {label: '自定义气价', value: '自定义气价'}],
        price_states: [{label: '全部', value: ''}, {label: '使用中', value: '使用中'}, {label: '删除', value: '删除'}, {label: '已作废', value: '已作废'}],
        usertypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : []
      }
    },
    props: {
      orderitem: {
        type: String,
        default: 'f_price_id'
      },
      row: {},
      config: {},
      ispartial: false,
      f_filialeid: {
        type: String
      }
    },
    ready () {
      this.$refs.paged.$refs.cri.model.performDate = this.$login.toStandardDateString()
      this.$refs.paged.$refs.cri.model.f_price_state = '使用中'
      this.search()
    },
    methods: {
      batchAdjust () {
        this.$parent.usershow = true
        this.$parent.priceid = null
        this.$parent.priceorgid = null
      },
      showhistory (row) {
        this.show = true
        this.row = row
      },
      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: ''}]
        }
      },
      operate (row) {
        this.$parent.usershow = true
        this.$parent.priceid = row.f_price_id
        this.$parent.pricename = row.f_price_name
        this.$parent.f_islow_income = row.f_islow_income
        this.$parent.priceorgid = row.f_filialeid
      },
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('search')
      },
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeid = val[0]
      },
      selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        console.log('查询。。。', this.f_orgid)
        if (this.f_orgid) {
          args.condition = `f_filialeid  in ${this.f_orgid} and ${args.condition}`
        }
        this.$refs.paged.$refs.grid.$el.scrollTop = 0
        this.model.search(args.condition, args.model)
      },
      add () {
        this.$dispatch('add')
      },
      passPrice (row) {
        console.log('气价审核通过。。。', row, this.$refs.paged.$refs.grid.selected)
        let selectedRow = this.$refs.paged.$refs.grid.selected
        if (selectedRow) {
          if (row.id !== selectedRow.id && selectedRow.f_state === '待审核') {
            this.$showAlert('您通过的气价信息不是您选中的信息', 'warning', 3000)
          } else {
            passGen(this, row)
          }
        } else {
          passGen(this, row)
        }
      },
      historySeleted () {

      }

    },
    watch: {
      'model.rows.length' (val) {
        if (val === 1) {
          this.$refs.paged.$refs.grid.select(this.model.rows[0])
          this.$parent.usershow = false
        }
      }
    },
    computed: {
      pricetypes () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气价类型')]
      },
      bookstates () {
        return [ {label: '有效', value: "= '有效'"}, {label: '无效', value: "= '无效'"} ]
      }
    }
  }
</script>
