<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" :tradeData="tradeData" ref="currentCom" v-if="open"></component>
        <div slot="footer" class="dialog-footer" style="text-align: center" v-if="operatorName != 'tradeDetail'">
          <el-button style="width: 150px;border-color: #1890ff;color: #1890ff;" @click="resetSuggestion">重置意见</el-button>
          <el-button type="primary" style="width: 150px" v-if="operatorName == 'evaluate'" @click="reject">驳回</el-button>
          <el-button type="primary" style="width: 150px" @click="submitSuggestion">提交</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import { getProject, updateProject, settlementTrade, settlePendingPayment } from "@/api/transaction/tradeProject.js";
  import tradeDetail from "./TradeDetail";
  import evaluate from "./Evaluate";
  import confirm from "./Confirm";
  import approval from "./Approval";
  import { sendNotice, noticeIsReadByRelationId } from "@/api/system/notice";
  export default {
    name: "operator-button",
    components:{
      evaluate,
      confirm,
      approval,
      tradeDetail
    },
    props:{
      operatorName: {
        type: String
      },
      tradeInfo: {
        type: Object
      }
    },
    created(){
      this.currentTabComponent = this.operatorName;
    },
    data(){
        return{
          open: false,
          currentTabComponent: "",
          tradeData: {}
        }
    },
    methods:{
      getOperatorName(){
        switch(this.operatorName){
          case "tradeDetail":
            return "详情";
          case "evaluate":
            return "经办人评价";
          case "confirm":
            return "买方部门长审核";
          case "approval":
            return "卖方部门长审核";
          case "toPay":
            return "去支付";
          case "settlePendingPayment":
            return "尾款结算"
          default:
            return "";
        }
      },
      openDialog(){
        if(this.operatorName == "toPay"){
          this.toPay();
          return;
        }
        if(this.operatorName == "settlePendingPayment"){
          this.settlePendingPayment();
          return;
        }
        this.getProjectInfo();
      },
      //去支付
      toPay(){
        this.$confirm(
          '是否确认去支付',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            settlementTrade({tradeId:this.tradeInfo.tradeId,tradeStatus:"5"}).then(res =>{
              if(res.code == 200){
                this.$emit("getList");
                this.msgSuccess("支付成功");
              }
            })
          })
          .catch(() => {});

      },
      settlePendingPayment(){
        this.$confirm(
          '是否确认结算尾款',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            settlePendingPayment({tradeId:this.tradeInfo.tradeId}).then(res =>{
              if(res.code == 200){
                this.$emit("getList");
                this.msgSuccess("结算成功");
              }
            })
          })
          .catch(() => {});
      },
      //获取项目信息
      getProjectInfo(){
        getProject(this.tradeInfo.tradeId).then(res =>{
          if(res.code == 200){
            this.tradeData = res.data;
            this.open = true;
          }
        })
      },
      //重置意见
      resetSuggestion(){
        this.$refs.currentCom.resetSuggestion();
      },
      //提交
      submitSuggestion(){
        if(this.$refs.currentCom.checkParam()){
          return;
        }
        if(this.operatorName == "approval"){
          settlementTrade(this.$refs.currentCom.submitSuggestion()).then(res =>{
            if(res.code == 200){
              this.open = false;
              this.$emit("getList");
              this.$message.success("复核成功！");
              if(this.$refs.currentCom.submitSuggestion().tradeStatus == "4"){
                sendNotice({noticeTitle:"您有新交易项目被驳回",noticeType:"1",noticeContent:"您有新交易项目被驳回",userId:this.tradeInfo.applyId,relationType:"1",relationId:this.tradeInfo.tradeId})
              }else{
                noticeIsReadByRelationId({relationId:this.tradeInfo.tradeId});
              }
            }else{
              this.$message.error("复核失败！");
            }
          })
          return;
        }
        let that = this;
        updateProject(this.$refs.currentCom.submitSuggestion()).then(res =>{
          if(res.code == 200){
            this.open = false;
            this.$emit("getList");
            that.$message.success("提交成功！");
            if(this.$refs.currentCom.submitSuggestion().tradeStatus == "1"){
              sendNotice({noticeTitle:"您有新交易项目待确认",noticeType:"1",noticeContent:"您有新交易项目待确认",
                deptId:this.tradeInfo.tradeDeptId,roles:"deptLeader",relationType:"1",relationId:this.tradeInfo.tradeId})
            }

            if(this.$refs.currentCom.submitSuggestion().tradeStatus == "2"){
              sendNotice({noticeTitle:"您有新交易项目待确认",noticeType:"1",noticeContent:"您有新交易项目待确认",
                deptId:this.tradeInfo.applyDeptId,roles:"deptLeader",relationType:"1",relationId:this.tradeInfo.tradeId})
            }

          }else{
            that.$message.error("提交失败！");
          }
        })
      },
      //驳回
      reject(){
        updateProject({tradeId:this.tradeInfo.tradeId,tradeStatus:"4"}).then(res =>{
          if(res.code == 200){
            this.open = false;
            this.$emit("getList");
            this.$message.success("驳回成功！");
            sendNotice({noticeTitle:"您有新交易项目被驳回",noticeType:"1",noticeContent:"您有新交易项目被驳回",userId:this.tradeInfo.applyId,relationType:"1",relationId:this.tradeInfo.tradeId})
          }else{
            this.$message.error("驳回失败！");
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
