<template>
  <div>
    <criteria-paged :model="model" pager='false' v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.search' v-ref:criteria>
        <div novalidate class="form-inline auto" partial>
          <div class="form-group">
            <label class="control-label" for="c_plan_year">年份</label>
            <v-select id="c_plan_year" :value.sync="$parent.$parent.model.c_plan_year"
                      :options='$parent.$parent.c_year_list' close-on-select clear-button></v-select>
          </div>
          <div class="form-group">
            <label class="control-label" for="c_plan_month">月份</label>
            <v-select id="c_plan_month" :value.sync="$parent.$parent.model.c_plan_month"
                      :options='$parent.$parent.c_month_list' close-on-select clear-button></v-select>
          </div>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_plan_name" id="f_plan_name"
                   placeholder="计划名称" condition="c.f_plan_name like '%{}%'">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_residential_area" id="f_residential_area"
                   placeholder="小区" condition="f_residential_area like '%{}%'">
          </div>
          <div class="form-group">
            <label class="control-label">安检员</label>
            <v-select :value.sync="model.f_checker_id" v-model='model.f_checker_id'
                      :value-single="true"
                      :options='$parent.$parent.$parent.checkersid' placeholder='安检员'
                      close-on-select
                      condition="c.f_checker_id = '{}'"></v-select>
          </div>
          <div class="form-group">
            <button type="button" class="btn btn-success width-80" @click="search()">查询</button>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>计划名称</th>
            <th>用户编号</th>
            <th>用户名称</th>
            <th>小区名称</th>
            <th>地址</th>
            <th>操作</th>
            <th>标记</th>
          </tr>
        </template>
        <template partial='body'>
          <td>{{$index+1}}</td>
          <td>{{row.f_plan_name}}</td>
          <td>{{row.f_userinfo_code}}</td>
          <td>{{row.f_user_name}}</td>
          <td>{{row.f_residential_area}}</td>
          <td>{{row.f_address}}</td>
          <td><span class="glyphicon glyphicon-trash" @click="$parent.$parent.$parent.delete(row)"></span></td>
          <td>{{row.sign=='1'?'◆':''}}</td>
        </template>
        <template partial="foot">
          <span>共{{ model.count }}项</span>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  import { PagedList } from 'vue-client'
  import * as Util from '../Util'
  import Vue from 'vue'
  import co from  'co'

  let syncDelete = function * (self,row) {
    yield self.$resetpost('rs/logic/deleteCheckPlanItem', {id:row.id },{
      warnMsg: '删除数据不可恢复，确认删除吗？',
      resolveMsg: '恭喜！删除成功！',
      rejectMsg: '删除失败：'
    })
    self.$refs.paged.$refs.criteria.search()
  }

  export default {
    title: '安检员A',
    data () {
      let options = []
      let year = (new Date()).getFullYear()
      for(let i = year - 8; i <= year +1; i++ )
        options.push({label: i+'', value: i+''})
      let moptions = []
      for(let i=1; i<=12; i++)
        moptions.push({label: i + '', value: i + ''})
      let model = new PagedList('rs/sql/根据年月安检员查询安检项', 3000,
        {f_subcompany: 'this.f_subcompany', f_plan_year: 'this.c_plan_year[0]', f_plan_month: 'this.c_plan_month[0]'})
      model.c_plan_year = [year+'']
      model.c_plan_month = [moptions[new Date().getMonth()].value]
      model.chosenOrg = {id:0}
      return {
        checker:this.$login.f,
        leftCheckers:this.$parent.checkers,
        fun:this.$login.f,
        model: model,
        c_year_list: options,
        c_month_list: moptions
      }
    },
    watch: {
      'model.c_plan_year' (val) {
        this.$parent.$refs.right.$refs.paged.$refs.criteria.model.f_plan_year = val[0]
      },
      'model.c_plan_month' (val) {
        this.$parent.$refs.right.$refs.paged.$refs.criteria.model.f_plan_month = val[0]
      },
      'checker'(){
        //tag
        this.model.chosenOrg = this.checker[0]
      }
    },
    methods:{
      search(args ){
        this.model.rows = []
        this.model.count = 0
        this.model.search(args.condition, args.model)
      },
      delete(row){
        let gen = syncDelete(this,row)
        return co(gen)
      }
    },
    ready () {
      this.model.id = this.$login.f.id
      this.model.f_subcompany = this.$login.f.f_fengongsi
    },
    computed: {
      selected () {
        return this.$refs.paged.$refs.grid.selected
      }
    }
  }
</script>
