<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <!-- 搜索栏与表格区域 -->
        <div class="main-container">
            <div class="search-container TableSelectMain widthAuto">
                <el-form class="table-select-main" :model="searchForm" label-position="left">
                    <el-row :gutter="15">
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入维保项名称" v-model="searchForm.opt"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-select
                                    placeholder="请选择驱动类型"
                                    v-model="searchForm.driveType"
                                    clearable
                                >
                                    <el-option
                                        :label="item.value"
                                        :value="item.key"
                                        v-for="(item, i) in driveTypeList"
                                        :key="i"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-select
                                    placeholder="维保周期"
                                    v-model="searchForm.validTime"
                                    clearable
                                >
                                    <el-option
                                        :label="item.value"
                                        :value="item.key"
                                        v-for="(item, i) in validTimeList"
                                        :key="i"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="启用状态" v-model="searchForm.statuz" clearable>
                                    <el-option label="是" :value="1"></el-option>
                                    <el-option label="否" :value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-button type="primary" plain round @click="query">查询</el-button>
                                <el-button type="primary" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    round
                                    icon="el-icon-plus"
                                    @click="create"
                                >新建</el-button>
                                <el-button type="primary" plain round @click="batchUse">批量启用</el-button>
                                <el-button type="primary" plain round @click="batchNoUse">批量关闭</el-button>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    @click="deleteData(ids, 'batch')"
                                >批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">{{ ids.length }}</span> 项
            </div>

            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectionChange"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <el-table-column
                        prop="opt"
                        label="维保项名称"
                        min-width="200px;"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column label="驱动类型" min-width="150px;" :show-overflow-tooltip="true">
                        <template slot-scope="scope">{{ scope.row.driveType | typePipe }}</template>
                    </el-table-column>
                    <el-table-column label="维保周期" min-width="110px;" :show-overflow-tooltip="true">
                        <template slot-scope="scope">{{ scope.row.validTime | cyclePipe }}</template>
                    </el-table-column>
                    <el-table-column
                        prop="requirement"
                        label="维保要求"
                        min-width="300px;"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column prop="planDate" label="启用状态" min-width="110px;">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.statuz"
                                active-text="是"
                                inactive-text="否"
                                :active-value="1"
                                :inactive-value="0"
                                @change="statusChange(scope.row.id, scope.row.statuz)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="180px;">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="edit(scope.row)"
                            >编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagenation">
                    <PageComponent
                        :total="total"
                        @pagination="handlePagination"
                        :pageSize.sync="page.pageSize"
                        :pageNum.sync="page.pageNum"
                    ></PageComponent>
                </div>
            </div>
        </div>
        <!-- 添加与编辑弹层 -->
        <AddEditDialog @queryListData="queryListData" ref="addEditDialog"></AddEditDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import { queryPage, removeData, editData, useBath, noUseBath } from '@/apps/mobile/api/template-library-maintenance/index';
import { queryConst } from '@/apps/mobile/api/tableDicts/index';
let that;
export default {
    components: {
        TitleBar,
        AddEditDialog,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [], // 列表数据
            page: {
                // 页码
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false, // table是否展示loading
            ids: [], // 选中的列
            driveTypeList: [], //驱动类型
            validTimeList: [], //维保周期
        };
    },
    methods: {
        /**
         * table选择列被改变时
         * @param {Array} data 选中的列
         */
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },
        // 页码变化时
        handlePagination(val) {
            // console.log(val, 'handlePagination');
            this.page = val;
            this.queryListData();
        },

        create() {
            this.$refs.addEditDialog.open(this.driveTypeList, this.validTimeList, 'add');
        },
        /**
         * 编辑
         */
        edit(data) {
            this.$refs.addEditDialog.open(this.driveTypeList, this.validTimeList, 'edit', data);
        },

        // 分页获取招聘信息
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'asc',
                    orderBy: 'createTime',
                },
            };
            queryPage(obj)
                .then(res => {
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        // 查询
        query() {
            for (let k in this.searchForm) {
                if (this.searchForm[k] === '' || this.searchForm[k] === null || this.searchForm[k] === undefined) {
                    delete this.searchForm[k];
                }
            }
            this.page.pageNum = 1;
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.page.pageNum = 1;
            this.queryListData();
        },
        /**
         * 删除功能
         * @param {String} type 授权操作类型:'batch'批量授权  'single'单个授权
         * @param {Number} id 当前需要删除的id或集合
         */
        deleteData(id, type = 'single') {
            let obj = {};
            if (type === 'batch') {
                if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
                // console.log(this.ids, "ids");
                obj = {
                    ids: this.ids
                }
                this.delete(obj);
            } else {
                obj = {
                    ids: [id]
                }
                this.delete(obj);
            }
        },
        delete(obj) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    removeData(obj)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },
        // 批量启用
        batchUse() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            useBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量停止
        batchNoUse() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            noUseBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        statusChange(id, type) {
            let obj = {
                id,
                statuz: type,
            };
            editData(obj)
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 驱动类型
        drivingType() {
            let obj = {
                tableName: 'maintenance_set',
                field: 'drive_type',
            };
            this.driveTypeList = this.queryConstList(obj);
            console.log(this.driveTypeList, '驱动类型');
        },
        // 维保周期
        async maintenanceCycle() {
            let obj = {
                tableName: 'maintenance_set',
                field: 'valid_time',
            };
            this.validTimeList = this.queryConstList(obj);
        },
        queryConstList(obj) {
            let arr = [];
            queryConst(obj)
                .then(res => {
                    res.data.forEach(item => {
                        console.log(item, 'item');
                        arr.push(item);
                    });
                })
                .catch();
            return arr;
        },
    },
    created() {
        this.queryListData();
        this.maintenanceCycle();
        this.drivingType();
    },
    beforeCreate() {
        that = this;
    },
    filters: {
        // periodStatus: (validTime) => {
        //     let str = '';
        //     for (let i = 0; i < periodStatus.length; i++) {
        //         if (periodStatus[i].id === validTime) {
        //             str = periodStatus[i].name;
        //             break;
        //         }
        //     }
        //     return str;
        // },
        typePipe: val => {
            let str = '';
            that.driveTypeList.forEach(item => {
                if (item.key == val) {
                    str = item.value;
                }
            });
            return str;
        },
        cyclePipe: val => {
            let str = '';
            that.validTimeList.forEach(item => {
                if (item.key == val) {
                    str = item.value;
                }
            });
            return str;
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
