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

      <el-form-item label="一级部门"  >
        <el-select v-model="dataForm.department1" :disabled="departmentReaderOnly" @change="changeDepartment('2')">
          <el-option
            v-for="item in departmentList1"
            :key="item.departmentId"
            :label="item.departmentName"
            :value="item.departmentId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级部门">
        <el-select v-model="dataForm.department2" :disabled="departmentReaderOnly" placeholder="请选择" @change="changeDepartment('3')">
          <el-option
            v-for="item in departmentList2"
            :key="item.departmentId"
            :label="item.departmentName"
            :value="item.departmentId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级部门">
        <el-select v-model="dataForm.department3" :disabled="departmentReaderOnly" placeholder="请选择">
          <el-option
            v-for="item in departmentList3"
            :key="item.departmentId"
            :label="item.departmentName"
            :value="item.departmentId"
          ></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="姓名">
        <el-input v-model="dataForm.name" placeholder="员工姓名" clearable></el-input>
      </el-form-item>

      <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-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>
      <el-button type="primary" style="margin-bottom: 10px" @click="submitBt()" v-if="isHiden">批量提交</el-button>
    </div>

    <el-tabs type="border-card" v-model="tabValue" @tab-click="tabChange">
      <el-tab-pane label="审批中" name="1,2,5"></el-tab-pane>
      <el-tab-pane label="全部" name=""></el-tab-pane>
      <el-tab-pane label="侍提交" name="3"></el-tab-pane>
      <el-tab-pane label="审批通过" name="4"></el-tab-pane>

      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading" fit highlight-current-row
        @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">
              <span>{{ scope.row.monthTime | formaterDate }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column  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="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" v-if="scope.row.status !== 1" @click="addOrUpdateHandle(scope.row.id,3)">详情</el-button>
            <el-button type="text" size="small" v-if="scope.row.status === 1" @click="addOrUpdateHandle(scope.row.id,3)">审批</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>


    </el-tabs>



    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>



</template>

<script>
import AddOrUpdate from './lzresultrecord-add-or-update'
import {apiGetResultRecordList, departmentQuery, recordIdsSubmit} 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: '',
        status: '1,2,5'
      },
      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,
      recordIdsSelect: [],
      tabValue: '1,2,5',
      isChangeDepartmentrtment: 0,
      isHiden: false,
      departmentReaderOnly: false
    }
  },
  computed: {},
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList(1)
  },
  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()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
      this.recordIdsSelect = []
      for (let i = 0; i < this.multipleSelection.length; i++) {
        this.recordIdsSelect.push(this.multipleSelection[i].goodId)
      }
    },
    alertInfo (msg) {
      this.$message({
        message: msg,
        type: 'success',
        duration: 3000,
        onClose: () => {
        }
      })
    },
    submitBt (msg = '你确定要提交吗？') {
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        recordIdsSubmit({
          'recordIds': this.recordIdsSelect.join(',')
        }).then(data => {
          this.alertInfo(data.msg)
          this.getDataList()
        })
      })
    },
    async changeDepartment (i) {
      this.isChangeDepartmentrtment = 1
      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
      })
    },
    tabChange (row) {
      if (row.name === '3') {
        this.isHiden = true
      } else {
        this.isHiden = false
      }
      this.dataForm.status = row.name
      this.getDataList(row.name)
    },
    // 获取数据列表
    getDataList () {
      apiGetResultRecordList({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'monthBeginDate': this.dataForm.beginDate,
        'monthEndDate': this.dataForm.endDate,
        'statusStr': this.dataForm.status,
        'department1': this.dataForm.department1,
        'department2': this.dataForm.department2,
        'department3': this.dataForm.department3,
        'name': this.dataForm.name,
        'remark': this.dataForm.remark,
        'isSelf': 2,
        'isChangeDepartmentrtment': this.isChangeDepartmentrtment
      }).then(data => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          if (this.isChangeDepartmentrtment === 0) {
            this.departmentList1 = data.departmentList1
            this.departmentList2 = data.departmentList2
            this.departmentList3 = data.departmentList3
            this.dataForm.department1 = data.department1
            this.dataForm.department2 = data.department2
            this.dataForm.department3 = data.department3
            if (!this.stringIsNull(data.department3) || !this.stringIsNull(data.department2)) {
              this.departmentReaderOnly = true
            }
          }
        } 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]
    },
    stringIsNull (str) {
      if (typeof str === 'undefined' || str === null || str === '') {
        return true
      }
      return 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 ''
      }
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },

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

    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
      this.recordIdsSelect = []
      for (let i = 0; i < this.dataListSelections.length; i++) {
        this.recordIdsSelect.push(this.dataListSelections[i].id)
      }
    },
    addOrUpdateHandle (id, recordType) {
      this.$router.push({name: 'recorddetail', query: {id: id, recordType: recordType}}, () => {
        this.mainTabsActiveName = this.$route.name
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/user/lzresultrecord/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>


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

</style>

