<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.updateItemList" id="out-table" ref="crud"
                    @selection-change="selectionChange" @select="selectHandle" @row-update="rowUpdate"
                    @resetColumn="resetColumn('crud', 'option', 'optionBack', 417)"
                    @saveColumn="saveColumn('crud', 'option', 'optionBack', 417)">
                    <template slot="indexHeader" slot-scope="{row,index}">
                        <el-button type="primary" size="mini" icon="el-icon-plus" :disabled="editDisabled" circle
                            @click="addRow()">
                        </el-button>
                    </template>
                    <template slot="index" slot-scope="{row,index}">
                        <span>{{ index + 1 }}</span>
                    </template>
                    <template slot-scope="{row,index}" slot="menu">
                        <el-button size="small" type="text" icon="el-icon-edit" @click.stop="rowEdit(row)">编 辑
                        </el-button>
                        <el-button size="small" icon="el-icon-delete" type="text" @click.stop="rowDel(row, index)">删
                            除</el-button>
                    </template>
                </avue-crud>
            </trade-card>
        </div>
    </div>
</template>

<script>
import { getDetail, submit, getUpdate, checkUpdate, removeItem } from "@/api/serviceConfiguration/modifyData"; import feeInfo from "@/components/tradeAgency/fee-info";
import dicSelect from "@/components/dicSelect/main";
import checkSchedule from "@/components/checkH/checkSchedule.vue";
import businessReports from "@/components/tradeAgency/businessReports.vue";
import reports from "@/components/tradeAgency/reports.vue";
import { getDicinit } from "@/api/dicSelect/index";
import _ from "lodash";
export default {
    name: "detailsPage",

    data() {
        return {
            data: [],
            selectionList: [],
            form: {
                type: 1,
                updateItemList: []
            },
            optionForm: {
                // menuBtn: false,
                submitBtn: false,
                emptyBtn: false,
                span: 8,
                disabled: false,
                column: [
                    {
                        label: "业务类型",
                        prop: "businessType",
                        type: 'select',
                        dicData: [{
                            value: 'SE',
                            label: '海运出口'
                        }, {
                            value: 'SI',
                            label: '海运进口'
                        }, {
                            value: 'AE',
                            label: '空运出口'
                        }, {
                            value: 'AI',
                            label: '空运进口'
                        }, {
                            value: 'BGSE',
                            label: '海运报关出口'
                        }, {
                            value: 'BGSI',
                            label: '海运报关进口'
                        }, {
                            value: 'BGAE',
                            label: '空运报关出口'
                        }, {
                            value: 'BGAI',
                            label: '空运报关进口'
                        }],
                        rules: [{
                            required: true,
                            message: " ",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "创建日期",
                        prop: "createTime",
                        disabled: true,
                    },
                    {
                        label: "创建人",
                        prop: "createUserName",
                        disabled: true,
                    },
                    {
                        label: "备注",
                        prop: "remarks",
                    }
                ]
            },
            option: {},
            optionBack: {
                height: 'auto',
                calcHeight: 30,
                menuWidth: 150,
                tip: false,
                border: true,
                addBtn: false,
                viewBtn: false,
                editBtn: false,
                delBtn: false,
                refreshBtn: false,
                selection: true,
                align: 'center',
                column: [
                    {
                        label: "index",
                        prop: "index",
                        width: "55",
                        headerslot: true,
                    },
                    {
                        label: "字段描述",
                        prop: "fieldDescribe",
                        overHidden: true,
                        width: "100",
                        cell: true,
                        rules: [
                            {
                                required: true,
                                message: '',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "字段",
                        prop: "field",
                        overHidden: true,
                        width: "100",
                        cell: true,
                        rules: [
                            {
                                required: true,
                                message: '',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "字段类型",
                        prop: "fieldType",
                        overHidden: true,
                        width: "100",
                        cell: true,
                        type: "select",
                        dicUrl: "/api/blade-system/dict-biz/dictionary?code=field_type",
                        props: {
                            label: "dictValue",
                            value: "dictKey"
                        },
                        rules: [
                            {
                                required: true,
                                message: '请选择商品类别',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "字段值类型",
                        prop: "fieldValueType",
                        cell: true,
                        overHidden: true,
                        width: "100",
                        type: 'select',
                        dicData: [{
                            label: '输入框',
                            value: '输入框'
                        }, {
                            label: '选择器',
                            value: '选择器'
                        }],
                        rules: [
                            {
                                required: true,
                                message: '请选择商品类别',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "数据地址",
                        prop: "fieldValueUrl",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "选择器绑定值",
                        prop: "fieldValueRetrieval",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "查询类型",
                        prop: "queryType",
                        cell: true,
                        width: "100",
                        overHidden: true,
                        type: 'select',
                        dicData: [{
                            label: '字典',
                            value: '字典'
                        }, {
                            label: '客户',
                            value: '客户'
                        }, {
                            label: '国家',
                            value: '国家'
                        }, {
                            label: '船名',
                            value: '船名'
                        }, {
                            label: '包装',
                            value: '包装'
                        }, {
                            label: '费用',
                            value: '费用'
                        }, {
                            label: '箱型',
                            value: '箱型'
                        }, {
                            label: '服务条款',
                            value: '服务条款'
                        }, {
                            label: '成交方式',
                            value: '成交方式'
                        }, {
                            label: '航线',
                            value: '航线'
                        }, {
                            label: '货物',
                            value: '货物'
                        }, {
                            label: '港口',
                            value: '港口'
                        }, {
                            label: '业务员',
                            value: '业务员'
                        }, {
                            label: '业务来源',
                            value: '业务来源'
                        }],
                    },
                    {
                        label: "id字段",
                        prop: "mainFieldId",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "编码字段",
                        prop: "mainFieldCode",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "中文字段",
                        prop: "mainFieldCname",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "id字段",
                        prop: "mainFieldId",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "编码字段",
                        prop: "mainFieldCode",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "修改表",
                        prop: "surface",
                        overHidden: true,
                        width: "170",
                        cell: true,
                        type: "select",
                        dicUrl: "/api/blade-system/dict-biz/dictionary?code=modify_table",
                        props: {
                            label: "dictValue",
                            value: "dictKey"
                        },
                        multiple: true,
                        dataType: "string",
                        tags: true,
                        rules: [
                            {
                                required: true,
                                message: '请选择商品类别',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "状态(是否启用)",
                        prop: "status",
                        overHidden: true,
                        width: "100",
                        cell: true,
                        type: 'select',
                        dicData: [{
                            label: '是',
                            value: 1
                        }, {
                            label: '否',
                            value: 0
                        }],
                        rules: [
                            {
                                required: true,
                                message: '请选择商品类别',
                                trigger: 'blur'
                            }
                        ]
                    },
                    {
                        label: "英文字段",
                        prop: "mainFieldEname",
                        cell: true,
                        width: "100",
                        overHidden: true
                    },
                    {
                        label: "amends id字段",
                        prop: "amendsFieldId",
                        width: "100",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "amends 编码字段",
                        prop: "amendsFieldCode",
                        width: "110",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "amends 中文字段",
                        prop: "amendsFieldCname",
                        width: "110",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "amends 英文字段",
                        prop: "amendsFieldEname",
                        width: "110",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "费用中心 id字段",
                        prop: "feeCenterFieldId",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "费用中心 编码字段",
                        prop: "feeCenterFieldCode",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "费用中心 中文字段",
                        prop: "feeCenterFieldCname",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "费用中心 英文字段",
                        prop: "feeCenterFieldEname",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "账单中心 id字段",
                        prop: "finAccBillsFieldId",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "账单中心 编码字段",
                        prop: "finAccBillsFieldCode",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "账单中心 中文字段",
                        prop: "finAccBillsFieldCname",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    {
                        label: "账单中心 英文字段",
                        prop: "finAccBillsFieldEname",
                        width: "120",
                        cell: true,
                        overHidden: true
                    },
                    // {
                    //     label: "关联字段",
                    //     prop: "relationField",
                    //     cell: true,
                    //     overHidden: true,
                    // },
                    {
                        label: "创建人",
                        prop: "createUserName",
                        width: "100",
                        overHidden: true,
                    },
                    {
                        label: "备注",
                        prop: "remarks",
                        width: "100",
                        cell: true,
                        overHidden: true,
                    }
                ]
            },
        }
    },
    components: {
        dicSelect,
        feeInfo,
        checkSchedule,
        businessReports,
        reports
    },
    props: {
        detailData: Object
    },
    async created() {
        this.roleName = localStorage.getItem('roleName').split(',')
        this.saberUserInfo = JSON.parse(localStorage.getItem("saber-userInfo")).content
        this.option = await this.getColumnData(this.getColumnName(417), this.optionBack);
        if (this.detailData.id) {
            this.getDetails(this.detailData.id)
        }
    },
    methods: {
        addRow() {
            this.$refs["form"].validate((valid, done) => {
                done();
                if (valid) {
                    this.form.updateItemList.push({ $cellEdit: true })
                    this.$refs.crud.dicInit();
                }
            })

        },
        rowEdit(row) {
            console.log(row)
            if (row.$cellEdit == true) {
                this.$set(row, "$cellEdit", false);
            } else {
                this.$set(row, "$cellEdit", true);
            }
        },
        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()
            })
        },
        rowDel(row, index) {
            this.$confirm("确定删除数据?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                if (row.id) {
                    removeItem({ ids: row.id }).then(res => {
                        this.form.updateItemList.splice(index, 1);
                        this.$message.success("成功删除");
                    })
                } else {
                    this.form.updateItemList.splice(index, 1);
                }

            });
        },
        submit(type) {
            this.$refs["form"].validate((valid, done) => {
                done();
                if (valid) {
                    for (let row of this.form.updateItemList) {
                        if (!row.fieldDescribe || !row.field || !row.fieldType || !row.fieldValueType) {
                            this.$refs.crud.rowCell(row, row.$index)
                            return this.$message.error("请完善字段明细");
                        }
                        if (row.fieldValueType == '选择器') {
                            if (!row.fieldValueUrl || !row.fieldValueRetrieval || !row.queryType || !row.mainFieldId || !row.mainFieldCname) {
                                return this.$message.error("请完善选择器类型的数据地址、选择器绑定值、查询类型、id字段、中文字段");
                            }
                        }
                    }
                    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);
        },
    }
}
</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;
}

::v-deep .el-table--small td,
.el-table--small th {
    padding: 2px !important;
}

::v-deep .el-card__body {
    padding: 3px 10px;
}
</style>
