<template>
  <div id='AddTobook'>
    <div class="auto select-overspread">
      <validator name='v'>
        <ul class="nav nav-tabs">
          <p class="bg-info text-center" style="padding: 8px;">{{tabstitles[$parent.$refs.tabs.active]}}</p>
        </ul>
        <div class="form-horizontal select-overspread container-fluid auto" style="margin-top:20px;flex: 1;" v-show="!($parent.$refs.tabs.active == 2)">
          <div class="row" style="text-align: center">

            <div class="col-sm-12 form-group" :class="[$v.f_check_book_num.required ? 'has-error' : '']">
              <label class="font_normal_body" style="width: 26%;">目标片区</label>
              <input type="text" v-model="model.f_check_book_num" v-show="false"
                     v-validate:f_check_book_num='{required: true }'>
              <v-select :value.sync="model.f_check_book_num" v-model="model.f_check_book_num"
                        :options='checkbook' placeholder='片区'
                        :value-single="true" :search="true"
                        style="width:60%"
                        close-on-select></v-select>
            </div>
          </div>
        </div>
        <div class="row"  v-show="!($parent.$refs.tabs.active == 2)">
          <div style="float:right;margin-top: 250px;margin-right: 20px;">
            <button @click="confirm()" class="btn button_search" style="width: 70px" :disabled='!$v.valid'>
              <span class="glyphicon glyphicon-floppy-disk"></span>保存
            </button>
          </div>
        </div>
      </validator>
    </div>
  </div>

</template>

<script>
import * as Util from '../../Util'
import {HttpResetClass} from "vue-client";
import bus from "../../../bus";
export default {
  name: 'AddTobook',

  data () {
    return {
      model:{
        f_check_book_num:''
      },
      checkbook: [],
      checkbooks:[],
      tabstitles:['请选择小区添加到目标片区','用户无法单独添加到片区内']
    }
  },
  props: {
    showtype:{
      type: String
    }
  },
  created () {
  },
  ready () {
    if (this.$appdata.getSingleValue('添加单位到片区')){
      this.tabstitles = ['请选择小区添加到目标片区','请选择单位添加到目标片区','用户无法单独添加到片区内']
    }
    // this.close()
    new HttpResetClass().load('POST','rs/sql/safe_singleTable_OrderBy',{data: {
        items: 'id,f_check_book_name,f_check_book_type,f_user_type',
        tablename: 't_check_book',
        condition: `f_orgid = '${this.$login.f.orgid}'`,
        orderitem: 'id'
      }}, {resolveMsg: null, rejectMsg: null}).then(res=>{
      console.log("checkbook", res.data)
      res.data.forEach(item=>{
        this.checkbooks.push({
          f_user_type:item.f_user_type,
          booktype:item.f_check_book_type,
          label:item.f_check_book_name,
          value:item.id
        })
      })
      this.checkbook = this.checkbooks.filter(item=>item.f_user_type == this.showtype)
    })
  },
  methods: {
    selectbook(val){

    },
    close () {

    },

    confirm () {
      if (!this.model.f_check_book_num){
        this.$showMessage('请选择片区')
        return
      }
      console.log(this.$parent.bookList)
      console.log(this.$parent.bookAll)
      console.log(this.$parent.bookCondition)
      let userCondition = ''
      if(this.$parent.bookAll){
        if(!this.$parent.bookCondition){
          this.$showMessage("请先查询")
          return
        }else{
          userCondition = this.$parent.bookCondition
        }
      }else if(this.$parent.bookList.length > 0){
        userCondition = this.showtype == '民用'? 'ta.id in (' + this.$parent.bookList.toString() + ')':
          'tc.id in (' + this.$parent.bookList.toString() + ')'
      }else{
        this.$showMessage(this.showtype == '民用'?'请选择小区':'请选择单位')
        return;
      }
      if(this.showtype == '民用'){
        new HttpResetClass().load('POST', 'rs/logic/updateCheckBook', {data: {f_check_book_id: this.model.f_check_book_num,
              f_check_book_type:'小区',condition:userCondition}},
          {resolveMsg: null, rejectMsg: '添加失败'}).then(res=>{
          if(res && res.data){
            this.model.f_check_book_num = ""
            this.$showMessage("本次成功添加"+res.data+"个小区",['confirm']).then(res=>{
              if(res==='confirm'){
                console.log("刷新了")
                bus.$emit('fresh-area')
              }
            })
          }else{
            this.$showMessage("添加失败")
          }
        })
      }else{
        new HttpResetClass().load('POST', 'rs/logic/updateCheckBook', {data: {f_check_book_id: this.model.f_check_book_num,
              f_check_book_type:'单位',condition:userCondition}},
          {resolveMsg: null, rejectMsg: '添加失败'}).then(res=>{
          if(res && res.data){
            this.model.f_check_book_num = ""
            this.$showMessage("本次成功添加"+res.data+"个单位",['confirm']).then(res=>{
              if(res==='confirm'){
                console.log("刷新了")
                bus.$emit('fresh-area')
              }
            })
          }else{
            this.$showMessage("添加失败")
          }
        })
      }
    }
  },
  watch:{
    'showtype'(val){
      if(val){
        this.checkbook = this.checkbooks.filter(item=>item.f_user_type == val)
      }
      this.model.f_check_book_num = ''
    }
  },
  computed: {

  }
}
</script>

<style lang="less">

</style>
