<!--
 * @Descripttion: 设置代理价
 * @version: 1.0
 * @Author: zzx
 * @Date: 2024-11-27 16:08:16
 * @LastEditors: zzx tigerzouzx@foxmail.com
 * @LastEditTime: 2024-12-02 18:00
-->
<template>
    <div class="app-container">
        <el-card class="operate-container" shadow="hover">
            <div style="
          display: flex;
          align-items: center;
          justify-content: space-between;
        ">
                <div style="display: flex; align-items: center">
                    <div>
                        <i class="el-icon-tickets"></i>
                        <span>数据列表</span>
                    </div>
                    <div style="margin-left: 20px">
                        <el-descriptions class="margin-top" :column="3" size="medium" border>
                            <el-descriptions-item label="电站">{{ name }}</el-descriptions-item>
                        </el-descriptions>
                    </div>
                </div>
                <div>
                    <back-button></back-button>
                </div>
            </div>
        </el-card>
        <div class="table-container">
            <el-table ref="adminTable" :data="dialogList" style="width: 100%" v-loading="dialogListLoading" border>
                <el-table-column label="时间段" align="center" prop="time"></el-table-column>
                <el-table-column label="销售合计价(元)" align="center" prop="platformPrice"></el-table-column>
                <el-table-column label="一级代理价(元)" align="center" prop="firstLevelPrice" v-if="isAdmin!=3"></el-table-column>
                <el-table-column label="二级代理价(元)" align="center" prop="secondLevelPrice" v-if="isAdmin==3"></el-table-column>
                <el-table-column label="二级代理价(元)" align="center" v-else>
                    <template slot-scope="scope">
                        <el-tooltip class="item" v-if="scope.row.isEdit === false" effect="dark" content="单击编辑" placement="left">
                            <span class="edit" @click="toggleEdit(scope.row)">{{ scope.row.secondLevelPrice }}</span>
                        </el-tooltip>
                        <el-input v-else v-model="editInput" size="small" oninput="value=value.replace(/[^\d.]/g,'')" style="width: 200px;" @blur="saveEdit(scope.row)">
                            <i slot="suffix" size="small" class="el-icon-check" @click="confrimEdit(scope.row)"></i>
                            <i slot="suffix" size="small" class="el-icon-close" @click="cancelEdit(scope.row)"></i>
                        </el-input>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-divider v-if="isAdmin!=3"></el-divider>
        <el-card class="operate-container" shadow="hover" v-if="isAdmin!=3">
            <div style="text-align: center;">
                <el-button type="primary" size="small" :loading="confirmLoading" @click="confirmForbidden">提交</el-button>
                <el-button type="info" size="small" @click="cancelForbidden">恢复默认</el-button>
            </div>
        </el-card>
    </div>
</template>
<script>
import { priceList, settingsPrice } from "@/api/distribution";
import Big from 'big.js';
const defaultListQuery = {
    adminUserId: null,
    stationId: null,
};
export default {
    name: "settingsPrice",
    data() {
        return {
            isAdmin: null,
            name: null,
            listQuery: Object.assign({}, defaultListQuery),
            forbiddenForm: {},
            dialogList: null,
            dialogListLoading: false,
            total: null,
            editInput: null,
            currentEditingRow: null, //用于记录当前正在编辑的行
            confirmLoading: false
        };
    },
    watch: {},
    created() {
        this.name = this.$route.query.name;
        this.listQuery.adminUserId = this.$route.query.adminUserId;
        this.listQuery.stationId = this.$route.query.stationId;
        this.isAdmin = localStorage.getItem("isAdmin");
        this.getList();
    },
    methods: {
        toggleEdit(row) {
            if (this.currentEditingRow) {
                this.currentEditingRow.isEdit = false;
            }
            row.isEdit = !row.isEdit;
            this.editInput = row.secondLevelPrice;
            if (row.isEdit) {
                this.currentEditingRow = row;
                
            } else {
                this.currentEditingRow = null;
            }
        },
        confrimEdit(row) {
            if (this.editInput >= row.platformPrice) {
                return this.$message.error('价格必须小于平台价格');
            }
            if (this.editInput <= row.firstLevelPrice) {
                return this.$message.error('不能小于一级代理价');
            }
            if(this.editInput<=0){
                return this.$message.error('价格不能小于0');
            }
            row.secondLevelPrice = this.editInput
            row.isEdit = false
            this.currentEditingRow = null;
        },
        cancelEdit(row) {
            row.isEdit = false
            this.editInput =0;
            this.currentEditingRow = null;
        },
        saveEdit(row) {
            // if (row.secondLevelPrice >= row.platformPrice) {
            //     row.secondLevelPrice = 0
            //     return this.$message.error('分销价格必须小于平台价格');
            // }
            // if (row.secondLevelPrice <= row.firstLevelPrice) {
            //     row.secondLevelPrice = 0
            //     return this.$message.error('二级代理价不能小于一级代理价');
            // }
            // row.secondLevelPrice = this.editInput
            // console.log(this.editInput,'---输入框');
            // console.log(row.secondLevelPrice,'---默认');
            // row.isEdit = false
        },
        confirmForbidden() {
            const newArray = this.dialogList.map(item => ({
                adminUserId: this.listQuery.adminUserId,
                firstLevelPrice: item.firstLevelPrice,
                id: item.id,
                platformPrice: item.platformPrice,
                secondLevelPrice: item.secondLevelPrice,
                servicePrice: new Big(item.platformPrice).minus(new Big(item.secondLevelPrice)).toNumber(),
                stationId: this.listQuery.stationId,
                time: item.time
            }))
            this.confirmLoading = true
            settingsPrice(newArray).then((res) => {
                this.confirmLoading = false
                if (res.code == 200) {
                    this.$message.success('设置成功');
                    this.getList();
                }
            })
        },
        cancelForbidden() {
            this.getList();
        },
        getList() {
            this.dialogListLoading = true;
            priceList(this.listQuery).then((response) => {
                this.dialogListLoading = false;
                this.dialogList = response.data;
                this.dialogList.map(item => {
                    this.$set(item, 'isEdit', false); //初始化为false
                })
            });
        },
    },
};
</script>
<style scoped>
.edit{
    color: #409EFF;
    cursor: pointer;
}
.el-icon-check{
    font-size: 22px;
    font-weight: 1000;
    color: rgb(103,194,58);
}
.el-icon-close{
    font-size: 22px;
    font-weight: 1000;
    color: rgba(240, 10, 10,0.6);
}
</style>
