<template>
  <div class="auto select-overspread">
    <validator name='v'>
      <form class="form-horizontal" novalidate>
        <ul class="nav nav-tabs" style="margin-bottom: 10px">
          <li class="active"><a href="#">安检册信息</a></li>
        </ul>
        <div class="row" style="margin-top: 10px">
          <div class="col-sm-12 form-group"  style="margin: 0">
            <role-selector-safe
              @alluser="getUsers"
              @re-res="getRes"
              role-name="安检员"
              role-lable="安&nbsp;检&nbsp;员&nbsp;"
              :value.sync="checkBook.f_checker_name"
              v-model="checkBook.f_checker_name">
            </role-selector-safe>
          </div>
<!--          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_check_book_type.required ? 'has-error' : '']">-->
<!--            <label class="font_normal_body">表册类型</label>-->
<!--            <input type="text" v-show="false" v-model="checkBook.f_check_book_type"-->
<!--                   :value.sync="checkBook.f_check_book_type"-->
<!--                   v-validate:f_check_book_type='{required: true }'>-->
<!--            <v-select :value.sync="checkBook.f_check_book_type" v-model='checkBook.f_check_book_type'-->
<!--                      :value-single="true" style="width: 60%"-->
<!--                      class="select_list select"-->
<!--                      :options='bookTypes' placeholder=''-->
<!--                      close-on-select></v-select>-->
<!--          </div>-->
          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_user_type.required ? 'has-error' : '']">
            <label class="font_normal_body">用户类型</label>
            <input type="text" v-show="false" v-model="checkBook.f_user_type"
                   v-validate:f_user_type='{required: true }'>
            <v-select :value.sync="checkBook.f_user_type" v-model='checkBook.f_user_type'
                      :value-single="true" style="width: 60%"
                      class="select_list select"
                      :options='userTypes' placeholder=''
                      close-on-select></v-select>
          </div>
          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_check_book_name.required ? 'has-error' : '']">
            <label class="font_normal_body">安检册名称</label>
            <input type="text" class="input_search" v-model="checkBook.f_check_book_name"
                   style="width: 60%" v-validate:f_check_book_name='{ required: true }'>
          </div>
          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_last_run_date.required ? 'has-error' : '']">
            <label class="font_normal_body">上次执行日期</label>
            <input v-show="false" type="text" class="input_search" v-model="checkBook.f_last_run_date"
                   v-validate:f_last_run_date='{ required: true }'
                   style="width: 60%"  >
            <datepicker
              :value.sync="checkBook.f_last_run_date"
              style="width: 60%"
              :format="'yyyy-MM-dd'"
              v-model="checkBook.f_last_run_date"
            >
            </datepicker>
          </div>
          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_run_date.required ? 'has-error' : '']">
            <label class="font_normal_body">执行日期</label>
            <input v-show="false" type="text" class="input_search" v-model="checkBook.f_run_date"
                   v-validate:f_run_date='{ required: true }'
                   style="width: 60%"  >
            <datepicker
              :value.sync="checkBook.f_run_date"
              style="width: 60%"
              :format="'yyyy-MM-dd'"
              v-model="checkBook.f_run_date"
            >
            </datepicker>
          </div>
          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_around_time.required ? 'has-error' : '']">
            <label class="font_normal_body">执行周期</label>
            <input type="number" class="input_search" v-model="checkBook.f_around_time"
                   v-validate:f_around_time='{ required: true }'
                   style="width: 60%"  >
          </div>
          <div class="form-group col-sm-6"  style="margin-top: 20px" :class="[$v.f_around_unit.required ? 'has-error' : '']">
            <label class="font_normal_body">周期单位</label>
            <input v-show="false" type="text" class="input_search" v-model="checkBook.f_around_unit"
                   v-validate:f_around_unit='{ required: true }'
                   style="width: 60%"  >
            <v-select :value.sync="checkBook.f_around_unit" v-model='checkBook.f_around_unit'
                      :value-single="true" style="width: 60%"
                      class="select_list select"
                      :options='aroundUnit' placeholder=''
                      close-on-select></v-select>
          </div>


<!--          <div class="form-group col-sm-6"  style="margin-top: 20px">-->
<!--            <label class="font_normal_body">自动下发</label>-->
<!--            <v-select :value.sync="checkBook.f_is_auto" v-model='checkBook.f_is_auto'-->
<!--                      :value-single="true" style="width: 60%"-->
<!--                      class="select_list select"-->
<!--                      :options='autos' placeholder=''-->
<!--                      close-on-select></v-select>-->
<!--          </div>-->
        </div>
      </form>
      <div style="text-align:right;height:auto;margin-top:6px;margin-right: 10%">
        <button @click="confirm()" :disabled="(!$v.valid) " class="button_search">确认</button>
<!--        <button @click="deleteBook()" class="button_delete">删除</button>-->
        <button @click="close()" class="button_clear">取消</button>
      </div>
    </validator>
  </div>
</template>
<script>
import * as Util from "../../../components/Util";
import bus from "../../../bus";
import {HttpResetClass} from 'vue-client'

export default {
  data () {
    return {
      aroundUnit: [{label: '年', value: '年'}, {label: '月', value: '月'}, {label: '日', value: '日'}],
      bookTypes:[{label: '用户', value: '用户'}, {label: '小区', value: '小区'}],
      userTypes:[{label: '民用', value: '民用'}, {label: '非民用', value: '非民用'}],
      autos:[{label: '是', value: '是'}, {label: '否', value: '否'}],
      allUsers:[],
      checkBook: {
        f_check_book_name: '',
        f_run_date: '',
        f_around_time: '',
        f_around_unit: '',
        f_create_date: '',
        f_create_person: '',
        f_user_type:'',
        f_orgid: '',
        f_is_auto:'否',
        f_check_book_type:"用户",
        f_checker_name: '',
        f_checker_id:'',
      }
    }
  },
  props: ['row','showtype'],
  watch: {
    'checkBook.f_checker_name' (val) {
      const user = this.allUsers.filter(item=>item.name==val);
      if(user.length>0){
        this.checkBook.f_checker_id = user[0]["id"]
      }
      console.log("777",this.checkBook)
    },
    row (row) {
      this.changeCheckBook(row)
    }
  },
  ready () {
    // console.log(this.$login.f.orgid)
    this.changeCheckBook(this.row)
    this.checkBook.f_user_type = this.showtype
    console.log('row', JSON.stringify(this.row))
  },
  methods: {
    changeCheckBook (row) {
      console.log('row改变了', row)
      if (row && row.id) {
        console.log('进入修改赋值')
        this.checkBook = row
      }
    },
    deleteBook () {
      console.log('delete', this.row)
    },
    close () {
      console.log('关闭')
      this.$parent.showItem = false
    },
    confirm () {
      if(!this.checkBook.f_check_book_name){
        this.$showMessage('请填写片区名称')
        return
      }
      // if(!this.checkBook.f_check_book_type){
      //   this.$showMessage('请选择表册类型')
      //   return
      // }
      if(!this.checkBook.f_user_type){
        this.$showMessage('请选择用户类型')
        return

      }
      this.checkBook.f_orgid = this.$login.f.orgid
      this.checkBook.f_create_person = this.$login.f.name
      this.checkBook.f_create_date = Util.toStandardDateString()
      console.log('checkbook', this.checkBook)
      let http = new HttpResetClass()
      http.load('POST', 'rs/logic/safeEntity', {data: {tableName: 't_check_book', entityData: this.checkBook}},
        {resolveMsg: '保存片区信息成功', rejectMsg: '保存片区信息失败'}).then(res => {
        if (res.data.code === 200) {
          let dataplan ={
             id:res.data.id,
             f_check_book_name: this.checkBook.f_check_book_name,
             f_checker_name:this.checkBook.f_checker_name,
             f_checker_id:this.checkBook.f_checker_id,
             f_create_person: this.$login.f.name,
             f_orgid: this.$login.f.orgid
          }
          http.load('POST', 'rs/logic/CreatorBookplan', {data:dataplan},{resolveMsg: null, rejectMsg: null})
          this.$showMessage('保存片区信息成功',['confirm']).then(res=>{
            if (res==='confirm'){
              bus.$emit('fresh-main')
            }
          })
        }else{
          this.$showMessage('保存片区信息失败')
        }
      })
    },
    getRes (obj) {
      if (obj.resids) {
        this.checkBook.f_orgid = obj.resids[0]
        console.log('id', this.checkBook.f_orgid)
      }
    },
    getUsers(obj){
      this.allUsers = obj
    }
  }
}
</script>
