<template>
  <div class="flex">
    <form novalidate class="form-inline">
        <div class="form-group">
          <datepicker id="startdate"
            v-model="model.f_date"
            :value.sync="model.f_date"
                      placeholder="时间"
            :disabled-days-of-Week="[]"
            :format="'yyyy-MM-dd'">
          </datepicker>
        </div>
        <div class="form-group">
          <input type="time"  v-model="model.upTime" placeholder="上班时间">
        </div>
      <div class="form-group">
        <input type="time" v-model="model.downTime" placeholder="下班时间">
      </div>
      <div class="form-group">
        <right-tree :userid.sync='userid' :source.sync='source' @re-res="getRes"></right-tree>
        {{model.chosenOrg.fullName}}
      </div>
      <div class="form-group">
        <button type="button" class="btn btn-success width-80" @click="confirm()">查询</button>
      </div>
    </form>
    <div class="span">
      <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>
          </tr>
        </template>
        <template partial='body'>
            <td>{{$index+1}}</td>
            <td>{{row.f_date}}</td>
            <td>{{row.f_user_name}}</td>
            <td>{{row.f_signin_time}}</td>
            <td>{{row.f_knockoff_time}}</td>
            <td>{{row.f_state}}</td>
        </template>
      </data-grid>
    </div>
  </div>
</template>

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

let search = function * (self) {
  let state = yield self.$resetpost('api/af-safecheck/sql/考勤查询', {data: {org_id: self.model.chosenOrg.id, f_date: self.model.f_date}}, {resolveMsg: null, rejectMsg: null})
  self.model.rows = state.data

  for (var row of self.model.rows) {
    Util.getAttendanceState(row, self.model.upTime, self.model.downTime)
  }
}

export default {
  title: '上下班签到管理',
  data () {
    return {
      userid: this.$login.f.id,
      source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
      model: {
        rows: [],
        f_date: '',
        upTime: '08:00:00',
        downTime: '18:00:00',
        chosenOrg: {
          id: 0
        },
      }
    }
  },
  methods: {
    confirm () {
      let gen = search(this)
      return co(gen)
    },
    getRes(obj) {
      console.log("obj",obj)
      this.model.chosenOrg.fullName = obj.res
      this.model.chosenOrg.id = obj.resids
    },
  },
  ready () {
    console.log("f",this.$login.f)
    this.model.f_date = Util.toStandardDateString()
  }
}
</script>
