<template>
  <div  class="flex">
    <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-3">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          class="select select_list"
                          style="width: 60%"
                          @res-select="$parent.$parent.getorg"
                          :initresid='$parent.$parent.curorgid'>
              </res-select>
            </div>
            <div class="col-sm-3">
              <label class="font_normal_body">气价名称</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_price_name" placeholder='气价名称'
                     condition="f_price_name like '%{}%'" v-next-el="cx" v-el:qjmc
                     >
            </div>
            <div class="col-sm-4">
              <label class="font_normal_body">生效时间小于</label>
              <datepicker :format="'yyyy-MM-dd'"
                          :value.sync="model.performDate"
                          placeholder="生效日期"
                          v-model="model.performDate" style="width: 55%">
              </datepicker>
            </div>
             <div class="span" style="float:right;">
              <button class="button_search" @click="$parent.$parent.search()" v-el:cx>查询</button>
              <button class="button_new" @click="$parent.$parent.add()">新增</button>
            </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 v-show="!$parent.$parent.$parent.ispartial">
              <nobr>生效日期</nobr>
            </th>
            <th v-show="!$parent.$parent.$parent.ispartial">
              <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>
          </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.f_user_type}}</td>
            <td style="text-align:center">{{row.f_gasproperties}}</td>
            <td style="text-align:center">{{row.f_price_type}}</td>
            <td style="text-align:center">{{row.f_price_name}}</td>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.ispartial">
              <nobr>{{row.f_perform_date}}</nobr>
            </td>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.ispartial">{{row.f_stair_start_date}}</td>
            <td style="text-align:center">{{row.f_orgname}}</td>
            <td style="text-align:center"><button class="btn btn-link" @click.stop="$parent.$parent.$parent.operate(row.f_price_id)">{{row.usercount}}</button></td>
            <td style="text-align:center">{{row.f_state === '待审核' ? '待审核' : '使用中'}}</td>
            <td style="text-align:center" v-if="$parent.$parent.$parent.config.audit">
              <button v-if="row.f_state === '待审核'" class="btn btn-link"
                      @click.stop="$parent.$parent.$parent.passPrice(row)" :disabled="$login.r.includes('气价审核')">通过
              </button>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </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 + '"'}),

        // 公司下拉
        curorgid: [this.$login.f.orgid],

        f_orgid: ''
      }
    },
    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.search()
    },
    methods: {
      operate (id) {
        this.$parent.usershow=true
        this.$parent.priceid=id
      },
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('search')
      },
      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 = `${args.condition} and s.f_filialeid  in ${this.f_orgid}`
        }
        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)
        }

      }

    },
    watch: {
      'model.rows.length' (val) {
        if (val === 1) {
          this.$refs.paged.$refs.grid.select(this.model.rows[0])
          this.$parent.usershow=false
        }
      }
    }
  }
</script>
