<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">
                </avue-form>
            </trade-card>
            <trade-card title="仓储明细">
                <avue-crud :option="option" :data="form.storageFeeItemsList" id="out-table" ref="crud"
                    @selection-change="selectionChange" @resetColumn="resetColumn('crud', 'option', 'optionBack', 409)"
                    @saveColumn="saveColumn('crud', 'option', 'optionBack', 409)">
                    <template slot="menuLeft">
                        <el-button type="primary" size="small" icon="el-icon-plus" @click="addRow">新 增
                        </el-button>
                    </template>
                    <tempalte slot="goodsTypeForm" slot-scope="{ row }">
                        <dic-select v-if="row.$cellEdit" v-model="row.goodsType" placeholder="商品类型" key="dictKey"
                            label="dictValue" url="/blade-system/dict-biz/dictionary?code=goods-type"
                            :filterable="true"></dic-select>
                        <span v-else>{{ row.goodsType }}</span>
                    </tempalte>
                    <!-- <template slot="feeName" slot-scope="{ row }">
                        <dic-select v-if="row.$cellEdit" v-model="row.feeName" placeholder="费用信息" key="id" res="records"
                            label="cnName" url="/blade-los/bfees/list" :filterable="true" :remote="true" fff
                            dataName="cnName" @selectChange="rowDicChange('feeName', $event, row)"></dic-select>
                        <span v-else>{{ row.feeName }}</span>
                    </template> -->
                    <template slot="menu" slot-scope="{ row, index }">
                        <el-button size="small" icon="el-icon-edit" type="text" @click="rowEdit(row)">{{ row.$cellEdit ?
                            '保存' : '编辑' }}</el-button>
                        <el-button size="small" icon="el-icon-delete" type="text" @click="rowDel(row, index)">删除</el-button>
                    </template>
                </avue-crud>
            </trade-card>
            <!-- <containerTitle title="上传附件"></containerTitle>
            <c-upload :data="form.filesCenterList" :enumerationValue="76"
                deleteUrl="/blade-purchase-sales/orderfiles/update" display :disabled="editDisabled"></c-upload> -->
        </div>
    </div>
</template>

<script>
import { submit, getDetail, copyAgent, itemRemove } from "@/api/iosBasicData/storageFee";
import { getBtrademodesList, getBtrademodesDetail, addBtrademodes, updateBtrademodes, removeBtrademodes } from "@/api/iosBasicData/warehouse";
import dicSelect from "@/components/dicSelect/main";
import { bfeesList } from "@/api/iosBasicData/bfees";
import _ from "lodash";
export default {
    name: "detailsPage",

    data() {
        return {
            form: {
                status: '0',
                storageFeeItemsList: []
            },
            optionForm: {
                menuBtn: false,
                span: 8,
                column: [
                    {
                        label: "编码",
                        prop: "code",
                        disabled: false,
                        rules: [{
                            required: true,
                            message: " ",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "中文名称",
                        prop: "cname",
                        disabled: false,
                        rules: [{
                            required: true,
                            message: " ",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "状态",
                        prop: "status",
                        type: "select",
                        search: true,
                        dicData: [{
                            label: '启用',
                            value: '0'
                        }, {
                            label: '停用',
                            value: '1'
                        }],
                        value: '0',
                        overHidden: true,
                    },
                    {
                        label: '备注',
                        prop: "remarks",
                        type: 'textarea',
                        disabled: false,
                        minRows: 2,
                    },
                ]
            },
            option: {},
            optionBack: {
                height: 'auto',
                calcHeight: 30,
                menuWidth: 140,
                border: true,
                index: true,
                addBtn: false,
                viewBtn: false,
                editBtn: false,
                delBtn: false,
                refreshBtn: false,
                align: 'center',
                column: [
                    {
                        label: "费用名称",
                        prop: "feeName",
                        overHidden: true
                    },
                    {
                        label: "计价单位",
                        prop: "feeUnit",
                        // cell: true,
                        type: 'select',
                        filterable: true,
                        remote: true,
                        dicUrl: "/api/blade-los/bunits/list",
                        props: {
                            label: 'cnName',
                            value: 'cnName',
                            res: 'data.records'
                        },
                        overHidden: true
                    },
                    {
                        label: "商品类别",
                        prop: "goodsType",
                        cell: true,
                        slot: true,
                        formslot: true,
                        overHidden: true,
                        rules: [
                            {
                                required: true,
                                message: '请选择商品类别',
                                trigger: 'blur'
                            }
                        ]
                    },
                    // {
                    //     label: "开始天数",
                    //     prop: "fromDays",
                    //     cell: true,
                    //     type: 'number',
                    //     controls: false,
                    //     overHidden: true
                    // },
                    // {
                    //     label: "结束天数",
                    //     prop: "enDays",
                    //     cell: true,
                    //     type: 'number',
                    //     controls: false,
                    //     overHidden: true
                    // },
                    {
                        label: "应收单价",
                        prop: "price",
                        cell: true,
                        type: 'number',
                        controls: false,
                        overHidden: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入应收单价',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "应付单价",
                        prop: "priceC",
                        cell: true,
                        type: 'number',
                        controls: false,
                        overHidden: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入应付单价',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "备注",
                        prop: "remarks",
                        cell: true,
                        overHidden: true
                    }
                ]
            },
        }
    },
    components: {
        dicSelect,
    },
    props: {
        detailData: Object
    },
    async created() {
        this.option = await this.getColumnData(this.getColumnName(409), this.optionBack);
        if (this.detailData.id) {
            this.getDetails(this.detailData.id)
        }
    },
    methods: {
        rowEdit(row, index) {
            if (row.$cellEdit) {
                if (!row.goodsType || !row.price|| !row.priceC) {
                    this.$refs.crud.rowCell(row, row.$index)
                    return this.$message.error("请完善明细信息");
                }
                this.$set(row, '$cellEdit', false)
            } else {
                this.$set(row, '$cellEdit', true)
            }
        },
        rowDel(row, index) {
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                if (row.id) {
                    itemRemove({ ids: row.id }).then(res => {
                        this.form.storageFeeItemsList.splice(index, 1);
                        this.$message.success("操作成功!");
                    });
                } else {
                    this.form.storageFeeItemsList.splice(index, 1);
                    this.$message.success("操作成功!");
                }
            }
            );
        },
        rowDicChange(name, row, el) {
            if (name == 'feeName') {
                if (row) {
                    el.feeId = row.id
                } else {
                    el.feeId = null
                }
            }
        },
        getDetails(id) {
            const loading = this.$loading({
                lock: true,
                text: '加载中',
                spinner: 'el-icon-loading',
                background: 'rgba(255,255,255,0.7)'
            });
            getBtrademodesDetail(id).then(res => {
                this.form = res.data.data
                console.log(this.form)
                this.$refs.crud.dicInit();
            }).finally(() => {
                loading.close()
            })
        },
        getCopydate(id) {
            const loading = this.$loading({
                lock: true,
                text: '加载中',
                spinner: 'el-icon-loading',
                background: 'rgba(255,255,255,0.7)'
            });
            copyAgent({ id: id }).then(res => {
                this.form = res.data.data
            }).finally(() => {
                loading.close()
            })
        },
        addRow() {
            this.$refs.crud.dicInit();
            bfeesList(1, 10, { cnName: '仓储费' }).then(res => {
                res.data.data.records.forEach(e => {
                    if (e.cnName == '仓储费')
                        this.form.storageFeeItemsList.push({
                            feeId: e.id,
                            feeName: e.cnName,
                            feeUnit: '吨',
                            $cellEdit: true
                        })
                })

            })

        },
        submit() {
            this.$refs["form"].validate((valid, done) => {
                done();
                if (valid) {
                    for (let row of this.form.storageFeeItemsList) {
                        if (!row.goodsType || !row.price|| !row.priceC) {
                            this.$refs.crud.rowCell(row, row.$index)
                            return this.$message.error("请完善明细信息");
                        }
                    }
                    const loading = this.$loading({
                        lock: true,
                        text: '加载中',
                        spinner: 'el-icon-loading',
                        background: 'rgba(255,255,255,0.7)'
                    });
                    updateBtrademodes(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>
