<template>
    <div style="display: inline-block;margin: 0px 5px">
      <el-button type="text" @click="openDialog" size="mini">{{getOperatorName()}}</el-button>
      <el-dialog :title="getOperatorName()" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" destroy-on-close>
        <component :is="currentTabComponent" :debitData="debitData" ref="currentCom" v-if="open"></component>
        <div slot="footer" class="dialog-footer" style="text-align: center" v-if="operatorName != 'debitDetail' && operatorName != 'repay'">
          <el-button style="width: 150px;border-color: #1890ff;color: #1890ff;" @click="resetSuggestion">重置意见</el-button>
          <el-button type="primary" style="width: 150px" @click="submitSuggestion">提交</el-button>
        </div>
        <div slot="footer" class="dialog-footer" style="text-align: center" v-if="operatorName == 'repay'">
          <el-button type="primary" style="width: 150px" @click="submitSuggestion">确认还款</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import { getCredit, updateCredit, settlementDebitCredit, repayDebitCredit } from "@/api/debit/credit";
  import debitDetail from "./DebitDetail";
  import confirm from "./Confirm";
  import calculate from "./Calculate";
  import repay from "./Repay"
  import { sendNotice,noticeIsReadByRelationId } from "@/api/system/notice";
  export default {
    name: "operator-button",
    components:{
      debitDetail,
      confirm,
      calculate,
      repay
    },
    props:{
      operatorName: {
        type: String
      },
      debitInfo: {
        type: Object
      }
    },
    created(){
      this.currentTabComponent = this.operatorName;
    },
    data(){
        return{
          open: false,
          currentTabComponent: "",
          debitData: {}
        }
    },
    methods:{
      getOperatorName(){
        switch(this.operatorName){
          case "debitDetail":
            return "详情";
          case "confirm":
            return "出借部长确认";
          case "calculate":
            return "核算部审批";
          case "repay":
            return "还款"
          default:
            return "";
        }
      },
      openDialog(){
        getCredit(this.debitInfo.debitId).then(res =>{
          if(res.code == 200){
            this.debitData = res.data;
            this.open = true;
          }
        })
      },
      //重置意见
      resetSuggestion(){
        this.$refs.currentCom.resetSuggestion();
      },
      //提交
      submitSuggestion(){
        if(this.$refs.currentCom.checkParam()){
          return;
        }
        if(this.debitData.debitStatus == "1"){
          settlementDebitCredit(this.$refs.currentCom.submitSuggestion()).then(res =>{
            if(this.$refs.currentCom.submitSuggestion().debitStatus == '4'){
              sendNotice({noticeTitle:"您的借贷申请被驳回",noticeType:"1",noticeContent:"您的借贷申请被驳回",userId:this.debitInfo.operatorId,relationType:"4",relationId:this.debitInfo.debitId});
            }else{
              noticeIsReadByRelationId({relationId:this.debitInfo.debitId});
            }
            this.dealResponse(res);
          })
          return;
        }

        if(this.debitData.debitStatus == "2"){
          repayDebitCredit(this.$refs.currentCom.submitSuggestion()).then(res =>{
            this.dealResponse(res);
          })
          return;
        }
        updateCredit(this.$refs.currentCom.submitSuggestion()).then(res =>{
          this.dealResponse(res);
          sendNotice({noticeTitle:"您有新借贷待审核",noticeType:"1",noticeContent:"您有新借贷待审核",roles:"calculator",relationType:"4",relationId:this.debitInfo.debitId})
        })

      },
      //返回处理
      dealResponse(res){
        if(res.code == 200){
          this.open = false;
          this.$emit("getList");
          this.$message.success("提交成功！");
        }else{
          this.$message.error("提交失败！");
        }
      },
    }
  }
</script>

<style scoped>

</style>
