<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;">
          <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"  >
          <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 {HttpResetClass} from "vue-client";
import bus from "../../../bus";
export default {
  name: 'AddTobook',

  data () {
    return {
      model:{
        f_check_book_num:''
      },
      excelUserCondtion:false,
      checkbook: [],
      checkbooks:[],
      tabstitles:['请选择用户添加到安检册内']
    }
  },
  created () {
  },
  ready () {
    // 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: `1=1 and 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
    })
  },
  methods: {
    selectbook(val){

    },
    close () {

    },

    confirm () {
      if (!this.model.f_check_book_num){
        this.$showMessage('请选择目标安检册')
        return
      }
      console.log(this.$parent.$refs.tabs.active)
      console.log(this.$parent.bookList)
      console.log(this.$parent.bookAll)
      console.log(this.$parent.bookCondition)
      let userCondition = ''
      let f_userinfo_ids = "'"
      if(this.$parent.bookAll){
        if(!this.$parent.bookCondition){
          this.$showMessage("请先查询")
          return
        }else{
          userCondition = this.$parent.bookCondition
        }
      }else if(this.$parent.bookList.length > 0){
        if(this.$parent.$refs.tabs.active ===0){
          for(let i = 0;i<this.$parent.bookList.length;i++){
            if(i === this.$parent.bookList.length-1){
              f_userinfo_ids += this.$parent.bookList[i].f_userinfo_id + "'"
            }else{
              f_userinfo_ids += this.$parent.bookList[i].f_userinfo_id + "','"
            }
          }
          userCondition ='tui.f_userinfo_id in (' + f_userinfo_ids+ ')'
        }
      }else{
        this.$showMessage(this.$parent.$refs.tabs.active===3?'请选择用户':'请选择')
        return;
      }
      if(this.$parent.$refs.tabs.active ===1){
        let condition = ' 1=2'
        const area = this.$parent.bookList.join("','");
        condition = 'ta.id in (' + area+ ')'
        new HttpResetClass().load('POST', 'rs/logic/updateCheckBook', {data: {f_check_book_id: this.model.f_check_book_num,
              f_check_book_type:'小区',condition:condition}},
          {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 if(this.$parent.$refs.tabs.active ===0){
        new HttpResetClass().load('POST', 'rs/logic/updateCheckBook', {data: {
              f_check_book_id: this.model.f_check_book_num,
              f_check_book_type:'用户',
              booklist:this.$parent.bookList,
              condition:userCondition
            }},
          {resolveMsg: null, rejectMsg: '添加失败'}).then(res=>{
          if(res && res.data){
            this.model.f_check_book_num = ""
            this.$showMessage("本次成功添加"+this.$parent.bookList.length+"个用户",['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:{
    // '$parent.$refs.tabs.active'(val){
    //   if(val =='0'){
    //     this.checkbook = this.checkbooks.filter(item=>item.f_user_type==='民用')
    //   }else{
    //     this.checkbook = this.checkbooks.filter(item=>item.f_user_type==='非民用')
    //   }
    //   this.model.f_check_book_num = ''
    // }
  },
  computed: {

  },
  events: {
    // onFileUpload: function (file, res) {
    //   if ( this.model.f_check_book_num==''){
    //     this.$showMessage("请先选择目标安检册")
    //     return
    //   }
    //   let data = {
    //     f_check_book_id: this.model.f_check_book_num,
    //     filepath: res.f_downloadpath
    //   }
    //   new HttpResetClass().load('POST','/rs/logic/checkbookImport', {data: data}, {
    //     resolveMsg: '导入成功！',
    //     rejectMsg: '导入失败'
    //   }).then((res) => {
    //     this.excelUserCondtion = false
    //   })
    // },

  }
}
</script>

<style lang="less">

</style>
