<template>
    <div class="financialAllocation">
        <div class="head-title">
            财务配置
        </div>
        <div v-loading="loading" style="background:#fff;padding:20px;margin-top:10px">
            <el-tabs style="margin-top:20px" v-model="activeName" @tab-click="handleClick">
                <el-tab-pane v-for="item in typeList" :key="item.Id" :dataId="item.Id" :label="item.Name" :name="item.Name"></el-tab-pane>
            </el-tabs>
            <div>
                <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="135px" style="width:50%">
                    <el-form-item v-if="type==1" label="账户类型">
                        <el-select @change="SelectIsPublic" v-model="AccountType" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in AccountList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==1" label="账户" prop="AccountId">
                        <el-select @change="SelectAccountType" v-model="addMsg.AccountId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Alias" :value="item.Id" v-for="(item,index) in AccountIdList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==1" label="汇款人" prop="RemitterName">
                        <el-input v-model="addMsg.RemitterName" size="small"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type==2 || type==3 || type==4" label="币种" prop="CurrencyId">
                        <el-select v-model="addMsg.CurrencyId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in CurrencyIddList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==1" label="商品收入费用" prop="IncomeCostTypeId">
                        <el-select v-model="addMsg.IncomeCostTypeId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in costList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==1" label="运费收入费用" prop="FreightCostTypeId">
                        <el-select v-model="addMsg.FreightCostTypeId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in costList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==3" label="退款费用" prop="RefundCostTypeId">
                        <el-select v-model="addMsg.RefundCostTypeId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in costList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==2" label="成本费用" prop="CBCostTypeId">
                        <el-select v-model="addMsg.CBCostTypeId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in costList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==4" label="打款费用" prop="RemitCostTypeId">
                        <el-select v-model="addMsg.RemitCostTypeId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in costList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==2" label="运杂费" prop="ExpensesCostTypeId">
                        <el-select v-model="addMsg.ExpensesCostTypeId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in costList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="流程" prop="WorkFlowId">
                        <el-select v-model="addMsg.WorkFlowId" placeholder="请选择" size="small" filterable>
                            <el-option :label="item.Name" :value="item.Id" v-for="(item,index) in TempLateList" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type==1" label="执行开始时间" prop="StartTime">
                        <el-date-picker
                        size="small"
                        v-model="addMsg.StartTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item v-if="type==1" label="执行周期" prop="IntervalDay">
                        <el-input v-model="addMsg.IntervalDay" size="small"></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div style="margin-top:20px">
            <el-button @click="Save('addMsg')" size="small" type="primary">保存</el-button>
        </div>
    </div>
</template>

<script>
export default {
    name: "financialAllocation",
    data(){
        return{
            loading:false,
            activeName:'收款',
            typeList:[],
            AccountType:'',
            addMsg:{

            },
            rules:{
                AccountType: [
                    { required: true, message: '请选择账户类型', trigger: 'change' }
                ],
                AccountId: [
                    { required: true, message: '请选择账户类型', trigger: 'change' }
                ],
                IntervalDay: [
                    { required: true, message: '请输入执行周期', trigger: 'blur' },
                ],
                RemitterName: [
                    { required: true, message: '请输入汇款人', trigger: 'blur' },
                ],
                CurrencyId: [
                    { required: true, message: '请选择币种', trigger: 'change' }
                ],
                IncomeCostTypeId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                FreightCostTypeId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                RefundCostTypeId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                RemitCostTypeId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                CBCostTypeId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                ExpensesCostTypeId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                WorkFlowId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                StartTime: [
                    { type: 'string', required: true, message: '请选择时间', trigger: 'change' }
                ],
            },
            AccountList:[],
            type:0,
            AccountIdList:[],
            costList:[],
            TempLateList:[],
        }
    },
    created(){
        this.getAccount();
        this.getOrderStatus();
        this.getCurrencyList();
        
    },
    mounted(){},
    methods:{
        Save(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.addMsg.Type=this.type;
                this.loading=true;
                this.apipost("/api/finance/SetFinanceConfigurationInfo", this.addMsg, res => {
                this.loading=false;
                if (res.data.resultCode == 1) {
                    this.Success(res.data.message)
                    this.GetFinance();
                } else {
                    this.Error(res.data.message)
                }

                })
            } else {
                return false;
            }
            });
        },
        getTempLate(){
            let val=1;
            if(this.type==1){
                val=1;
            }else{
                val=2;
            }
            this.apipost("/api/finance/GetFinanceWorkFlowTemplateList",{TempLateClass:val}, res => {
                this.TempLateList=res.data.data;
            })
        },
        getcostList(){
            let val=1;
            if(this.type==1){
                val=1;
            }else{
                val=2;
            }
            this.apipost("/api/finance/GetFinanceCostTypeList",{Type:val}, res => {
                this.costList=res.data.data;
            })
        },
        getCurrencyList(){
            this.apipost("/api/finance/GetFinanceCurrencyList",{}, res => {
                this.CurrencyIddList=res.data.data;
            })
        },
        SelectAccountType(val){
            this.AccountIdList.forEach(item=>{
                if(item.Id==val){
                    this.addMsg.AccountType=item.BankType;
                }
            })
        },
        SelectIsPublic(val){
            this.AccountList.forEach(item=>{
                if(item.Id==val){
                    this.addMsg.IsPublic=item.IsPublic;
                    this.getBankAccountList(val)
                }
            })
        },
        getBankAccountList(val){
            this.apipost("/api/finance/GetBankAccountList",{TypeId:val}, res => {
                this.AccountIdList=res.data.data;
            })
        },
        handleClick(val){
            let dataId=val.$attrs.dataId;
            this.type=dataId;
            this.GetFinance();
            this.getcostList();
            this.getTempLate();
        },
        // 账户类型
        getAccount(){
            this.apipost("/api/finance/GetAccountTypeList",{}, res => {
                this.AccountList=res.data.data;
            })
        },
        getOrderStatus(){
            this.apipost("/api/finance/GetFinanceConfigurationEnumList",{}, res => {
                this.typeList=res.data.data;
                this.type=this.typeList[0].Id;
                this.GetFinance();
                this.getcostList();
                this.getTempLate();

            })
        },
        GetFinance(){
            this.loading=true;
            this.apipost("/api/finance/GetFinanceConfigurationInfo",{Type:this.type}, res => {
                this.loading=false;
                if(res.data.data.Status==2){
                    this.addMsg=res.data.data.FModel;
                    this.AccountType=this.addMsg.TypeId;
                    this.SelectIsPublic(this.AccountType)

                }else{
                    this.AccountType='';
                    this.addMsg={
                        "Id":0,
                        "IsPublic":'',
                        "AccountType":'',
                        "AccountId":'',
                        "RemitterName":"",
                        "CurrencyId":'',
                        "IncomeCostTypeId":'',
                        "FreightCostTypeId":'',
                        "RefundCostTypeId":'',
                        "CBCostTypeId":'',
                        "RemitCostTypeId":'',
                        "WorkFlowId":'',
                        "Type":1,
                        "StartTime":"",
                        "IntervalDay":''
                    }
                }
            })
        },
        
       
    },
};
</script>
<style>

</style>
