<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :span="6">
                        <a-form-item label="关键字">
                            <a-input placeholder="关键字(方案、编号、简码)" v-model="queryParam.paramName" allowClear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                            <a-button type="primary" @click="searchReset" icon="reload"
                                      style="margin-left: 8px">重置</a-button>
                        </span>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator">
            <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
            <a-dropdown v-if="selectedRowKeys.length > 0">
<!--                <a-button style="margin-left: 8px">-->
<!--                    批量操作 <a-icon type="down" /></a-button>-->
                <a-button @click="batchDel" type="danger" icon="delete">批量删除</a-button>
            </a-dropdown>
        </div>

        <!-- table区域-begin -->
        <div>
            <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

            <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns"
                     :dataSource="dataSource" :pagination="ipagination" :loading="loading" :rowSelection="{
                    selectedRowKeys: selectedRowKeys,
                    onChange: onSelectChange,
                }" class="j-table-force-nowrap" @change="handleTableChange">
                <template slot="openSlot" slot-scope="text, record, index">
                    <a-switch checked-children="开" un-checked-children="关" default-checked :checked="record.open == 1"
                              @change="e=>changeOpenState(e,record)" />
                </template>
                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
                <template slot="imgSlot" slot-scope="text, record">
                    <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
                    <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
                         style="max-width: 80px; font-size: 12px; font-style: italic" />
                </template>
                <template slot="fileSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
                    <a-button v-else :ghost="true" type="primary" icon="download" size="small"
                              @click="downloadFile(text)">
                        下载
                    </a-button>
                </template>

                <span slot="action" slot-scope="text, record">
                    <a @click="handleEdit(record)">编辑</a>

                    <a-divider type="vertical" />
                    <a-dropdown>
                        <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                        <a-menu slot="overlay">
                            <a-menu-item>
                                <a @click="handleDetailSetting(record)">详情</a>
                            </a-menu-item>
                            <a-menu-item>
                                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                                    <a>删除</a>
                                </a-popconfirm>
                            </a-menu-item>
                        </a-menu>
                    </a-dropdown>
                </span>
            </a-table>
        </div>
<!--        <room-layout-form ref="modalForm" @ok="modalFormOk"></room-layout-form>-->
<!--        <room-layout-price-modal ref="priceModal" @ok="onPriceSave"></room-layout-price-modal>-->
<!--        <room-layout-detail-modal @saveOk="onSaveOk" ref="detailModal"></room-layout-detail-modal>-->
        <house-price-scheme-modal ref="modalForm" @ok="modalFormOk"></house-price-scheme-modal>
        <house-price-scheme-detail-modal ref="detailModal" @ok="modalFormOk"></house-price-scheme-detail-modal>
    </a-card>
</template>

<script>

    import { JeecgListMixin } from "@/mixins/JeecgListMixin";
    import { modifyAppState } from '@/api/roomLayout'
    import { filterObj } from "@/utils/util";
    import HousePriceSchemeModal from "./modules/housePriceSchemeModal";
    let hotelInfo = JSON.parse(localStorage.getItem('storeInfo'))
    import { httpAction, getAction } from '@/api/manage'
    import HousePriceSchemeDetailModal from "./modules/housePriceSchemeDetailModal";
    export default {
        name: "housePriceSchemeList",
        mixins: [JeecgListMixin],
        components: {
            HousePriceSchemeDetailModal,
            HousePriceSchemeModal
        },
        data() {
            return {
                queryParam: {},
                // 分页参数
                ipagination: {
                    current: 1,
                    pageSize: 10,
                    pageSizeOptions: ["10", "20", "30"],
                    showTotal: (total, range) => {
                        return range[0] + "-" + range[1] + " 共" + total + "条";
                    },
                    showQuickJumper: true,
                    showSizeChanger: true,
                    total: 0,
                },
                // 表头
                columns: [
                    {
                        title: "简码",
                        align: "center",
                        dataIndex: "simpleCode",
                    },
                    {
                        title: "编号",
                        align: "center",
                        dataIndex: "number",
                    },
                    {
                        title: "房价方案",
                        align: "center",
                        dataIndex: "priceScheme",

                    },
                    {
                        title: "客人类型",
                        align: "center",
                        dataIndex: "guestTypeName",
                    },
                    {
                        title: "入住时间",
                        align: "center",
                        dataIndex: "checkInTimeText",
                    },
                    {
                        title: "有效期",
                        align: "center",
                        dataIndex: "validTimeText",
                    },
                    {
                        title: "状态",
                        align: "center",
                        dataIndex: "open",
                        scopedSlots: {customRender: "openSlot"},
                    },
                    {
                        title: "排序",
                        align: "center",
                        dataIndex: "sort",
                    },
                    {
                        title: "操作时间",
                        align: "center",
                        dataIndex: "createAt",
                    },
                    {
                        title: "操作",
                        dataIndex: "action",
                        align: "center",
                        fixed: "right",
                        width: 147,
                        scopedSlots: {customRender: "action"},
                    },
                ],
                url: {
                    // list: 'org.jeecg.modules.business/busMarketMember/list',
                    list: "/rooms/cesHousePriceScheme/list?hotelId=" + hotelInfo.id,
                    delete: "/rooms/cesHousePriceScheme/remove",
                    deleteBatch: "/rooms/cesHousePriceScheme/deleteBatch",
                    exportXlsUrl: "/rooms/cesHousePriceScheme/exportXls",
                    importExcelUrl:
                        "rooms/cesHousePriceScheme/importExcel",
                    modifyOpen: "/rooms/cesHousePriceScheme/modifyOpen",
                },
                dictOptions: {},
                superFieldList: [],
                selectedRowKeys: [],
                isorter: {
                    column: "createTime",
                    order: "desc",
                },
            };
        },
        created() {
            // this.loadData()
        },
        methods: {
            onSaveOk() {
                this.loadData()
            },
            onPriceSave() {

            },
            handleDetailSetting(record) {
                this.$refs.detailModal.title="详情"
                this.$refs.detailModal.visible = true
                this.$refs.detailModal.schemeId=record.id
            },
            changeOpenState(e, record) {
                console.log(e)
                var that = this;
                this.confirmLoading = true
                httpAction(this.url.modifyOpen +'/'+ record.id + "?open=" + (e ? 1 : 0), {}, 'put').then((res) => {
                    if (res.success) {
                        that.$message.success('操作成功')
                        that.$emit('ok')
                        that.loadData(1);
                    } else {
                        that.$message.warning(res.message)
                    }
                }).finally(() => {
                    that.confirmLoading = false
                })
            },
            changeAppState(e, param) {
                modifyAppState(param).then(res => {
                    if (res.code == 200) {
                        this.loadData();
                    }
                })
            },
            getAvatarView: function (avatar) {
                return getFileAccessHttpUrl(avatar);
            },

            batchFrozen: function (status) {
                if (this.selectedRowKeys.length <= 0) {
                    this.$message.warning("请选择一条记录！");
                    return false;
                } else {
                    let ids = "";
                    let that = this;
                    let isAdmin = false;
                    that.selectionRows.forEach(function (row) {
                        if (row.username == "admin") {
                            isAdmin = true;
                        }
                    });
                    if (isAdmin) {
                        that.$message.warning("管理员账号不允许此操作,请重新选择！");
                        return;
                    }
                    that.selectedRowKeys.forEach(function (val) {
                        ids += val + ",";
                    });
                    that.$confirm({
                        title: "确认操作",
                        content: "是否" + (status == 1 ? "解冻" : "冻结") + "选中账号?",
                        onOk: function () {
                            frozenBatch({ids: ids, status: status}).then((res) => {
                                if (res.success) {
                                    that.$message.success(res.message);
                                    that.loadData();
                                    that.onClearSelected();
                                } else {
                                    that.$message.warning(res.message);
                                }
                            });
                        },
                    });
                }
            },
            handleMenuClick(e) {
                if (e.key == 1) {
                    this.batchDel();
                } else if (e.key == 2) {
                    this.batchFrozen(2);
                } else if (e.key == 3) {
                    this.batchFrozen(1);
                }
            },
            handleFrozen: function (id, status, username) {
                let that = this;
                //TODO 后台校验管理员角色
                if ("admin" == username) {
                    that.$message.warning("管理员账号不允许此操作！");
                    return;
                }
                frozenBatch({ids: id, status: status}).then((res) => {
                    if (res.success) {
                        that.$message.success(res.message);
                        that.loadData();
                    } else {
                        that.$message.warning(res.message);
                    }
                });
            },
            handleChangePassword(username) {
                this.$refs.passwordmodal.show(username);
            },
            passwordModalOk() {
                //TODO 密码修改完成 不需要刷新页面，可以把datasource中的数据更新一下
            },
            onSyncFinally({isToLocal}) {
                // 同步到本地时刷新下数据
                if (isToLocal) {
                    this.loadData();
                }
            },
        },
    };
</script>
<style scoped>
    @import "~@assets/less/common.less";
</style>
