<template>
  <div style="flex:1;" class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div class="form-group">
            <label>使用公司</label>
            <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source'
                        @re-res="$parent.$parent.getRes"></right-tree>
          </div>
          <div class="form-group">
            <label>气价名称</label>
            <input type="text" class="form-control" v-model="model.f_price_name" placeholder='气价名称'
                   condition="f_price_name = '{}'" v-next-el="cx" v-el:qjmc
                   :size="model.f_price_name ? model.f_price_name.length*2 : 4">
          </div>
          <div class="form-group">
            <button class="btn btn-success width-100" @click="$parent.$parent.search()" v-el:cx>查询</button>
          </div>
          <div class="form-group">
            <button class="btn btn-success width-100" @click="$parent.$parent.add()">新增气价</button>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' 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_filiale}}</td>
            <td style="text-align:center">{{row.usercount}}</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="!$parent.$parent.$parent.permissions.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,
      f_operator: self.$login.f.name
    }
    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, {orderitem: '"' + this.orderitem + '"'}),
        filialeNameStr: this.$login.f.f_fengongsi,
        filialeCodeStr: this.$login.f.f_orgids,
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,

        permissions: this.$login.r
      }
    },
    props: {
      orderitem: {
        type: String,
        default: 'f_price_id'
      },
      row: {},
      config: {},
      ispartial: false
    },
    ready () {
        console.log('权限。。。', this.permissions)
      this.search()
    },
    methods: {
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('search')
      },
      getRes (obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
      selfSearch (args) {
        args.condition = `${args.condition} and s.f_filialeids like '${this.filialeCodeStr}%'`
        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])
        }
      }
    }
  }
</script>
