<template>
  <el-card class="container">
    <template slot="header">
      <el-row class="header" type="flex" justify="space-between" align="middle">
        <el-col :span="4">
          <el-tag effect="plain" size="medium">审批</el-tag>
        </el-col>
        <el-col :span="18">
          <el-tabs v-model="formData.type">
            <el-tab-pane v-for="(item,index) in tabsOption" :key="index" :label="item.label" :name="item.type"/>
          </el-tabs>
        </el-col>
        <el-col :span="2">
          <el-button icon="el-icon-refresh" type="text" :loading="loading" @click="getList" />
        </el-col>
      </el-row>
      <el-form class="header" inline style="text-align: left">
        <template v-if="formData.type !== 'apply'">
          <el-form-item label-width="0">
            <el-select v-model="formData.sourceType">
              <el-option
                v-for="item in reviewSourceType"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label-width="0">
            <el-select v-model="formData.ptId">
              <el-option
                v-for="item in pts"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label-width="0">
            <el-select v-model="formData.targetId" filterable clearable placeholder="受益人">
              <el-option
                v-for="item in employeeMap"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label-width="0">
            <el-select v-model="formData.applyType">
              <el-option
                v-for="(item,index) in applyTypeOption"
                :key="index"
                :label="item.name"
                :value="item.type"
              />
            </el-select>
          </el-form-item>
          <el-form-item label-width="0">
            <el-input
              v-model="formData.keyword"
              placeholder="申请内容"
              maxlength="10"
              clearable
            />
          </el-form-item>
        </template>
      </el-form>
    </template>
    <template slot="default">
      <el-table
        :height="300"
        :max-height="300"
        v-loading="loading"
        :data="list"
        @row-click="openReview"
      >
        <el-table-column
          label="内容"
          align="center"
          prop="remark.customize"
          width="300"
          show-overflow-tooltip
        />
        <el-table-column label="来源" align="center" prop="source_type">
          <template slot-scope="scope">
            <el-tag :type="sourceTypeTag(scope.row.source_type)">{{sourceMap[scope.row.source_type] || '--'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="时间"
          align="center"
          prop="event_time"
        />
      </el-table>
      <center style="padding: 20px 0;">
        <el-pagination
          small
          layout="prev, pager, next"
          :total="formData.total"
          :page-size="formData.pageSize"
          :current-page="formData.page"
          @current-change="p => formData.page = p"
        />
      </center>
    </template>
    <examinePopup :refresh-parent-list="false" style="text-align: left" :title="'审核详情'" :id="currentReviewId" :show.sync="showReview" @hasSubmit="getList"/>
  </el-card>
</template>

<script>
import Template from "../../examine/components/Template.vue";
import examinePopup from "../../point/views/common/examinePopup.vue";
import {_debounce} from "../../utils/auth";

export default {
  name: 'workbenchReview',
  components: {Template,examinePopup},
  data(){
    const tabs = [
      {label:'待审批',type:'waiting'},
      {label:'参与审批',type: 'reviewed'},
      {label:'我申请的',type: 'apply'},
    ]
    const applyType = [{name:'待审批',type:'waiting'},{name:'已通过',type:'complete'},{name:'被驳回',type:'refuse'}]
    const sourceType = [{ name: '审批来源', id: 0 }, { name: '积分任务', id: 1 }, { name: '积分申请', id: 2 }, { name: '积分奖扣', id: 3 }]
    const pts = this.$getTyps()
    pts.unshift({code:'all',id:0,name:'积分类型'})
    return {
      tabsOption:tabs,
      loading:false,
      reviewSourceType:sourceType,
      applyTypeOption:applyType,
      pts:pts,
      employeeMap: this.$getEmployeeMap(),
      formData:{
        type:tabs[0].type,
        ptId:0,
        sourceType:0,
        targetId:null,
        page:1,
        pageSize:5,
        total:0,
        applyType:applyType[0].type,
        keyword:''
      },
      list:[],
      sourceMap:{
        1:'任务',
        2:'申请',
        3:'积分奖扣',
      },
      showReview:false,
      currentReviewId:0,
    }
  },
  watch:{
    'formData.type'(v){
      this.initPageData()
      this.getList()
    },
    'formData.page'(v){
      this.getList()
    },
    'formData.ptId'(v){
      this.initPageData()
      this.getList()
    },
    'formData.sourceType'(v){
      this.initPageData()
      this.getList()
    },
    'formData.targetId'(v){
      this.initPageData()
      this.getList()
    },
    'formData.applyType'(v){
      this.initPageData()
      this.getList()
    },
    'formData.keyword':_debounce(function (val){
      this.initPageData()
      this.getList()
    }),
  },
  methods:{
    initFormData(){
    },
    initPageData(){
      this.formData.page = 1
      this.formData.pageSize = 5
      this.formData.total = 0
    },
    sourceTypeTag(sourceType){
      const map = {
        1:'info',
        2:'success',
        3:'warning',
      }
      return map[sourceType] || 'info'
    },
    getList(){
      switch (this.formData.type){
        case 'apply':
          this.getApplyList()
          break;
        default:
          this.getReviewList()
      }
    },
    getReviewList(){
      let params = {
        type:this.formData.type,
        pt_id:this.formData.ptId,
        source_type:this.formData.sourceType,
        page:this.formData.page,
        page_size:this.formData.pageSize,
      }
      if (this.formData.targetId) params.target_id = this.formData.targetId
      let self = this
      self.loading = true
      self.$axiosUser('get', '/api/pro/integral/review/list', params)
        .then(res => {
          if (res.data.code !== 1) throw new Error(res.data.msg)
          self.list = res.data.data.list
          self.formData.total = res.data.data.total
        })
        .catch(err => {
          self.$message.error(err)
        })
        .finally(() => {
          self.loading = false
        })
    },
    getApplyList(){
      let params = {
        pt_id:this.formData.ptId,
        type:this.formData.applyType,
        page:this.formData.page,
        page_size:this.formData.pageSize,
      }
      if (this.formData.keyword) params.keyword = this.formData.keyword
      let self = this
      self.loading = true
      self.$axiosUser("get","/api/pro/integral/review/apply/list",params)
        .then(res => {
          if (res.data.code !== 1) throw new Error(res.data.msg)
          self.list = res.data.data.list
          self.formData.total = res.data.data.total
        })
        .catch(err => {
          self.$message.error(err)
        })
        .finally(() => {
          self.loading = false
        })
    },
    openReview(row){
      this.currentReviewId = row.id
      this.showReview = true
    }
  },
  mounted() {
    this.getList()
  }
}
</script>

<style scoped>
.container{
  width: 100%;
  height: 100%;
}
.container .header{
  min-height: 50px;
}
::v-deep .el-table tbody tr:hover{
  cursor: pointer;
}

</style>
