<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"
                  :width="'55%'"
                  style="width: 55%"
                  :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"
                  :width="'55%'"
                  style="width: 55%"
                  :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"
                    v-model="model.f_checker_id"
                    :value.sync="model.f_checker_id"
                    placeholder='安检员'
                    :value-single="true"
                    class="select_list select"
                    :width="'55%'"          style="width: 55%"
                    :options='checkers'
                    @change="handleCheckerChange"
                    close-on-select clear-button>
          </v-select>
        </div>
      </div>
      <div class="row form-group">
      <!--<div style="width: 65%;height:100%; display: inline-block">-->
        <div class="col-sm-4">
          <label class="font_normal_body">是否共享:</label>
          <v-select id="share" :value.sync="model.f_is_share" placeholder='是否共享'
                    :value-single="true"
                    class="select_list select"
                    :width="'55%'"
                    style="width: 55%"
                    :options='shares' close-on-select clear-button> </v-select>
        </div>

        <div class="col-sm-4">
          <label class="font_normal_body">计划名称:</label>
          <input type="text" class="input_search" v-model="model.f_plan_name"
                 style="width: 60%" placeholder="计划名称" condition="f_plan_name like '%{}%'">
        </div>
        <div class="col-sm-4">
          <button type="button" class="button_search" @click="search()">查询</button>
        </div>
      </div>
    </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 selectnew = function * (self) {
    let http = new HttpResetClass()
    http.load('POST', '/rs/search', {
        data:{
          source: `this.getSpecialResByType($user$).where(row.getAttributes().get($rolestr$)!=null ).where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1)`,
          userid: self.$login.f.orgid
        }
      }, {resolveMsg: null, rejectMsg: null}).then((res) => {
      self.checkers = []
      let arr = []
      //tag
      res.data.forEach((resRow) => {
        if(!arr.includes(resRow.name) &&  resRow.state == '在职'){
          arr.push(resRow.name)
          self.checkers.push({label:resRow.name,value:resRow.id})
        }
      })
      self.checkers.push({label:'全部',value:''})
    })
  }


  export default {
    title: '选择安检计划',
    props: ['f'],
    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/按年月人员提取安检计划', 20,
        {f_subcompany:'this.f_subcompany',f_plan_name:'this.f_plan_name',f_checker_id: 'this.f_checker_id', f_plan_year: 'this.f_plan_year', f_plan_month: 'this.f_plan_month', f_is_share: 'this.f_is_share',f_filialeid:'this.f_filialeid'})
      model.f_checker_id = ''
      model.f_plan_name=''
      model.f_filialeid = this.$login.f.orgid
      return {
        checker:'',
        checkers:[],
        shares: [{label: '全部', value: ''}, {label: '是', value: '是'}, {label: '否', value: '否'}],
        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 = this.$login.f.orgid
      //tag
      //tag
      //tag
      this.model.f_plan_name=''
      let gen = selectnew(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'(){
        //tag
        this.selectedRow = {}
      }
    },
    methods: {
      handleCheckerChange(value) {
        this.model.f_checker_id = value || ''
      },
      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>
