<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <div>
            <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
                <el-tab-pane label="历史规则" name="1"></el-tab-pane>
                <el-tab-pane label="当前规则" name="2"></el-tab-pane>
                <el-tab-pane label="审核" name="3"></el-tab-pane>
            </el-tabs>
        </div>
        <div class="content-box">
            <el-form :model="formData" :rules="rules" ref="formData" label-width="180px">
                <el-form-item label="平台选择专家默认收费:" prop="appointExpertPrice">
                    <el-input
                        v-model="formData.appointExpertPrice"
                        :disabled="!show"
                        placeholder="请输入"
                    ></el-input>
                </el-form-item>
                <el-form-item label="附近人范围:" prop="nearPeopleArea">
                    <el-input v-model="formData.nearPeopleArea" :disabled="!show" placeholder="请输入">
                        <template slot="append">公里</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="税率:" prop="defaultTax">
                    <el-input v-model="formData.defaultTax" :disabled="!show" placeholder="请输入">
                        <template slot="append">%</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="单次提现上限金额:" prop="withdrawFee">
                    <el-input v-model="formData.withdrawFee" :disabled="!show" placeholder="">
                    </el-input>
                </el-form-item>
                <el-form-item label="每月提现次数:" prop="withdrawNum">
                    <el-input v-model="formData.withdrawNum" :disabled="!show" placeholder="">
                    </el-input>
                </el-form-item>
                <el-form-item label="打卡范围:" prop="clockInArea">
                    <el-input v-model="formData.clockInArea" :disabled="!show" placeholder="">
                        <template slot="append">米</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="间隔时间:" prop="intervalTime">
                    <el-input v-model="formData.intervalTime" :disabled="!show" placeholder="">
                        <template slot="append">分钟</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="经纬度推送开关:">
                    <el-switch
                        v-model="formData.isPush"
                        disabled
                        active-text="开"
                        inactive-text="关"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
            </el-form>

            <div class="btn">
                <el-button
                    type="primary"
                    :disabled="!auditId"
                    round
                    v-if="!show && activeName == '3'"
                    @click="pass"
                >通过</el-button>
                <el-button
                    type="primary"
                    :disabled="!auditId"
                    round
                    v-if="!show && activeName == '3'"
                    @click="rejectShow"
                >驳回</el-button>
            </div>
        </div>
        <!-- 驳回弹窗 -->
        <RejectDialog @isReject="isReject" ref="regectDialog"></RejectDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import RejectDialog from '@/apps/mobile/components/reject/index';
import {
    addData,
    addDataSave,
    removeData,
    editData,
    examine,
    queryNew,
    queryHistory,
    queryCommit,
    queryStay,
    submitData,
} from '@/apps/mobile/api/operation/other-settings/index';
import { validWithoutZero } from '@/apps/mobile/utils/validate';
export default {
    components: {
        TitleBar,
        RejectDialog,
    },
    data() {
        // 正整数校验
        var checkNum = (rule, value, callback) => {
            if (value && !validWithoutZero(value)) {
                callback(new Error('格式不正确'));
            } else {
                callback();
            }
        };
        return {
            input: '',
            rules: {
                appointExpertPrice: [
                    { required: true, message: '请输入平台选择专家最低费用' },
                    { max: 10, message: '长度最大10个字符', trigger: 'blur' },
                    { validator: checkNum, message: '请输入正确的费用', trigger: 'blur' },
                ],
                nearPeopleArea: [
                    { required: true, message: '请输入附近人范围数' },
                    { max: 10, message: '长度最大10个字符', trigger: 'blur' },
                    { validator: checkNum, message: '请输入正确的范围数', trigger: 'blur' },
                ],
                defaultTax: [
                    { required: true, message: '请输入税率' },
                    { max: 10, message: '长度最大10个字符', trigger: 'blur' },
                    { validator: checkNum, message: '请输入正确的税率', trigger: 'blur' },
                ],
            },
            formData: {},
            activeName: '2',
            show: 0,
            btndisabled: false,
            id: null, //待提交数据ID
            auditId: null, //待审核id
            editobj: {}, // 储存待提交的数据
            theSame: true, // 待提交的数据是否改变 false改变  true未变
        };
    },
    methods: {
        reset() {
            this.formData = {};
            this.$refs.formData.resetFields();
        },
        // 进入审批页面调用
        showChange() {
            this.show = 0;
            this.queryNewData();
        },
        // 点击标签页
        handleClick(tab) {
            console.log(tab, 'activeName');
            if (tab.name === '1') {
                // 查询历史版本
                this.queryHistoryData();
            }
            if (tab.name === '2') {
                // 查询最新的
                this.queryNewData();
            }
            if (tab.name === '3') {
                // 查询待审核的
                this.queryStayData();
            }
        },
        // 查询历史版本
        queryHistoryData() {
            queryHistory()
                .then(res => {
                    console.log(res, '历史');
                    if (res.data.id) {
                        this.formData = { ...res.data };
                    } else {
                        this.reset();
                    }
                })
                .catch(err => err);
        },

        // 查询待审核
        queryStayData() {
            queryStay()
                .then(res => {
                    console.log(res);
                    if (res.data && res.data.length > 0) {
                        this.formData = res.data.pop();
                        this.auditId = this.formData.id;
                    } else {
                        this.auditId = null;
                        this.reset();
                    }
                })
                .catch(err => err);
        },

        // 查询最新版本
        queryNewData() {
            if (!this.show && this.activeName === '2') {
                queryNew()
                    .then(res => {
                        console.log(res, '最新');
                        if (res.data.id) {
                            this.formData = res.data;
                        } else {
                            this.reset();
                        }
                    })
                    .catch(err => err);
            }
        },
        // 审批
        approval(data) {
            examine(data).then(res => {
                if (res.statusCode === '1') {
                    this.$message.success(res.message);
                    this.auditId = null;
                    this.queryStayData();
                } else {
                    this.$message.warning(res.message);
                }
            });
        },
        // 审核通过  (仅在审批页面使用)
        pass() {
            this.$confirm('是否通过审批?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    if (this.auditId) {
                        // 有待审数据
                        let examineObj = {
                            id: this.auditId,
                            examineFlag: 1,
                        };
                        this.approval(examineObj);
                    }
                })
                .catch(() => {});
        },
        // 驳回 (审批页面调用)
        isReject(data) {
            let bhObj = {
                id: this.auditId,
                notExamineReason: data.checkComment,
                examineFlag: 2,
            };
            this.approval(bhObj);
        },
        rejectShow() {
            this.$refs.regectDialog.open(this.auditId);
        },
    },
    created() {
        this.$nextTick(() => {
            this.queryNewData();
        });
    },
    watch: {
        // 判断用户是都修改了数据
        formData: {
            handler(newVal) {
                if (JSON.stringify(newVal) !== JSON.stringify(this.editobj)) {
                    this.theSame = false;
                } else {
                    this.theSame = true;
                }
            },
            deep: true,
        }
    },
};
</script>
<style scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}

.el-input {
    width: 200px;
}

::v-deep .el-input-group__append {
    background-color: #fff;
    border: none;
    color: #000;
}

::v-deep .el-input__inner {
    width: auto;
}

.content-box {
    margin-left: 100px;
    margin-top: 20px;
}

.btn {
    margin-left: 250px;
    margin-top: 40px;
}
</style>
