<template>
    <el-dialog :visible.sync="dialogVisible"    width="80%" :before-close="()=>{
    $emit('update:dialogVisible',false)
  }">
    <div class="fee-application">
      <h1>{{$t('费用申请')}}-{{orderDetails.orderNo}}
      </h1>
      <el-divider></el-divider>
      <el-form label-width="100px" inline>
        <el-form-item :label="$t('订单号')+':'"><div class="content">
          {{orderDetails.orderNo}}
        </div></el-form-item>
        <el-form-item :label="$t('发货人')+'：'"><div class="content">{{orderDetails.consignorVO ? orderDetails.consignorVO.name :''}}</div></el-form-item>
        <el-form-item :label="$t('唛头')"><div class="content">{{orderDetails.marks}}</div></el-form-item>
        <el-form-item> <el-button @click="addCost">{{$t('添加申请')}}</el-button></el-form-item>
      </el-form>
      <el-table :data="list">
         <el-table-column :label="$t('序号')" type="index"></el-table-column>
         <el-table-column :label="$t('费用类型')">
           <template  v-slot:default = "scope">
             <dict-selector  :disabled="isModify[forbidden(scope.row)]"  :type="DICT_TYPE.FEE_TYPE" v-model="scope.row.feeType" />
           </template>
         </el-table-column>
         <el-table-column :label="$t('金额')">
           <template v-slot:default = 'scope'>
             <el-input  :disabled="isModify[forbidden(scope.row)]" v-model.number="scope.row.applicationFee" ></el-input>
           </template>
         </el-table-column>
         <el-table-column :label="$t('货币类型')">
           <template v-slot:default = 'scope'>
             <el-select   :disabled="isModify[forbidden(scope.row)]"  v-model="scope.row.applicationFeeCurrency">
               <el-option v-for="item in JSON.parse(currencys)" :key="item.id" :label="item.titleZh" :value="item.id" />
             </el-select>
           </template>
         </el-table-column>
         <el-table-column :label="$t('付款类型')">
           <template v-slot = {row}>
             <dict-selector  :disabled="isModify[forbidden(row)]" :type="DICT_TYPE.PAYMENT_TYPE" v-model="row.payType" />
           </template>
         </el-table-column>
         <el-table-column :label="$t('备注')">
           <template v-slot:default="scope">
             <el-input  :disabled="isModify[forbidden(scope.row)]" v-model="scope.row.remarks" type="textarea"></el-input>
           </template>
         </el-table-column>
         <el-table-column :label="$t('确认收款')">
           <template v-slot:default ="scope">
             <dict-tag :type="DICT_TYPE.ECW_RECEIVABLE_STATE" :value="scope.row.receiveFlag" />
           </template>
         </el-table-column>
         <el-table-column :label="$t('申请人')">
           <template v-slot:default ="scope">
             {{scope.row.status === 0 ? $t('未提交'): scope.row.applicationAuthor}}
           </template>
         </el-table-column>
         <el-table-column :label="$t('操作')">
           <template v-slot:default = 'scope'>
             <el-button type="text" v-if="scope.row.status !== 0&&scope.row.status !== 1">{{STATUS[scope.row.status]}}</el-button>
             <el-button type="text" v-if="scope.row.status == 1" @click="examineFn">{{$t('审核中')}}</el-button>
             <el-button type="text" v-if="scope.row.status === 2" @click="modify(scope.row)">{{$t('修改')}}</el-button>
           </template>
         </el-table-column>
      </el-table>
      <div style="padding: 20px">
        <work-flow xmlkey="free_apply" v-model="selectedUsers"  />
      </div>
      <div style="text-align: center;margin-top: 20px;">
        <el-button v-if="!IsExamine" style="margin-right: 30px;" @click="submit">{{$t('提交')}}</el-button>
        <el-button v-if="IsExamine" style="margin-right: 30px;" @click="examineFn">{{$t('审核中')}}</el-button>
        <el-button v-if="IsExamine" style="margin-right: 30px;" @click="cancel">{{$t('取消审核')}}</el-button>
        <el-button @click="$emit('update:dialogVisible',false)">{{$t('返回')}}</el-button>
      </div>
    </div>
  </el-dialog>

</template>

<script>
import {
  getOrder,
  feeApplicationCreate,
  ApplicationListByOrderId,
  applicationUpdate,
  feeApplicationListByOrderId, feeApplicationCancel
} from "@/api/ecw/order";
import { getDictDatas, DICT_TYPE } from '@/utils/dict';
import Template from "@/views/cms/template";
import workFlow from "@/components/WorkFlow";
export default {
  name: "feeApplication",
  components: {Template,workFlow},
  props:{
    orderId:[Number, String],
    dialogVisible:{
      type:Boolean,
      default:false,
    },
    currencys:String
  },
  data(){
    return {
      orderDetails:{},
      list:[],
      DICT_TYPE,
      getDictDatas,
      STATUS:{},
      isModify:[],
      isModifyIf:false,
      selectedUsers:[]
    }
  },
  created() {
    this.getDictDatas(this.DICT_TYPE.AUDIT_STATUS).forEach( e =>{
      this.STATUS[e.value] = e.label
    })

  },
  computed:{
    forbidden(){
      return (row)=>{
        return this.list.findIndex(e => e.id === row.id)
      }
    },
    IsExamine(){
      return this.list.some(e => e.status === 1)
    }
  },
  methods:{
    addCost(){
      if(this.list.some(e => e.status === 0 || e.status === 1)){
        return this.$message.success(this.$t('当前有申请费用为审核中或未提交，请审核后在申请'));
      }
        this.list.push(
          {
            orderId:this.orderId,
            feeType:undefined,//费用申请类型
            applicationFee:undefined,//金额
            applicationFeeCurrency:undefined,//	费用申请货币类型
            remarks:undefined,
            receiveFlag:0,
            applicationAuthor:undefined,
            status:0,
          }
        )
      this.isModify.push(false)
      this.isModifyIf = false;
    },
    submit(){
      if(this.isModifyIf){
        applicationUpdate({...this.list[this.isModify.findIndex(e => e === false)],status:1}).then(r => {
          if(r.code === 0){
            this.$emit('update:dialogVisible',false)
            this.$message.success(this.$t('修改成功'));
          }
        })
      }else {
        if(this.list[this.list.length - 1].status === 0){
          feeApplicationCreate( {...this.list[this.list.length - 1],status:1,copyUserId:this.selectedUsers}).then(r=>{
            if(r.code === 0){
              this.$emit('update:dialogVisible',false)
              this.$message.success(this.$t('添加成功'));
              this.selectedUsers = [];
            }
          })
        }else {
          this.$message.success(this.$t('当前有申请费用为审核中或未提交，请审核后在申请'));
        }
      }
    },
    getOrderList(){
      ApplicationListByOrderId({orderId: this.orderId}).then(r => {
        if(r.code === 0){
          this.list = r.data;
          this.list.forEach(()=>this.isModify.push(true))
          let p = this.list.find(e => e.status === 1)
        }
      })
    },
    modify(row){
      if(this.list.some(i => i.status === 0 || i.status === 1))return this.$message.warning(this.$t('当前有未提交的数据或审核中的数据不能修改'))
      this.isModifyIf = true;
      this.isModify.forEach((item,index)=>{
        this.$set(this.isModify, index, true )
      })
       this.$set(this.isModify, this.list.findIndex(e => e.id === row.id), false )
    },
    examineFn(){
     let item =  this.list.find(e => e.status === 1);
     console.log(item)
      this.$router.push({path:'/bpm/process-instance/detail',query:{id:item.bpmProcessId}})
    },
    cancel(){
      this.$prompt(this.$t('请输入取消原因'), this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
      }).then(({ value }) => {
        let item =  this.list.find(e => e.status === 1);
        feeApplicationCancel({huifu:value,bpmProcessId:item.bpmProcessId}).then(r => {
          this.$message({
            type: 'success',
            message:this.$t('取消成功')
          });
          this.$emit('update:dialogVisible',false)
        })
      }).catch(() => {
      });
    }
  },
  watch:{
    dialogVisible(val){
       if(val){
         this.isModify = []
         this.getOrderList()
         getOrder(this.orderId).then(r =>{
           if(r.code === 0){
             this.orderDetails = r.data
           }
         })
       }
    },
  }
}
</script>

<style scoped lang="scss">
.fee-application {
  padding: 0 20px;
  h1{
    font-weight: 600;
    font-size: 20px;
  }
  .content{
    width: 200px;
  }
}
.my-process-designer {
  height: calc(100vh - 200px);
}

.box-card {
  width: 100%;
  margin-bottom: 20px;
}
</style>
