<template>
    <div class="device">
        <div class="search">
            <el-input placeholder="输入设备编号/地址" class="search-input" clearable v-model="mixins_query.meterNo"></el-input>
            <el-select filterable v-model="mixins_query.approvalStatus" placeholder="请选择审核状态" clearable>
                <el-option label="未审核" :value="0"></el-option>
                <el-option label="已审核" :value="1"></el-option>
            </el-select>
            <zz-doubleInput
                middle="至"
                inputValueStart="currentReading"
                inputValueEnd="lastReading"
                :startPlaceholder="params.type == 1 ? '用水量起度' : '用电量起度'"
                :endPlaceholder="params.type == 1 ? '用水量止度' : '用电量止度'"
                :query="mixins_query"
                :clearable="true"
            >
            </zz-doubleInput>
            <el-button type="primary" class="search-btn" @click="mixins_search" icon="el-icon-search">查询 </el-button>
            <div class="search-icon">
                <el-tooltip placement="bottom" class="item" effect="light" content="批量审核">
                    <i class="zoniot_font zoniot-icon-piliangshenhe" @click="auditSelect()"></i>
                </el-tooltip>
                <el-tooltip placement="bottom" class="item" effect="light" content="全部审核">
                    <i class="zoniot_font zoniot-icon-quanbushenhe" @click="allAudit()"></i>
                </el-tooltip>
                <el-tooltip placement="bottom" class="item" effect="light" content="返回">
                    <i class="zoniot_font zoniot-icon-fanhui" @click="$emit('close')"></i>
                </el-tooltip>
            </div>
        </div>
        <zz-table
            :cols="cols"
            :data="mixins_list"
            :loading="mixins_onQuery"
            :pageset="mixins_pageset"
            @sort-change="sortChange"
            @page-change="pageChange"
            :settings="{ showCheckbox: true, showIndex: true }"
            @selection-change="selectionChange"
        >
            <template slot="settletimeRange" slot-scope="scope">
                <span>{{ timeformat(scope.row.clearingStartDate) }} - {{ timeformat(scope.row.clearingEndDate) }}</span>
            </template>
            <template slot-scope="scope" slot="opt">
                <div class="opt">
                    <el-tooltip class="item" effect="light" placement="bottom" :content="params.type == 1 ? '修改水量' : '修改电量'">
                        <span class="zoniot_font" @click="editRow(scope.row)">&#xe689;</span>
                    </el-tooltip>
                </div>
            </template>
        </zz-table>
    </div>
</template>
<script>
import list from '@utils/list.js';

export default {
    mixins: [list],
    props: ['params'],
    data() {
        return {
            auditOptions: [
                { id: '', name: '全部' },
                { id: '0', name: '未审核' },
                { id: '1', name: '已审核' }
            ],
            cols: [
                {
                    label: '所属社区',
                    prop: 'communityName'
                },
                {
                    label: '地址',
                    prop: 'location'
                },
                {
                    label: '设备编号',
                    prop: 'deviceNo'
                },
                {
                    label: '用途',
                    prop: 'useType',
                    format(val, data) {
                        return val == 1 ? '房间表' : '公摊表';
                    }
                },
                {
                    label: '计量周期',
                    prop: 'settletimeRange',
                    slot: 'settletimeRange',
                    width: 300
                },
                {
                    label: '上期读数',
                    prop: 'lastReading'
                },
                {
                    label: '本期读数',
                    prop: 'currentReading'
                },
                {
                    label: '用水量（吨）',
                    prop: 'waterConsumption'
                },
                {
                    label: '审核状态',
                    prop: 'approvalStatus',

                    format(val) {
                        return String(val) == '1' ? '已审核' : '未审核';
                    }
                },
                {
                    label: '操作',
                    slot: 'opt'
                }
            ],
            mixins_post: 'post',
            isfocus: false
        };
    },
    methods: {
        mixinsxxx() {
            console.log(this.mixins_query);
        },
        timeformat(val) {
            return this.$moment(val).format('YYYY-MM-DD HH:mm:ss');
        },
        // 多选框勾选事件
        selectionChange(val) {
            this.selectRow = val;
        },
        // 批量
        auditSelect() {
            if (!this.selectRow || !this.selectRow.length) {
                this.$message.error('请选择要审核的条目');
                return;
            }
            let ids = [];
            this.selectRow.forEach((v) => {
                ids.push({
                    id: v.id,
                    planId: this.params.id
                });
            });
            const title = '审核';
            let msg = '确认批量审核以上勾选的水表';
            this.__confirm(msg, title)
                .then((_) => {
                    this.$http
                        .post('/sc-energy/measurementRecord/audit', ids)
                        .then(({ status, data, msg }) => {
                            if (0 == status) {
                                this.$message.success(msg);
                                this.mixins_search();
                            } else {
                                this.$message.error(msg);
                            }
                        })
                        .catch((err) => {});
                })
                .catch((_) => {});
        },
        //全部
        allAudit() {
            const title = '审核全部水表';
            let msg = '确认审核全部水表？';
            this.__confirm(msg, title)
                .then((_) => {
                    console.log(this.params);
                    this.$http
                        .postForm('/sc-energy/measurementRecord/auditAll', { planId: this.params.id })
                        .then(({ status, data, msg }) => {
                            if (0 == status) {
                                this.$message.success(msg);
                                this.mixins_search();
                            } else {
                                this.$message.error(msg);
                            }
                        })
                        .catch((err) => {});
                })
                .catch((_) => {});
        },
        editRow(data = {}) {
            new Promise((resolve) => {
                this.$store.dispatch('addPopup', {
                    url: '/settlementAudit/popups/editWaterUsg',
                    width: '520px',
                    height: '200px',
                    props: {
                        data,
                        type: this.params.type,
                        callback: resolve
                    },
                    title: this.params.type == 1 ? '修改水量' : '修改电量'
                });
            }).then(() => {
                this.mixins_search();
            });
        }
    },
    created() {
        this.mixins_dataUrl = '/sc-energy/measurementRecord/page';
        this.mixins_query = {
            planId: this.params.id,
            lastReading:null,
            currentReading: null,
            status: ''
        };
        if (this.params.type == 2) {
            this.cols[7].label = '用电量(度)';
        }

        this.mixins_search();
    },
    mounted() {}
};
</script>

