<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="backToList(0)">返回列表
                </el-button>
            </div>
            <div class="add-customer-btn">
                <el-button class="el-button--small-yh" style="margin-left: 6px;" type="info" size="small"
                    :disabled="isSave || form.status != 1" @click="inRevoke">撤 销
                </el-button>
                <el-button class="el-button--small-yh" style="margin-left: 6px;" type="success" size="small"
                    :disabled="isSave || form.status != 0 || !form.id" @click="inConfirm">确 认
                </el-button>
                <el-button class="el-button&#45;&#45;small-yh" style="margin-left: 6px;" type="primary" size="small"
                    v-if="isSave" @click="inEdit">编 辑
                </el-button>
                <el-button class="el-button--small-yh" v-else style="margin-left: 6px;" type="primary" size="small"
                    :disabled="isSave" @click="submit">保 存
                </el-button>
            </div>
        </div>
        <div style="margin-top: 50px">
            <trade-card title="基础信息">
                <avue-form :option="optionForm" v-model="form" ref="form">
                </avue-form>
            </trade-card>
        </div>
    </div>
</template>

<script>
import {
    getDetails,
    submit, confirming, revoke
} from "@/api/tirePartsMall/financingManagement/balanceRecharge";
import SearchQuery from "@/components/iosbasic-data/searchquery.vue";
import dicSelect from "@/components/dicSelect/main";
import _ from "lodash";
export default {
    name: "detailsPage",

    data() {
        return {
            isSave: false,
            editButton: false,
            loading: false,
            // 商品列表数据合计
            tableData: [],
            // tabs切换
            data: [],
            isStatus: 1,
            goodsIdoptions: [],
            dataList: [],
            key: 0,
            form: {
                status: 0
            },
            customerData: [], // 客户数据
            optionForm: {
                menuBtn: false,
                span: 8,
                disabled: false,
                column: [
                    {
                        label: '金额',
                        prop: "amount",
                        overHidden: true,
                    },
                    {
                        label: '单号',
                        prop: "sysNo",
                        disabled: true,
                    },
                    {
                        label: '状态',
                        prop: "status",
                        type: 'select',
                        dicData: [{
                            label: '已确认',
                            value: 1
                        }, {
                            label: '录入',
                            value: 0
                        }],
                        disabled: true
                    },
                    {
                        label: '确认人',
                        prop: "confirmingPersonName",
                        search: true,
                        overHidden: true,
                        type: 'select',
                        filterable: true,
                        remote: true,
                        props: {
                            label: 'cname',
                            value: 'cname',
                            res: 'data.records'
                        },
                        dicUrl: '/api/blade-sales-part/corpsDesc/list?current=1&size=20&corpType=GYS&cname={{key}}',
                        disabled: true,
                    },
                    {
                        label: "确认时间",
                        prop: "confirmingPersonDate",
                        type: "date",
                        format: "yyyy-MM-dd",
                        valueFormat: "yyyy-MM-dd HH:mm:ss",
                        disabled: true,
                    },
                    {
                        label: "制单人",
                        prop: "createUserName",
                        disabled: true
                    },
                    {
                        label: "制单日期",
                        prop: "createTime",
                        type: "date",
                        format: "yyyy-MM-dd",
                        valueFormat: "yyyy-MM-dd HH:mm:ss",
                        disabled: true
                    },
                    {
                        label: "更新人",
                        prop: "updateUserName",
                        disabled: true
                    },
                    {
                        label: "更新日期",
                        prop: "updateTime",
                        type: "date",
                        format: "yyyy-MM-dd",
                        valueFormat: "yyyy-MM-dd HH:mm:ss",
                        disabled: true
                    },
                    {
                        label: '备注',
                        prop: "remarks",
                        type: 'textarea',
                        disabled: false,
                        span: 24,
                        minRows: 2
                    }]
            },
            optionContacts: {},
            optionContactsBack: {
                disabled: false,
                border: true,
                align: 'center',
                index: true,
                height: 500,
                addRowBtn: false,
                addBtn: false,
                editBtn: false,
                delBtn: false,
                column: [
                    {
                        label: '来源类型',
                        prop: 'type',
                        overHidden: true,
                    },
                    {
                        label: '来源单号',
                        prop: 'srcNo',
                        overHidden: true,
                    },
                    {
                        label: '金额',
                        prop: 'amount',
                        overHidden: true,
                    },
                    {
                        label: '配资比例',
                        prop: 'proportion',
                        overHidden: true,
                    },
                    {
                        label: "制单人",
                        prop: "createUserName",
                        overHidden: true,
                    },
                    {
                        label: "制单日期",
                        prop: "createTime",
                        type: "date",
                        format: "yyyy-MM-dd",
                        valueFormat: "yyyy-MM-dd HH:mm:ss",
                        overHidden: true,
                    },
                    {
                        label: "更新人",
                        prop: "updateUserName",
                        overHidden: true,
                    },
                    {
                        label: "更新日期",
                        prop: "updateTime",
                        type: "date",
                        format: "yyyy-MM-dd",
                        valueFormat: "yyyy-MM-dd HH:mm:ss",
                        overHidden: true,
                    },
                    {
                        label: "备注",
                        prop: "remarks",
                        overHidden: true,
                    }
                ]
            }
        }
    },
    components: { SearchQuery, dicSelect },
    props: {
        onLoad: Object,
        detailData: Object
    },
    async created() {
        this.optionContacts = await this.getColumnData(this.getColumnName(404), this.optionContactsBack);
        if (this.detailData.id) {
            this.editButton = true
            this.optionForm.disabled = true
            this.isSave = true
            this.getDetail(this.detailData.id)
        }
        if (this.$route.query.srcId) {
            this.editButton = true
            this.optionForm.disabled = true
            this.isSave = true
            this.getDetail(this.$route.query.srcId)
        }
    },
    methods: {
        inEdit() {
            this.editButton = false
            if (this.form.status == 1) {
                this.optionForm.disabled = true
            } else {
                this.optionForm.disabled = false
            }
            this.isSave = false
        },
        dicChange(name, row) {
            if (name == 'confirmingPersonName') {
                if (row) {
                    this.form.confirmingPersonId = row.id
                } else {
                    this.form.confirmingPersonId = null
                }
            }
        },
        //修改提交触发
        submit() {
            this.$refs["form"].validate((valid, done) => {
                done();
                if (valid) {
                    this.mingxibaocun = true
                    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.getDetail(res.data.data.id)
                    }).finally(() => {
                        loading.close();
                    })
                } else {
                    return false;
                }
            });
        },
        getDetail(id, type) {
            const loading = this.$loading({
                lock: true,
                text: '加载中',
                spinner: 'el-icon-loading',
                background: 'rgba(255,255,255,0.7)'
            })
            getDetails({ id: id }).then(res => {
                this.form = res.data.data
                if (res.data.data.status == 1) {
                    this.optionForm.disabled = true
                } else {
                    this.optionForm.disabled = false
                }
            }).finally(() => {
                loading.close();
            })
        },
        inRevoke() {
            revoke(this.form).then(res => {
                this.editButton = false
                this.optionForm.disabled = false
                this.$message.success("撤销成功");
                this.getDetail(this.form.id)
            })
        },
        inConfirm() {
            confirming(this.form).then(res => {
                this.editButton = true
                this.optionForm.disabled = true
                this.$message.success("确认成功");
                this.getDetail(this.form.id)
            })
        },
        //自定义列保存
        async saveColumnTwo(ref, option, optionBack, code) {
            /**
             * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
             * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
             * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
             */
            const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
            if (inSave) {
                this.$message.success("保存成功");
                //关闭窗口
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },
        //自定义列重置
        async resetColumnTwo(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;
            }
        },
        backToList(type) {
            this.$emit("backToList", type);
        },
    }
}
</script>

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