<!--  -->
<template>
  <div>
    <div class="staff-archives-title">条件筛选</div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline staff-archives-form">
      <el-form-item label="选择部门">
        <el-input
          placeholder="请选择部门"
          @focus="isChoose=true"
          readonly
          :value="formInline.departmentName"
        ></el-input>
      </el-form-item>

      <el-form-item :label="`选择时间段\n(仅对员工数量生效)`">
        <el-date-picker
          v-model="formInline.value"
          type="daterange"
          align="right"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeData"
        ></el-date-picker>
        <div>
          <el-button
            v-for="i in Datalist"
            :key="i.Month"
            :type="index==i.Month?'primary':'info'"
            size="mini"
            @click="handleChangeData(i)"
          >{{i.title}}</el-button>
        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="restValue">重置</el-button>
      </el-form-item>
    </el-form>

    <el-dialog title="选择部门" :visible.sync="isChoose" width="30%">
      <el-tree :data="menuList" :props="defaultProps" @node-click="handleNodeClick">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ data.departmentName }}</span>
          <span>({{ data.memberCount }})人</span>
        </span>
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isChoose = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="onsumbit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { apiOrganizationList } from '@/api/api_staff'
import { getDataForMonth } from '@/utils'
export default {
  data () {
    return {
      Datalist: [{
        Month: 1,
        title: '近一个月'
      }, {
        Month: 3,
        title: '近三个月'
      }, {
        Month: 6,
        title: '近半年'
      }, {
        Month: 12,
        title: '近一年'
      }],
      index: null,
      title: null,
      value2: [],
      isChooseObj: {},
      isChoose: false,
      menuList: [],
      defaultProps: {
        children: 'list',
        label: 'name'
      },
      formInline: {
        value: []
      }
    }
  },
  computed: {},
  beforeMount () { },
  created () {
    this.handleGetMenuList()
    this.handleChangeData({
      Month: 3,
      title: '近三个月'
    })
  },
  mounted () { },
  methods: {
    async  handleChangeData (data) {
      this.index = data.Month
      this.title = data.title
      let dataList = getDataForMonth(data.Month)
      this.formInline.value = dataList
      this.formInline.beginDate = dataList[0]
      this.formInline.endDate = dataList[1]
      this.onSubmit()
    },
    changeData (data) {
      this.index = null
      this.title = null
      this.formInline.beginDate = data[0]
      this.formInline.endDate = data[1]
    },
    // 获取侧边架构列表
    async handleGetMenuList () {
      let result = await apiOrganizationList()
      this.menuList = result
    },
    onSubmit () {
      let obj = {
        departmentId: this.formInline.departmentId,
        beginDate: this.formInline.beginDate,
        endDate: this.formInline.endDate
      }
      let monthLetter = {
        index: this.index,
        title: this.title
      }
      this.$emit('submit', obj, monthLetter)
    },
    restValue () {
      this.formInline = {}
      this.index = null
      this.title = null
    },
    onsumbit () {
      this.formInline = Object.assign({}, this.formInline, this.isChooseObj)
      this.isChoose = false
    },
    handleNodeClick (a, b) {
      this.isChooseObj = {
        departmentId: a.departmentId,
        departmentName: a.departmentName
      }
    }
  },
  watch: {}

}
</script>

<style lang='scss'>
.el-range-separator {
  width: 7% !important;
}
.el-input__icon {
  transition: all 0.5s;
}
.staff-archives-choose {
  margin: 2px;
  height: 200px;
  overflow: auto;
  transition: all 0.3s;
  position: relative;
  border: 1px solid #dcdfe6;
}
.staff-archives-choose1 {
  height: 0px;
  opacity: 0;
  overflow: hidden;
}
</style>
