<template>
    <div>
      <el-button type="text" @click="open=true" 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" @click="submitSuggestion">提交</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import { getProject, updateProject } from "@/api/transaction/tradeProject.js";
  import tradeDetail from "./TradeDetail";
  import evaluate from "./Evaluate";
  import confirm from "./Confirm";
  import approval from "./Approval";
  export default {
    name: "operator-button",
    components:{
      evaluate,
      confirm,
      approval,
      tradeDetail
    },
    props:{
      operatorName: {
        type: String
      },
      tradeInfo: {
        type: Object
      }
    },
    created(){
      this.currentTabComponent = this.operatorName;
      this.getTradeInfo();
    },
    data(){
        return{
          open: false,
          currentTabComponent: "",
          tradeData: {}
        }
    },
    methods:{
      getOperatorName(){
        switch(this.operatorName){
          case "tradeDetail":
            return "详情";
          case "evaluate":
            return "经办人评价";
          case "confirm":
            return "买方部门长确认";
          case "approval":
            return "卖方部门长审核";
          default:
            return "";
        }
      },
      getTradeInfo(){
        getProject(this.tradeInfo.tradeId).then(res =>{
          if(res.code == 200){
            this.tradeData = res.data;
          }
        })
      },
      //重置意见
      resetSuggestion(){
        this.$refs.currentCom.resetSuggestion();
      },
      //提交
      submitSuggestion(){
        if(this.$refs.currentCom.checkParam()){
          return;
        }
        let that = this;
        updateProject(this.$refs.currentCom.submitSuggestion()).then(res =>{
          if(res.code == 200){
            this.open = false;
            this.$emit("getList");
            that.$message.success("提交成功！");
          }else{
            that.$message.error("提交失败！");
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
