<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-4 form-group form-fix-width">
       	<label class="control-label">计划名称</label>
          <input class="form-control" readonly='readonly' id="f_plan_name1" maxlength="200"
           v-model="item.f_plan_name"  v-validate:f_plan_name='{ required: true }'>
       </div>
        <div class="col-sm-4 form-group form-fix-width">
          <label  class="control-label">是否下发</label>
            <v-select :value.sync="f_issued"
                :options='selects' placeholder='请选择'
                close-on-select clear-button></v-select>
        </div>
        <div class="col-sm-2 form-group form-fix-width">
           <button type="button" class="btn btn-success" v-if ="$v.valid && f_issued ==  '是'" @click="confirm()">保存</button>
        </div>
      </div>
    </form>
      <div class="span">
        <data-grid :model="model" partial='list' v-ref:grid >
          <template partial='head'>
            <tr>
              <th style="text-align: center">用户名</th>
              <th style="text-align: center">用户类型</th>
              <th style="text-align: center">小区名称</th>
              <th style="text-align: center">地址</th>
            </tr>
          </template>
          <template partial='body' >
            <td>{{row.f_user_name}}</td>
            <td>{{row.f_user_type}}</td>
            <td>{{row.f_residential_area}}</td>
            <td>{{row.f_address}}</td>
          </template>
        </data-grid>
    </div>
      <span>共{{ model.rows.length}}项</span>
    </validator>
  </div>
</template>

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

let saveGen = function * (self) {
  console.log('进来了')
  self.model.id = self.item.id
  self.model.f_issued = self.f_issued[0]
  self.model.f_issue_time = self.$login.toStandardTimeString()
  yield self.$resetpost('api/af-safecheck/logic/BespeakCheckPlan', {data: self.model}, {resolveMsg: '下发计划成功！', rejectMsg: '下发计划失败！'}).then((respanse)=>{
    if(respanse.data.code == 200){
      window.alert("预约计划下发成功!")
    }else{
      window.alert("预约计划下发失败!")
    }
  })
}

let fetchItems = function * (self) {
  let f_plan_id = "f_plan_id='" + self.item.id + "'"
  let plan = yield self.$resetpost('api/af-safecheck/sql/预约计划下发详情',
  { data: {items: '*', tablename: 't_check_plan_item', condition: f_plan_id, groupitem: '', orderitem: 'f_userinfoid'} },
  { resolveMsg: null, rejectMsg: null })
  self.model.rows = plan.data
}

export default {
  title: '项目编写',
  data () {
    return {
      model: {
        rows: []
      },
      f_issued: '',
      selects: [
        {label: '是', value: '是'},
        {label: '否', value: '否'}
      ]
    }
  },
  props: [ 'item' ],
  methods: {
    confirm () {
      let gen = saveGen(this)
      co(gen)
    }
  },
  watch: {
    'item' (val) {
      if (this.item) {
        this.f_issued = [this.item.f_issued]
        let ge = fetchItems(this)
        co(ge)
      }
    }
  }
}
</script>
