<template >
  <div class="flex" style="justify-content: space-between;">
    <div @keyup.enter="search" style="">
      <criteria-paged :model="model" v-ref:paged>
        <criteria partial='criteria' @condition-changed='$parent.search' v-ref:criteria>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">
              <div class="col-sm-2 form-group" >
                <label class="font_normal_body">设备编号</label>
                <input  type="text" style="width:60%" class="input_search" v-model="model.f_devices_no"
                        condition="f_devices_no like '%{}%'" placeholder='设备编号'>
              </div>
              <div class="col-sm-2 form-group" >
                <label class="font_normal_body">设备品牌</label>
                <input  type="text" style="width:60%" class="input_search" v-model="model.f_brand"
                        condition="f_brand like '%{}%'" placeholder='设备品牌'>
              </div>
              <div class="col-sm-2 form-group" >
                <label class="font_normal_body">设备型号</label>
                <input  type="text" style="width:60%" class="input_search" v-model="model.f_devices_model"
                        condition="f_devices_model like '%{}%'" placeholder='设备型号'>
              </div>
              <div class="col-sm-2 form-group" >
                <label class="font_normal_body">管道类型</label>
                <v-select :value.sync="model.f_pipe_type"
                          v-model="model.f_pipe_type"
                          :options='$parent.$parent.pipetype' placeholder='请选择'
                          condition="f_pipe_type = '{}'"
                          close-on-select></v-select>
              </div>

              <div class="span" style = "float:right;">
                <button class="button_search button_spacing" @click="search()">查询</button>
                <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
                <print-data :defaultfield="$parent.$parent.defaultfield" :field="$parent.$parent.fields" :is-selected="true" :model="$parent.$parent.printModel"
                            @print-data="$parent.$parent.print()" print-name="设备查询"></print-data>
                <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()"></div>
              </div>
            </div>
            <div class="row" v-show="false">

              <div class="col-sm-2 form-group" >
                <label for="startDate" class="font_normal_body" title="操作开始日期">开始日期</label>
                <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                            v-model="model.startDate"
                            :value.sync="model.startDate"
                            :format="'yyyy-MM-dd'"
                            :show-reset-button="true"
                            condition="f_operate_date >= '{} 00:00:00'">
                </datepicker>
              </div>
              <div class="col-sm-2 form-group" >
                <label for="endDate" class="font_normal_body" title="操作结束日期">结束日期</label>
                <datepicker id="endDate" placeholder="结束日期" style="width:60%"
                            v-model="model.endDate"
                            :value.sync="model.endDate"
                            :format="'yyyy-MM-dd'"
                            :show-reset-button="true"
                            condition="f_operate_date <= '{} 23:59:59'">
                </datepicker>
              </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><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'>
            <td style="text-align: center;"><nobr>{{row.f_meternumber}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.watchpurchase}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_devices_type}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_devices_no}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_brand}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_devices_model}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_devices_num}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_userinfodevices_state}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_pipe_type}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_make_date}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_service_life}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_input_person}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_input_date}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_expire_date}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_operator}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_operate_date}}</nobr></td>
            <td style="text-align: center;"><nobr>{{row.f_comments}}</nobr></td>
          </template>
          <template partial='foot'>

          </template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>
</template>

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

  export default {
    title: '设备查询',
    data () {
      return {
        condition: '1=1',
        model: new PagedList('rs/sql/sale_deviceQuery', 20, {}, ),
        show: false,
        deviceTypes: this.$appdata.getParam('设备类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('设备类型')] : [],
        // brand: this.$appdata.getParam('设备品牌') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('设备品牌')] : [],
        watchpurchases: this.$appdata.getParam('购买方式') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('购买方式')] : [],
        pipetype:  this.$appdata.getParam('管道类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('管道类型')] : [],
        defaultfield: [ 'f_meternumber', 'watchpurchase', 'f_devices_type'],
        fields: {  'f_meternumber': '表号', 'watchpurchase': '购买方式', 'f_devices_type': '设备类型',
          'f_devices_no': '设备编号',
          'f_brand': '设备品牌', 'f_devices_model': '设备型号', 'f_devices_num': '设备数量', 'f_userinfodevices_state': '设备状态',
          'f_pipe_type': '管道类型', 'f_make_date': '生产日期', 'f_service_life': '使用年限', 'f_input_person': '安装人'},
        printModel: {
          rows: []
        },
        all: [],
        // row数据
        rowsdata: [],
      }
    },
    props: ['row'],
    ready () {
      this.$refs.paged.$refs.criteria.search()
    },
    methods: {
      select () {
        let index = this.model.pageIndex - 1
        if (!this.radio[index]) {
          this.radio.$set(index, [])
        }
        if (this.all[index]) {
          // 数据
          this.rowsdata[index] = Object.assign([], this.model.rows)
          // 勾选
          for (var i = 0; i < this.model.rows.length; i++) {
            this.radio[index].$set(i, true)
          }
        } else {
          // 数据
          this.rowsdata[index] = []
          // 不勾选
          for (var i = 0; i < this.model.rows.length; i++) {
            this.radio[index].$set(i, false)
          }
        }
        let z = 0
        for (let i = 0; i < this.all.length; i++) {
          for (let j = 0; this.rowsdata[i] && j < this.rowsdata[i].length; j++) {
            this.printModel.rows[z++] = this.rowsdata[i][j]
          }
        }
      },
      hidden(){
        this.criteriaShow = !this.criteriaShow
      },
      async search () {
        console.log('aaa:', this.$refs.paged.$refs.criteria.condition)
        this.condition = `${this.$refs.paged.$refs.criteria.condition} and f_user_id = '${this.row.f_user_id}' `
        this.model.search(this.condition, this.model)
        let http = new HttpResetClass()
        let res = await http.load('POST', 'rs/sql/sale_deviceQuery', {
          data: {
            condition: this.condition
          }
        }, {resolveMsg: null, rejectMsg: null})
        this.printModel.rows = res.data
      },
      // 多选框初始化
      selectInit () {
        this.rowsdata = []
        this.all = []
        this.radio = []
      },
      print () {
        this.selectInit()
      },
      selectOne (event, row, i) {
        let index = this.model.pageIndex - 1
        if (!this.rowsdata[index]) {
          this.rowsdata[index] = []
        }
        if (!this.radio[index]) {
          this.radio.$set(index, [])
        }
        if (event.target.checked) {
          // 数据
          this.rowsdata[index][i] = row
          // 勾选
          this.radio[index].$set(i, true)
          // 判断是否全部选中
          var allState = true
          if (this.model.rows.length != this.radio[index].length) {
            allState = false
          }
          for (var state of this.radio[index]) {
            if (!state) {
              allState = false
            }
          }
          if (allState) {
            this.all.$set(index, true)
          } else {
            this.all.$set(index, false)
          }
        } else {
          // 数据
          this.rowsdata[index][i] = []
          // 不勾选
          this.radio[index].$set(i, false)
          // 任意取消一个则全选状态设为false
          this.all.$set(index, false)
        }
        let z = 0
        this.printModel.rows = []
        for (let i = 0; i < this.all.length; i++) {
          for (let j = 0; this.rowsdata[i] && j < this.rowsdata[i].length; j++) {
            if (this.rowsdata[i][j] && this.rowsdata[i][j].f_user_id) {
              this.printModel.rows[z++] = this.rowsdata[i][j]
            }
          }
        }
      }
    },
    computed: {
    }
  }
</script>

