<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" :purchaseData="purchaseData" ref="currentCom" v-if="open"></component>
        <div slot="footer" class="dialog-footer" style="text-align: center" v-if="operatorName != 'purchaseDetail'">
          <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 { getPurchase, updatePurchase, approvalPurchase } from "@/api/transaction/purchase";
  import calculate from "./Calculate";
  import confirm from "./Confirm";
  import purchaseDetail from "./PurchaseDetail";
  export default {
    name: "operator-button",
    components:{
      calculate,
      confirm,
      purchaseDetail
    },
    props:{
      operatorName: {
        type: String
      },
      purchaseInfo: {
        type: Object
      }
    },
    created(){
      this.currentTabComponent = this.operatorName;
    },
    watch: {
      open(newVal,oldVal){
        if(newVal){
          this.getPurchaseInfo();
        }
      }
    },
    data(){
        return{
          open: false,
          currentTabComponent: "",
          purchaseData: {}
        }
    },
    methods:{
      getOperatorName(){
        switch(this.operatorName){
          case "purchaseDetail":
            return "详情";
          case "confirm" :
            return "部门长审核";
          case "calculate":
            return "核算部审批";
          default:
            return "";
        }
      },
      getPurchaseInfo(){
        getPurchase(this.purchaseInfo.purchaseId).then(res =>{
          if(res.code == 200){
            this.purchaseData = res.data;
          }
        })
      },
      //重置意见
      resetSuggestion(){
        this.$refs.currentCom.resetSuggestion();
      },
      //提交
      submitSuggestion(){
        if(this.$refs.currentCom.checkParam()){
          return;
        }

        if("calculate" == this.operatorName){
          approvalPurchase(this.$refs.currentCom.submitSuggestion()).then(res =>{
            if(res.code == 200){
              this.open = false;
              this.$emit("getList");
              this.$message.success("审核成功！");
            }else{
              this.$message.error("审核失败！");
            }
          });
          return;
        }

        updatePurchase(this.$refs.currentCom.submitSuggestion()).then(res =>{
          if(res.code == 200){
            this.open = false;
            this.$emit("getList");
            this.$message.success("提交成功！");
          }else{
            this.$message.error("提交失败！");
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
