<template>
    <div>
        <div class="customer-head">
            <div class="customer-back">
                <el-button type="danger" style="border: none;background: none;color: red" icon="el-icon-arrow-left"
                    @click="goBack(0)">返回列表
                </el-button>
            </div>
            <div class="add-customer-btn">
                <el-button class="el-button--small-yh" style="margin-left: 6px;" type="primary" size="small"
                    @click="submit">保 存
                </el-button>
            </div>
        </div>
        <div style="margin-top: 50px">
            <trade-card title="基础信息">
                <avue-form :option="optionForm" v-model="form" ref="form">
                    <tempalte slot="corpName" slot-scope="{ row }">
                        <dic-select v-model="form.corpName" placeholder="客户" key="id" label="cnName" res="records"
                            url="/blade-los/bcorps/listByType?corpTypeName=客户" :filterable="true" :remote="true"
                            dataName="cnName" @selectChange="dicChange('corpName', $event)"
                            :disabled="form.advanceChargeItemList.length > 0"></dic-select>
                    </tempalte>
                    <tempalte slot="advancePaymentBalance">
                        <el-input-number v-model="form.advancePaymentBalance"
                            @change="dicChange('advancePaymentBalance', form)" :controls="false" placeholder="请输入 预付款余额"
                            size="small" style="width: 100%;"
                            :disabled="form.advanceChargeItemList.length > 0"></el-input-number>
                    </tempalte>
                    <tempalte slot="inOverpayment">
                        <el-input-number v-model="form.inOverpayment" @change="dicChange('inOverpayment', form)"
                            :controls="false" placeholder="请输入 增加金额" size="small" style="width: 100%;"
                            :disabled="form.advanceChargeItemList.length > 0"></el-input-number>
                    </tempalte>
                    <tempalte slot="outOverpaymen">
                        <el-input-number v-model="form.outOverpaymen" @change="dicChange('outOverpaymen', form)"
                            :controls="false" placeholder="请输入 消费金额" size="small" style="width: 100%;"
                            :disabled="form.advanceChargeItemList.length > 0"></el-input-number>
                    </tempalte>
                </avue-form>
            </trade-card>
            <trade-card title="预付款明细">
                <avue-crud :option="option" :data="form.advanceChargeItemList" id="out-table" ref="crud"
                    @selection-change="selectionChange" @resetColumn="resetColumn('crud', 'option', 'optionBack', 400)"
                    @saveColumn="saveColumn('crud', 'option', 'optionBack', 400)">
                    <template slot="menuLeft">
                        <el-button type="primary" size="small" icon="el-icon-plus" @click="addRow">新 增
                        </el-button>
                    </template>
                    <template slot="orderNo" slot-scope="{ row }">
                        <span style="color: #1e9fff" @click="rowEdit(row)">{{ row.orderNo }}</span>
                    </template>
                    <template slot="billNo" slot-scope="{ row }">
                        <span style="color: #1e9fff" @click="rowEdit(row)">{{ row.billNo }}</span>
                    </template>
                    <template slot="menu" slot-scope="{ row, index }">
                        <el-button v-if="row.identifier == 1" size="small" icon="el-icon-edit" type="text"
                            @click="inEdit(row)">{{ row.$cellEdit ?
                                "保存" :
                                "编辑" }}</el-button>
                    </template>
                </avue-crud>
            </trade-card>
        </div>
    </div>
</template>

<script>
import { submit, getDetail } from "@/api/iosBasicData/advanceCharge";
import dicSelect from "@/components/dicSelect/main";

import { bfeesList } from "@/api/iosBasicData/bfees";
import _ from "lodash";
export default {
    name: "detailsPage",

    data() {
        return {
            form: {
                status: '0',
                advanceChargeItemList: []
            },
            optionForm: {
                menuBtn: false,
                span: 8,
                column: [
                    {
                        label: '客户',
                        prop: "corpName",
                        rules: [{
                            required: true,
                            message: " ",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "预付款余额",
                        prop: "advancePaymentBalance",
                    },
                    {
                        label: "增加金额",
                        prop: "inOverpayment",
                    },
                    {
                        label: "消费金额",
                        prop: "outOverpaymen",
                    },
                    {
                        label: '备注',
                        prop: "remarks",
                        type: 'textarea',
                        disabled: false,
                        span: 24,
                        minRows: 2,
                    },
                ]
            },
            option: {},
            optionBack: {
                height: 'auto',
                calcHeight: 30,
                menuWidth: 60,
                border: true,
                index: true,
                addBtn: false,
                viewBtn: false,
                editBtn: false,
                delBtn: false,
                menu: true,
                refreshBtn: false,
                align: 'center',
                column: [
                    {
                        label: "订单号",
                        prop: "orderNo",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "提单号",
                        prop: "billNo",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "金额",
                        prop: "overpayment",
                        type: 'number',
                        controls: false,
                        cell: true,
                        rules: [{
                            required: true,
                            message: " ",
                            trigger: "blur"
                        }],
                        overHidden: true
                    },
                    {
                        label: "收款金额",
                        prop: "amountD",
                        overHidden: true
                    },
                    {
                        label: "付款金额",
                        prop: "amountC",
                        overHidden: true
                    },
                    {
                        label: "消费类型",
                        prop: "overpaymentType",
                        type: 'select',
                        cell: true,
                        dicData: [{
                            label: '增加',
                            value: 0
                        }, {
                            label: '消费',
                            value: 1
                        }],
                        rules: [{
                            required: true,
                            message: " ",
                            trigger: "blur"
                        }],
                        overHidden: true
                    },
                    {
                        label: "来源单号",
                        prop: "srcOrderNo",
                        overHidden: true
                    },
                    {
                        label: "生成标识",
                        prop: "identifier",
                        type: 'select',
                        dicData: [{
                            label: '系统生成',
                            value: 0
                        }, {
                            label: '手动添加',
                            value: 1
                        }],
                        overHidden: true
                    },
                    {
                        label: "生成时间",
                        prop: "createTime",
                        type: "datetime",
                        format: "yyyy-MM-dd HH:mm:ss",
                        valueFormat: "yyyy-MM-dd HH:mm:ss",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "备注",
                        prop: "remarks",
                        overHidden: true
                    }
                ]
            },
        }
    },
    components: {
        dicSelect,
    },
    props: {
        detailData: Object
    },
    async created() {
        this.option = await this.getColumnData(this.getColumnName(400), this.optionBack);
        if (this.detailData.id) {
            this.getDetails(this.detailData.id)
        }
    },
    methods: {
        addRow() {
            this.form.advanceChargeItemList.push({
                $cellEdit: true,
                identifier: 1
            })
        },
        // 编辑
        inEdit(row) {
            if (row.$cellEdit == true) {
                if (!row.overpayment || row.overpaymentType == null) return this.$message.error("请完善明细信息");
                this.$set(row, '$cellEdit', false)
            } else {
                this.$set(row, '$cellEdit', true)
            }

        },
        dicChange(name, row) {
            if (name == 'corpName') {
                if (row) {
                    this.form.corpId = row.id
                } else {
                    this.form.corpId = null
                }
            }
            if (name == 'advancePaymentBalance') {
                row.inOverpayment = _.add(Number(row.advancePaymentBalance ? row.advancePaymentBalance : 0), Number(row.outOverpaymen ? row.outOverpaymen : 0))
            }
            if (name == 'inOverpayment') {
                row.advancePaymentBalance = _.subtract(Number(row.inOverpayment ? row.inOverpayment : 0), Number(row.outOverpaymen ? row.outOverpaymen : 0))
            }
            if (name == 'outOverpaymen') {
                row.inOverpayment = _.add(Number(row.advancePaymentBalance ? row.advancePaymentBalance : 0), Number(row.outOverpaymen ? row.outOverpaymen : 0))
            }
        },
        rowEdit(row) {
            if (row.overpaymentType == 1) {
                if (this.$store.getters.firstSetStatus) {
                    this.$alert("货款收费(T)页面已存在,请关闭货款收费(T)再进行操作", "温馨提示", {
                        confirmButtonText: "确定",
                        type: 'warning',
                        callback: action => {
                        }
                    });
                } else {
                    this.$router.push({
                        path: '/tradeAgency/firstSettlement/index',
                        query: {
                            billNo: row.srcPid
                        },
                    })
                }

            }
            if (row.overpaymentType == 0) {
                if (this.$store.getters.exchangePurStatus) {
                    this.$alert("货款付费(T)页面已存在,请关闭货款付费(T)再进行操作", "温馨提示", {
                        confirmButtonText: "确定",
                        type: 'warning',
                        callback: action => {
                        }
                    });
                } else {
                    this.$router.push({
                        path: '/tradeAgency/exchangePurchasing/index',
                        query: {
                            billNo: row.srcPid
                        },
                    })
                }
            }
        },
        getDetails(id) {
            const loading = this.$loading({
                lock: true,
                text: '加载中',
                spinner: 'el-icon-loading',
                background: 'rgba(255,255,255,0.7)'
            });
            getDetail({ id: id }).then(res => {
                this.form = res.data.data
                this.$refs.crud.dicInit();
            }).finally(() => {
                loading.close()
            })
        },
        submit() {
            for (let row of this.form.advanceChargeItemList) {
                if (row.identifier == 1) {
                    if (!row.overpayment || row.overpaymentType == null) {
                        return this.$message.error("请完善明细信息");
                    }
                }
            }
            this.$refs["form"].validate((valid, done) => {
                done();
                if (valid) {
                    const loading = this.$loading({
                        lock: true,
                        text: '加载中',
                        spinner: 'el-icon-loading',
                        background: 'rgba(255,255,255,0.7)'
                    });
                    submit(this.form).then(res => {
                        this.$message.success("保存成功");
                        this.getDetails(res.data.data.id)
                    }).finally(() => {
                        loading.close();
                    })

                } else {
                    return false;
                }
            });
        },
        //自定义列保存
        async saveColumn(ref, option, optionBack, code) {
            const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
            if (inSave) {
                this.$message.success("保存成功");
                //关闭窗口
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },
        //自定义列重置
        async resetColumn(ref, option, optionBack, code) {
            this[option] = this[optionBack];
            const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
            if (inSave) {
                this.$message.success("重置成功");
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },
        goBack(type) {
            this.$emit("goBack", type);
            this.$emit('updateKey')
        },
    }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
    margin-bottom: 8px !important;
}

::v-deep .el-table .cell {
    padding: 0 2px !important;

    .el-form-item {
        margin-bottom: 0px !important;
    }

}

::v-deep .avue-crud .el-table .el-form-item__label {
    left: -1px;
}

::v-deep#out-table .back-one {
    background: #ecf5ff !important;
}

::v-deep#out-table .back-two {
    background: #ecf5ff !important;
}
</style>
