<template>
    <el-dialog :title="$t('device.allot-record.155854-0')" :visible.sync="open" width="1000px">
        <div class="allot-recor-dialog">
            <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
                <el-form-item prop="distributeType">
                    <el-select v-model="queryParams.distributeType" :placeholder="$t('device.allot-record.155854-16')" filterable size="small" clearable>
                        <el-option :label="$t('device.index.105953-14')" :value="1"></el-option>
                        <el-option :label="$t('device.index.105953-15')" :value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="productId">
                    <el-select v-model="queryParams.productId" :placeholder="$t('device.allot-record.155854-2')" filterable size="small" clearable>
                        <el-option v-for="item in productList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="operateDeptId" style="width: 205px">
                    <treeselect v-model="queryParams.operateDeptId" :options="deptOptions" :show-count="true" :placeholder="$t('device.allot-record.155854-1')" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">{{ $t('search') }}</el-button>
                    <el-button icon="el-icon-refresh" size="small" @click="resetQuery">{{ $t('reset') }}</el-button>
                </el-form-item>
            </el-form>

            <el-table v-loading="loading" ref="singleTable" :data="dataList" size="small" :border="false">
                <el-table-column :label="$t('device.allot-record.155854-2')" align="left" prop="productName" min-width="190" />
                <el-table-column align="left" prop="operateDeptName" min-width="160">
                    <template slot="header">
                        <span>{{ $t('device.allot-record.155854-1') }}</span>
                        <el-tooltip class="item" effect="dark" :content="$t('device.allot-record.155854-5')" placement="top" style="margin-left: 10px">
                            <i class="el-icon-warning-outline"></i>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column align="left" prop="targetDeptName" min-width="170">
                    <template slot="header">
                        <span>{{ $t('device.allot-import-dialog.060657-2') }}</span>
                        <el-tooltip class="item" effect="dark" :content="$t('device.allot-record.155854-7')" placement="top" style="margin-left: 10px">
                            <i class="el-icon-warning-outline"></i>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('device.allot-record.155854-8')" align="center" prop="total" min-width="85" />
                <el-table-column :label="$t('device.allot-record.155854-9')" align="center" prop="successQuantity" min-width="85" />
                <el-table-column :label="$t('device.allot-record.155854-10')" align="center" prop="failQuantity" min-width="85" />
                <el-table-column :label="$t('device.allot-record.155854-11')" align="center" prop="status" min-width="90">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.common_status_type" :value="scope.row.status" size="small" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('device.allot-record.155854-12')" align="center" prop="distributeTypeDesc" min-width="90" />
                <el-table-column :label="$t('device.allot-record.155854-13')" align="center" prop="createTime" width="150" />
                <el-table-column :label="$t('opation')" align="center" width="100">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" icon="el-icon-download" @click="handleDownLoad(scope.row)" v-hasPermi="['iot:device:record:export']">
                            {{ $t('device.allot-record.155854-15') }}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" layout="prev, pager, next" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
    </el-dialog>
</template>

<script>
import { listProduct } from '@/api/iot/product';
import { listRecycleRecord } from '@/api/iot/device';
import { deptsTreeSelect } from '@/api/system/user';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
    name: 'allotRecord',
    dicts: ['common_status_type'],
    components: {
        Treeselect,
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 总条数
            total: 0,
            // 打开选择产品对话框
            open: false,
            // 产品列表
            productList: [],
            dataList: [],
            //时间范围
            daterangeTime: [],
            deptOptions: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                type: 3,
            },
        };
    },
    created() {
        this.getProductList();
        this.getDeptTree();
    },
    methods: {
        /** 查询产品列表 */
        getProductList() {
            this.loading = true;
            const params = {
                pageSize: 999,
                showSenior: true,
            };
            listProduct(params).then((response) => {
                this.productList = response.rows.map((item) => {
                    return { value: item.productId, label: item.productName };
                });
                this.loading = false;
            });
        },
        //查询分配记录列表
        getList() {
            this.loading = true;
            listRecycleRecord(this.queryParams).then((response) => {
                this.dataList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        //下载明细
        handleDownLoad(row) {
            const params = {
                parentId: row.id,
                type: 4,
            };
            this.download(
                'iot/record/export',
                {
                    ...params,
                },
                `allot_${new Date().getTime()}.xlsx`
            );
        },
        /** 查询机构下拉树结构 */
        getDeptTree() {
            deptsTreeSelect().then((response) => {
                this.deptOptions = response.data;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        /**关闭对话框 */
        closeDialog() {
            this.open = false;
        },
    },
};
</script>

<style lang="scss" scoped>
.allot-recor-dialog {
    ::v-deep .vue-treeselect {
        width: 205px;
        height: 32px;
        line-height: 29.5px;
        font-size: 13px;
    }

    ::v-deep .vue-treeselect__control {
        margin-top: 2px;
        height: 29.5px;
    }

    ::v-deep .vue-treeselect__placeholder {
        line-height: 29.5px;
    }

    ::v-deep .vue-treeselect__single-value {
        line-height: 29.5px;
    }

    ::v-deep .vue-treeselect__input {
        height: 29.5px;
    }
}
</style>
