<template>
  <div style="flex:1; " 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-3 form-group form-input-group" style="width: 20%">
                <label class="control-label">用户编号:</label>
                <input type="text" class="form-control fixed-width" v-model="model.f_userinfo_code" placeholder='用户编号'
                       condition="h.f_userinfo_code like '%{}%'" v-next-el='bbh' v-filter v-focus>
            </div>
              <div class="col-sm-3 form-group form-input-group" style="width: 20%">
                 <label class="control-label">用户姓名:</label>
                <input type="text" class="form-control fixed-width" v-model="model.f_user_name" placeholder='用户姓名'
                       condition="h.f_user_name like '%{}%'" v-next-el='bbh' v-filter v-focus>
              </div>
              <div class="col-sm-3 form-group form-input-group" style="width: 20%">
                 <label class="control-label">表号:</label>
                <input type="text" class="form-control fixed-width" v-model="model.f_merternumber" placeholder='表号'
                       condition="u.f_meternumber like '%{}%'" v-next-el='xq' v-filter v-focus v-el:bbh>
              </div>
              <div class="col-sm-3 form-group form-input-group" style="width: 20%">
                <label class="control-label">指令类型:</label>
                <input type="text" class="form-control" v-model="model.f_instruct_type" placeholder='指令类型'
                       condition="i.f_instruct_type like '%{}%'" id="f_instruct_type" v-el:xq v-next-el='cx'>
              </div>
              <div class="col-sm-3 form-group form-input-group" style="width: 20%">
                <label class="control-label">小区:</label>
                <input type="text" class="form-control" v-model="model.f_districtname" placeholder='小区'
                       condition="h.f_residential_area like '%{}%'" id="f_residential_area" v-el:xq v-next-el='cx'>
              </div>
              <div class="col-sm-3 form-group form-input-group" style="width: 20%">
                <label class="control-label">上表状态:</label>
                <v-select :value.sync="model.f_receive_state" v-model="model.f_receive_state"
                          :options='$parent.$parent.receivestatus' placeholder='上表状态'
                          close-on-select condition="i.f_receive_state='{}'"></v-select>
              </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">组织机构</label>
              <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source'  @re-res="$parent.$parent.getRes"></right-tree>
            </div>
            <div class="col-sm-1 form-group">
              <label class="control-label"></label>
                <button class="btn btn-success width-100" style = "float:right;" @click="search()" v-el:cba>查询</button>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>用户编号</th>
            <th>用户信息</th>
            <th>表号</th>
            <th>表厂</th>
            <th>指令类型</th>
            <th>指令内容</th>
            <th>指令状态</th>
            <th>上表状态</th>
            <th>指令生成时间</th>
            <th>操作</th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{$index+1}}</td>
            <td>{{row.f_userinfo_code}}</td>
            <td>{{row.f_user_name}}&nbsp;&nbsp;{{row.f_address}}</td>
            <td>{{row.f_meternumber}}</td>
            <td>{{row.f_manufacturers}}</td>
            <td>{{row.f_instruct_type}}</td>
            <td>{{row.f_manufacturers==='重庆山城' ? row.f_cmdparam:''}}</td>
            <td>{{row.f_instruct_state}}</td>
            <td>{{row.f_receive_state}}</td>
            <td>{{row.f_instruct_date}}</td>
            <td>
              <dropdown v-if="row.f_manufacturers === '重庆山城'">
                <button type="button" data-toggle="dropdown" style="border: 0px;background: none;">
                  <span class="glyphicon glyphicon-th-list" style="position: inherit;"></span>
                </button>
                <ul slot="dropdown-menu" class="dropdown-menu dropdown-menu-right">
                  <li v-if="row.f_receive_state !== '成功'">
                    <a href="#" @click.stop="$parent.$parent.$parent.operate(row.cmdid)"></span>查询</a>
                  </li>
                  <li v-if="row.f_receive_state === '失败'">
                    <a href="#" @click.stop="$parent.$parent.$parent.resend(row.id)"></span>重新发送</a>
                  </li>
                </ul>
              </dropdown>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  /**
   *指令状态查看
   */
  import {PagedList} from 'vue-client'
  import co from 'co'

  let refreshGen = function* (self, ids) {
    yield self.$resetpost('rs/logic/getreceivestates', {data: {cmdid: ids}}).then((res) => {
      console.log('res', res)
      self.search()
    })
  }
  export default {
    title: '指令查看',
    data() {
      return {
        // filialeNameStr: null,
        // filialeCodeStr: this.$login.f.f_orgids,
        // userid: this.$login.f.id,
        // source:`tool.getFullTree(this.getRights().where(row.getType() != $zone$).where(row.getType() != $role$))`,
        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$))`,
        model: new PagedList('rs/sql/webQueryInstruct', 50)
      }
    },
    ready() {
      console.log('进入ready')
      // return this.search().then(() => {
      //   this.$emit('ready')
      // }).catch((error) => {
      //   this.$emit('error', error)
      // })
      this.search()
    },
    methods: {
      getRes(obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
      refreshs() {
        console.log('刷新了00000000000000000')
        this.search()
        let self = this
        if (this.model.rows.length > 0) {
          let dataArr = this.model.rows
          let ids = []
          console.log('dataArr  is', dataArr)
          dataArr.forEach(function (item) {
            ids.push(item.cmdid)
          })
          console.log('ids', '(' + ids.toString() + ')')
          co(refreshGen(self, '(' + ids.toString() + ')'))
        }
      },
      resend(id) {
        let data = {
          id: id,
          f_receive_state: '0',
          f_instruct_state: '保存成功'
        }
        return this.$resetpost('rs/entity/t_instruct', data).then(() => {
          this.search()
        })
      },
      operate(id) {
        console.log('id =', id)
        let ids = []
        ids.push(id)
        let arg = '(' + ids.toString() + ')'
        this.$resetpost('rs/logic/getreceivestates', {data: {cmdid: arg}}).then((res) => {
          console.log('res', res)
          this.search()
        })
      },
      search() {
        this.$refs.paged.$refs.cri.search()
      },
      // selfSearch(args) {
      //   args.condition = `${args.condition} and h.f_filialeids like '${this.$login.f.f_orgids.replace(/\./g, '')}%'`
      //   this.model.search(args.condition, args.model)
      // }
        async selfSearch (args) {
          let f_orgstr = this.filialeCodeStr
          console.log('xxxx',f_orgstr)
          args.condition =  `${args.condition} and h.f_filialeids like '${f_orgstr}%'`
          console.log('我的阐述',JSON.stringify(args.condition))
          await this.model.search(args.condition, args.model)
         }
    },
    computed: {
      receivestatus() {
        return [
          {label: '全部', value: ''},
          {label: '未执行', value: '0'},
          {label: '执行中', value: '1'},
          {label: '成功', value: '2'},
          {label: '失败', value: '3'},
          {label: '上表成功', value: '上表成功'},
          {label: '上表失败', value: '上表失败'}
        ]
      }
    }
  }
</script>
<!--<style scoped>-->
  <!--.PanelTR {-->
    <!--width: 100%;-->
    <!--height: 40px;-->
  <!--}-->

  <!--.PanelTD {-->
    <!--width: 25%;-->
  <!--}-->
<!--</style>-->
