<template>
  <div class="auto select-overspread">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="form-group col-sm-3">
              <label class="font_normal_body">客户编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_userinfo_code"
                     placeholder='客户编号'
                     condition="f_userinfo_code = '{}'" @keyup.enter="search()">
            </div>
            <div class="form-group col-sm-2 button-range">
              <button class="button_search" style="margin-right: 10px" @click="search()" v-el:cba>查询</button>

            </div>
            <div class="form-group col-sm-2 button-range">
              <button class="button_search" style="margin-right: 10px" @click="$parent.$parent.delOfCheckBook()" v-el:cba>移出</button>

            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" class="table_sy" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <nobr><input type="checkbox" v-model="$parent.$parent.$parent.bookAll"></input>全选</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'>
          <tr>
            <td style="text-align: center">
              <nobr><input type="checkbox" :checked="$parent.$parent.$parent.checkModel(row)"
                           @change="$parent.$parent.$parent.checkChange(row, $event)"></input>
              </nobr>
            </td>
            <td style="text-align:center;">{{ $index + 1 }}</td>
            <td style="text-align:center">{{ row.f_userinfo_code }}</td>
            <td style="text-align:center">{{ row.f_user_name }}</td>
            <td style="text-align:center">{{ row.f_user_phone }}</td>
            <td style="text-align:center">{{ row.f_address }}</td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

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

export default {
  title: '安检记录列表',
  props: ['row'],
  data() {
    return {
      rowdata: this.row,
      bookAll: false,
      bookList:[],
      model: new PagedList('rs/sql/safe_singleTable_OrderBy', 50,
        {
          items: '"tua.id,tua.f_address,tui.f_userinfo_code,tui.f_user_name,tui.f_user_phone,tui.f_userinfo_id"',
          tablename: '"t_user_address tua left join t_userinfo tui on tua.f_userinfo_id = tui.f_userinfo_id"',
          orderitem: '"tua.id desc"'
        }
      )
    }
  },
  ready() {
    if (this.row) {
      let condition = ''
      condition = `tua.f_check_book_id =${this.row.id}`
      this.model.search(condition)
      //只显示用户页面
      this.row.f_check_book_type = '用户'
    }
  },
  watch: {
    'row'(val) {
      if (val) {
        this.rowdata = val
        let condition = ''
        condition = `tua.f_check_book_id =${val.id} `
        this.model.search(condition)
      }
    }
  },
  methods: {
    selfSearch(args) {
      if (this.rowdata.id) {
        args.condition += `and tua.f_check_book_id =${this.rowdata.id}`
        this.model.search(args.condition)
      } else {
        this.$showMessage('请选择片区')
      }
    },
    checkModel (row) {
      if (this.bookAll) {
        return true
      } else {
        return this.bookList.findIndex(res => res === row.id) !== -1;
      }
    },
    checkChange (row, e) {
      if (e.target.checked) {
        this.bookList.push(row.id)
      } else {
        this.bookList.splice(this.bookList.findIndex(res => res === row.id), 1)
      }
    },
    delOfCheckBook(){
      try {
        let upAddress = `update t_user_address set f_check_book_id = null where 1 = 2 `
        if (this.bookAll){
          upAddress = `update t_user_address set f_check_book_id = null where f_check_book_id = '${this.row.id}'`
        }else if (this.bookList.length > 0){
          let codes = this.bookList.map( item => { return `'${item}'` } ).join()
          upAddress = `update t_user_address set f_check_book_id = null where id in (${codes})`
        }
        this.$showMessage('确认移出安检册', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            let http = new HttpResetClass()
            http.load('POST', 'rs/logic/runSQL', {data: {sql: upAddress}},
              {resolveMsg: null, rejectMsg: '移出安检册失败'}).then(resp=>{
              this.$showMessage('移出成功')
              this.$refs.paged.$refs.criteria.search()
            }).catch(error=>{
              this.$showMessage('移出失败',error)

            })
          }
        })
      }catch (e) {
        this.$showMessage('移出失败',e)
      }
    }
  }
}
</script>
