<template>
  <div>
    <form class="form-inline">
      <div class="row form-group">
        <div class="col-sm-4">
          <label class="font_normal_body" for="c_plan_year">年份：</label>
          <v-select id="c_plan_year" :value.sync="c_plan_year"
                    class="select_list select"
                    style="width: 60%"
                    :options='c_year_list' close-on-select clear-button></v-select>
        </div>
        <div class="col-sm-4">
          <label class="font_normal_body" for="c_plan_month">月份：</label>
          <v-select id="c_plan_month" :value.sync="c_plan_month"
                    class="select_list select"
                    style="width: 60%"
                    :options='c_month_list' close-on-select clear-button></v-select>
        </div>
        <div class="col-sm-4">
          <label class="font_normal_body">人员:</label>
          <v-select id="checker" :value.sync="model.f_checker_id" placeholder='安检员'
                    :value-single="true"
                    class="select_list select"
                    style="width: 60%"
                    :options='checkers' close-on-select clear-button> </v-select>
        </div>
      </div>
      <div style="width: 65%;height:100%; display: inline-block">
      </div>
      <button type="button" class="button_search" @click="search()">查询</button>
    </form>
    <hr>
    <table class="table table_sy">
      <thead>
      <tr>
        <th></th>
        <th>计划名称</th>
        <th>计划类型</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="row in model.rows">
        <td @click="select(row, $index)">{{row.selected ? '☆' : ''}}</td>
        <td @click="select(row, $index)">{{row.f_plan_name}}</td>
        <td @click="select(row, $index)">{{row.f_plan_type}}</td>
      </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
  import { PagedList } from 'vue-client'
  import Vue from 'vue'
  import co from 'co'
  import { HttpResetClass } from 'vue-client'

  let select = function * (self) {
    let http = new HttpResetClass()
    http.load('POST', '/rs/search', {
        source: 'this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))',
        userid: self.$login.f.id
      }, {resolveMsg: null, rejectMsg: null}).then((res) => {
      console.log('获取安检员', res)
      res.data.forEach((checker)=>{
        self.checkers.push({label:checker.name,value:checker.id})
      })
      self.checkers.push({label:'全部',value:''})
    })
  }


  export default {
    title: '选择安检计划(未整合)',
    props: ['f','safetype'],
    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('api/af-safecheck/sql/按年月人员提取安检计划TS', 20,
        {f_checker_id: 'this.f_checker_id', f_plan_year: 'this.f_plan_year', f_plan_month: 'this.f_plan_month',safecheckType:'this.safecheckType',f_filialeid:`${this.$login.f.orgid}`})
      model.f_checker_id = ''
      return {
        checker:'',
        checkers:[],
        c_plan_year: [year + ''],
        c_year_list: options,
        c_month_list: moptions,
        c_plan_month: [moptions[new Date().getMonth()].value],
        model: model,
        selectedRow: {}
      }
    },
    ready () {
      this.model.f_plan_year = this.c_plan_year[0]
      this.model.f_plan_month = this.c_plan_month[0]
      this.model.f_subcompany = Vue.f_subcompany
      if(this.safetype){
        this.model.safecheckType=this.safetype
      }else{
        this.model.safecheckType="''"
      }
      let gen = select(this)
      return co(gen)
    },
    watch: {
      'c_plan_year' (val) {
        this.model.f_plan_year = val[0]
      },
      'c_plan_month' (val) {
        this.model.f_plan_month = val[0]
      },
      'checker'(){
        console.log("f_checker",this.checker)
        this.selectedRow = {}
      }
    },
    methods: {
      search () {
        this.model.rows = []
        this.model.rows = []
        this.model.search()
      },
      select(row, idx) {
        if(this.selectedRow)
          this.$set('model.rows[' + this.selectedRow.idx + '].selected', false)
        this.selectedRow = row
        this.selectedRow.idx = idx
        this.$set('model.rows[' + idx + '].selected', true)
      }
    }
  }
</script>
