<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

      <el-form-item :label="`月份`">
        <el-date-picker
          v-model="dataForm.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 label="审批状态">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in stateList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="dataForm.remark" placeholder="备注" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="btn-box">
      <el-button type="primary" plain @click="addOrUpdateHandle(0,1)">新增月初目标</el-button>
      <el-button type="primary" plain  @click="addOrUpdateHandle(0,2)">新增月未总结</el-button>
    </div>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      :cell-style="cellStyle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="ID"></el-table-column>
      <el-table-column prop="departmentName" header-align="center" align="center" label="部门名称"></el-table-column>
      <el-table-column prop="staffName" header-align="center" align="center" label="姓名"></el-table-column>
      <el-table-column prop="monthTime" header-align="center" align="center" label="月份">
        <template slot-scope="scope">
          <div class="conatnt-name">
            <a class="formaterDateCss" @click="handleDateAddOrUpdate(scope.row.id)">{{ scope.row.monthTimeStr  }}</a>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="statusStr" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <div class="conatnt-name">
            <span>{{ scope.row.status | getStatusStr }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="类型">
        <template slot-scope="scope">
          <div class="conatnt-name">
            <span>{{ scope.row.type | getType }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="lastScore" header-align="center" align="center" label="最后得分"></el-table-column>
      <el-table-column prop="allScore" header-align="center" align="center" label="总分"></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注"></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id,3)">详情</el-button>
          <el-button type="text" size="small" v-if="scope.row.status===0" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <date-add-or-update v-if="dateAddOrUpdateVisible" ref="dateAddOrUpdate" @refreshDataList="getDataList"></date-add-or-update>
  </div>
</template>
<script>

import AddOrUpdate from './lzresultrecord-add-or-update'
import DateAddOrUpdate from './date-add-or-update'

import {apiGetResultRecordList, departmentQuery, deleteRecord} from '@/api/api_result'
import {getDataForMonth} from '@/utils'

export default {
  filters: {
    formaterDate (val) {
      var date = new Date(val)
      var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      return date.getFullYear() + '-' + month
    },
    getStatusStr (status) {
      if (status === 0) {
        return '新建'
      } else if (status === 1) {
        return '审批中'
      } else if (status === 2) {
        return '侍审批'
      } else if (status === 3) {
        return '侍提交'
      } else if (status === 4) {
        return '审批通过'
      } else if (status === 5) {
        return '驳回'
      }
      return ''
    },
    getType (value) {
      if (value === 1) {
        return '月初目标'
      } else if (value === 2) {
        return '月未总结'
      }
      return ''
    }
  },
  data () {
    return {
      Datalist: [{
        Month: 1,
        title: '近一个月'
      }, {
        Month: 3,
        title: '近三个月'
      }, {
        Month: 6,
        title: '近半年'
      }, {
        Month: 12,
        title: '近一年'
      }],
      dataForm: {
        key: '',
        value: [],
        department1: '',
        department2: '',
        department3: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      stateList: [
        {value: '0', label: '新建'},
        {value: '1', label: '审批中'},
        {value: '2', label: '拒绝'},
        {value: '3', label: '侍提交'},
        {value: '4', label: '审批通过'}
      ],
      departmentList1: [],
      departmentList2: [],
      departmentList3: [],
      addOrUpdateVisible: false,
      dateAddOrUpdateVisible: false

    }
  },
  computed: {},
  components: {
    AddOrUpdate,
    DateAddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    async handleChangeData (data) {
      this.index = data.Month
      this.title = data.title
      let dataList = getDataForMonth(data.Month)
      this.dataForm.value = dataList
      this.dataForm.beginDate = dataList[0]
      this.dataForm.endDate = dataList[1]
      this.onSubmit()
    },
    async changeDepartment (i) {
      let departmentId = 0
      if (i === '2') {
        departmentId = this.dataForm.department1
      } else if (i === '3') {
        departmentId = this.dataForm.department2
      } else {
        departmentId = this.dataForm.department3
      }
      departmentQuery({
        'departmentId': departmentId
      }).then(data => {
        if (data && data.code === 0) {
          this['departmentList' + i] = data.departmentList
          if (i === '2') {
            this.dataForm['department2'] = ''
            this.dataForm['department3'] = ''
          } else if (i === '3') {
            this.dataForm['department3'] = ''
          }
        }
        this.dataListLoading = false
      })
    },
    cellStyle ({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 5 && row.status === 5) {
        return 'color:red!important;'
      } else if (columnIndex === 5 && row.status === 4) {
        return 'color:blue!important;'
      } else if (columnIndex === 6 && row.type === 2) {
        return 'font-weight:bold!important;'
      } else {
        return ''
      }
    },
    // 获取数据列表
    getDataList () {
      apiGetResultRecordList({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'monthBeginDate': this.dataForm.beginDate,
        'monthEndDate': this.dataForm.endDate,
        'status': this.dataForm.status,
        'department1': this.dataForm.department1,
        'department2': this.dataForm.department2,
        'department3': this.dataForm.department3,
        'name': this.dataForm.name,
        'remark': this.dataForm.remark,
        'isSelf': 1
      }).then(data => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          this.departmentList1 = data.departmentList1
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    changeData (data) {
      this.index = null
      this.title = null
      this.dataForm.beginDate = data[0]
      this.dataForm.endDate = data[1]
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },

    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },

    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    //
    addOrUpdateHandle (id, recordType) {
      this.$router.push({name: 'recorddetail', query: {id: id, recordType: recordType}}, () => {
        this.mainTabsActiveName = this.$route.name
      })
    },
    handleDateAddOrUpdate (id) {
      this.dateAddOrUpdateVisible = true
      this.$nextTick(() => {
        setTimeout(() => {
          this.$refs.dateAddOrUpdate.init(id)
        }, 500)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRecord({
          'ids': ids.join(',')
        }).then(data => {
          if (data && data.code === 0) {
            this.getDataList()
            this.$message({
              message: data.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          }
        })
      })
    }
  }
}
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.contentEdit {
  color: blue;
  cursor: pointer;
}
.btn-box{
  margin-bottom: 10px;
}
.formaterDateCss{
  cursor: pointer;
  color: blue;
}
.formaterDateCss :hover{
  text-decoration: underline;
}



</style>

