<template>
  <div style="flex:1;" class="flex">
    <validator name='v'>
      <form novalidate class="auto form-horizontal select-overspread">
        <div class="row">
          <div class="col-sm-6 form-group form-fix-width">
            <label class="control-label">计划名称</label>
            <input class="form-control" type="text" v-model="model.f_plan_name"  placeholder="计划名称(必填)" v-validate:f_plan_name='{ required: true }'>
          </div>
          <div class="col-sm-6  form-group form-fix-width">
            <label class="control-label">计划年份</label>
            <v-select id="c_plan_year" :value.sync="f_plan_year"
                      :options='c_year_list' close-on-select clear-button> </v-select>
          </div>
          <div class="col-sm-6  form-group form-fix-width">
            <label class="control-label">计划月份</label>
            <v-select id="c_plan_month" :value.sync="f_plan_month"
                      :options='c_month_list' close-on-select clear-button> </v-select>
          </div>
          <div class="col-sm-6 form-group form-fix-width">
            <label class="control-label">安检员</label>
            <v-select id="checker" :value.sync="f_checker"
                      :options='checkers' close-on-select clear-button> </v-select>
          </div>
          <div class="col-sm-12 form-group text-right">
            <button class="btn btn-success width-80" type="button" v-if ='$v.valid && f_checker' @click="confirm()">保存</button>
          </div>
        </div>
      </form>
    </validator>
    <div class="span">
      <data-grid :model="model" partial='list' v-ref:grid >
        <template partial='head'>
          <tr>
            <th><nobr>序号</nobr></th>
            <th><nobr>用户名称</nobr></th>
            <th><nobr>小区名称</nobr></th>
            <th><nobr>地址</nobr></th>
            <th><nobr>电话</nobr></th>
            <!--<th>最后安检状态</th>-->
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center">{{$index+1}}</td>
          <td style="text-align: center">{{row.f_username}}</td>
          <td style="text-align: center">{{row.f_districtname}}</td>
          <td style="text-align: center">{{row.f_address}}</td>
          <td style="text-align: center">{{row.f_phone}}</td>
          <!--<td>{{row.f_last_check_state}}</td>-->
        </template>
      </data-grid>
    </div>
    <span>共{{ model.rows.length}}项</span>
  </div>
</template>

<script>
import co from 'co'
import Vue from 'vue'
import * as Util from '../Util'

let saveGen = function * (self) {
  self.model.f_plan_type = '集中安检'
  self.model.f_issued = '否'
  self.model.f_filialeids = self.$login.f.f_orgids
  self.model.f_orgstr = self.$login.f.orgpathstr
  self.model.f_plan_year = self.f_plan_year[0]
  self.model.f_plan_month = self.f_plan_month[0]
  for (var i in self.model.rows) {
    self.model.rows[i].f_state = '未检'
    self.model.rows[i].f_upload_state = '未传'
  }
  yield self.$resetpost('rs/logic/GenCallInPlan', {data: self.model}).then((respanse)=>{
    console.log(respanse)
    if(respanse.data.code == 200){
      alert("集中安检生成成功!")
    }else{
      alert("集中安检生成失败!")
    }
  })
}
let select = function * (self) {
  let getGasman = yield self.$resetpost('/rs/search', {
    source: 'this.getParentByType($organization$).getChildByName($安检员$).getUsers()',
    userid: self.$login.f.id
  }, {resolveMsg: null, rejectMsg: null})
  console.log('获取安检员', getGasman)
  getGasman.data.forEach((checker)=>{
    self.checkers.push({label:checker.name,value:checker})
  })
}
export default {
  title: '集中安检',
  data () {
    let options = []
    let year = (new Date()).getFullYear()
    for(let i = year ; i <= year +3; i++ )
      options.push({label: i+'', value: i+''})
    let moptions = []
    for(let i=1; i<=12; i++)
      moptions.push({label: i + '', value: i + ''})
    let model = {rows :[]}
    model.f_subcompany = this.$login.f.f_fengongsi
    return {
      f_checker:{},
      checkers:[],
      chosenOrg: {id:0},
      f:this.$login.f,
      model: model,
      f_checker_chosen : false,
      f_plan_year: year + '',
      f_plan_month: moptions[new Date().getMonth()].value,
      c_year_list: options,
      c_month_list: moptions
    }
  },
ready(){
  let gen = select(this)
  return co(gen)
},
  methods: {
    confirm () {
      if(!this.model.f_checker_id) {
        window.alert('请选择安检员！')
        return
      }
      if(this.model.rows.length==0) {
        window.alert('请添加安检用户！')
        return
      }
      let gen = saveGen(this)
      return co(gen)
    }
  },
  watch:{
    'f_checker'(){
      console.log("f_checker",this.f_checker)
     this.model.f_checker = this.f_checker[0].name
      this.model.f_checker_id = this.f_checker[0].id
    }
  },
  computed: {
    selected () {
      return this.$refs.grid.selected
    }
  }
}
</script>
