<template>
  <div class="flex-row">
    <div class="basic-main">
      <criteria-paged :model="model" v-ref:paged>
        <criteria partial='criteria' class="search_area" @condition-changed='$parent.selfSearch' v-ref:criteria>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">

              <div class="col-sm-3 form-group" style="margin-bottom: 0px">
                <label class="font_normal_body">公&emsp;&emsp;司</label>
                <right-tree-safe  @re-res="$parent.$parent.getResm" v-model="model.f_orgid" condition="tc.f_orgid in {}"></right-tree-safe>
              </div>
              <div class="form-group col-sm-3">
                <label class="font_normal_body">单位名称</label>
                <input type="text" class="input_search" v-model="model.f_company_name"
                       style="width: 60%" placeholder="单位名称" condition="f_company_name like '%{}%'">
              </div>
              <div class="form-group col-sm-3">
                <label class="font_normal_body">是否在册</label>
                <v-select :value.sync="model.f_is_checkbook" v-model='model.f_is_checkbook'
                          :value-single="true" style="width: 60%"
                          class="select_list select"
                          @change="$parent.$parent.changeState"
                          :options='$parent.$parent.isCheckBook' placeholder='有无所属片区'
                          close-on-select
                          condition="{}"></v-select>
              </div>
              <div class="form-group col-sm-3">
                <button class="button_spacing button_search" @click="search()">查询</button>
<!--                <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>
        </criteria>
        <data-grid class="list_area table_sy" style="overflow: hidden" :model="model" partial='list' v-ref:grid>
          <template partial='head'>
            <tr @dblclick.stop="">
              <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'>
            <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">{{ row.f_rea }}</td>
            <td style="text-align: center">{{ row.f_company_street }}</td>
            <td style="text-align: center">{{ row.f_company_name }}</td>
            <td style="text-align: center">{{ row.f_company_position }}</td>
            <td style="text-align: center">{{ row.f_check_book_name }}</td>
          </template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>
</template>

<script>
import {PagedList, HttpResetClass} from 'vue-client'
import * as Util from '../../../components/Util'
import bus from "../../../bus";

export default {
  title: '片区管理',
  data () {
    let model = new PagedList('rs/sql/safe_singleTable_OrderBy', 20,{
      items: "'tc.*,tcb.f_check_book_name'",
      tablename: "'t_company tc left join t_check_book tcb on tc.f_check_book_id=tcb.id'",
      orderitem: "'id desc'"
    })
    return {
      model: model,
      criteriaShow: false,
      excelHeaders: {},
      checkBooks: [],
      f_orgid: '',
      bookAll: false,
      bookList: [],
      selectBook:'',
      // init:false,
      isCheckBook: [{label: '全部', value: ''}, {label: '是', value: 'f_check_book_id is not null'}, {label: '否', value: 'f_check_book_id is null'}],
      isSearch: false,
      checkBook: {
        f_check_book_name: '',
        f_run_date: '',
        f_around_time: '',
        f_around_unit: '',
        f_create_date: Util.toStandardDateString(),
        f_create_person: this.$login.f.name,
        f_orgid: '',
        f_checker_name: ''
      }
    }
  },
  beforeDestroy() {
    //tag
    bus.$off('fresh-area')
  },
  computed: {
    searchData () {
      return {condition: this.model.condition}
    },
    bookCondition () {
      return this.model.condition
    },
    isCreateBook () {
      return this.$refs.paged.$refs.criteria.model.f_is_checkbook === 'f_check_book_name is null'
    },
    isChangeBook () {
      return this.$refs.paged.$refs.criteria.model.f_is_checkbook === 'f_check_book_name is not null'
    }
  },
  watch:{
    'bookList'(){
      this.$emit('book-list',this.bookList)
    },
    'bookAll'(val){
      if(!val){
        this.bookList=[]
      }
      this.$emit('book-all',this.bookAll)
    }
  },
  methods: {
    changeState (val) {
      //tag
      //tag
      this.bookAll = false
      this.bookList = []
      if (this.$refs.paged.$refs.criteria.model.f_orgid){
        this.$refs.paged.$refs.criteria.search()
      }
    },
    checkModel (row) {
      if (this.bookAll) {
        //tag)
        return true
      } else {
        if (this.bookList.findIndex(res => res === row.id) !== -1) {
          return true
        } else {
          return false
        }
      }
    },
    checkChange (row, e) {
      //tag
      if (e.target.checked) {
        this.bookList.push(row.id)
      } else {
        this.bookList.splice(this.bookList.findIndex(res => res === row.id), 1)
      }
    },
    getResm (obj) {
      this.$refs.paged.$refs.criteria.model.f_orgid = this.$login.convertToIn(obj.resids)
    },
    hidden () {
      this.criteriaShow = !this.criteriaShow
    },
    selfSearch (args) {
      this.isSearch = true
      this.bookAll = false
      this.bookList = []
      this.$emit('book-condition',args.condition)
      this.model.search(args.condition, args.model)
    },
    // freshCheckBook () {
    //   this.checkBooks = []
    //   new HttpResetClass().load('POST', `/rs/sql/safe_singleTable_OrderBy`, {
    //     data: {
    //       items: 'id,f_check_book_name',
    //       tablename: 't_check_book',
    //       condition: this.$refs.paged.$refs.criteria.model.f_orgid?`f_orgid in ${this.$refs.paged.$refs.criteria.model.f_orgid}`:'1=1',
    //       orderitem: 'id desc'
    //     }
    //   }, {resolveMsg: null, rejectMsg: null}).then(res => {
    //     this.checkBooks.push({label: '全部', value: ''})
    //     res.data.forEach(ress => {
    //       this.checkBooks.push({
    //         label: ress.f_check_book_name,
    //         value: ress.id
    //       })
    //     })
    //   })
    // }
  },

  ready () {
    bus.$on('fresh-area',()=>{
      console.log("进入方法",this.bookAll,this.bookList)
      this.bookAll = false
      this.bookList = []
      this.$refs.paged.$refs.criteria.search()
    })
    this.$refs.paged.$refs.criteria.model.f_is_checkbook = 'f_check_book_id is null'
    // this.freshCheckBook()
  }
}
</script>
<style  scoped>
/*.btn-group > .btn:first-child {*/
/*  margin-left: 0;*/
/*  width: 100%;*/
/*}*/
</style>
