<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="3">
            <a-form-item label="">
              <a-input
                placeholder="名称"
                v-model="queryParam.hourRoomName"
                style="width: 200px"
              ></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-dropdown>
    </div>
    <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="idSlot" slot-scope="text, record">
            <div style="text-align:left;">1、开房后{{ record.afterOpenRoom?record.afterOpenRoom:0 }}分钟内按{{ record.afterOpenRoomPay?record.afterOpenRoomPay:0 }}元收取</div>
            <div style="text-align:left;">
                2、超时之后每{{ record.exceedTime?record.exceedTime:0 }}分钟收取{{ record.exceedPay?record.exceedPay:0 }}元，
                不足{{ record.exceedTime?record.exceedTime:0 }}分钟，超过{{ record.notReachExceedTime?record.notReachExceedTime:0 }}分钟加收{{ record.notReachExceedTimePay?record.notReachExceedTimePay:0 }}元
            </div>
            <div style="text-align:left;">
                3、 {{ record.maxExtraPay?record.maxExtraPay:0 }}元封顶 注意：0元视为不封顶
            </div>
            <div style="text-align:left;">
                4、消费超过{{ record.limitTimeTransferAllDay?record.limitTimeTransferAllDay:0 }}分钟，自动转为全天房 注意：0分钟视为不自动转全天房
            </div>
      </template>

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

          <a-divider type="vertical" />
          <a @click="handleRelation(record)">关联</a>
          <a-divider type="vertical" />
          <a-popconfirm
                  title="确定删除吗?"
                  @confirm="() => handleDelete(record.id)"
                >
                  <a>删除</a>
                </a-popconfirm>

          
        </span>
      </a-table>
    </div>
    <modal-form ref="modalForm" @ok="modalFormOk"></modal-form>
    <relation-form ref="relationForm" @ok="relationOk"></relation-form>
  </a-card>
</template>
  
<script>
import { JeecgListMixin } from "@/mixins/JeecgListMixin";
import modalForm from "./hourRoomFormModal.vue"; // todo roomLayoutForm 需要替换成房型的表单弹窗
import relationForm from "./hourLayoutRelation.vue"; // todo roomLayoutForm 需要替换成房型的表单弹窗
// import RoomLayoutDetailModal from "./RoomLayoutFormDetailModal.vue";

// import RoomLayoutPriceModal from "./RoomLayoutPriceModal.vue";

import { modifyAppState } from "@/api/roomLayout";
import { filterObj } from "@/utils/util";
let hotelInfo = JSON.parse(localStorage.getItem("storeInfo"));
export default {
  name: "roomLayoutList",
  mixins: [JeecgListMixin],
  components: {
    modalForm,
    relationForm
    // RoomLayoutPriceModal,
    // RoomLayoutDetailModal,
  },
  data() {
    return {
      queryParam: {
        hourRoomName:''
      },
      // 分页参数
      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: "hotelName",
        },
        {
          title: "名称",
          align: "center",
          dataIndex: "hourRoomName",
        },
        {
          title: "规则",
          align: "center",
          dataIndex: "id",
          scopedSlots: { customRender: "idSlot" },
        },
        {
          title: "更新时间",
          align: "center",
          dataIndex: "updateAt",
        },
        {
          title: "操作",
          dataIndex: "action",
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: { customRender: "action" },
        },
      ],
      url: {
        // list: 'org.jeecg.modules.business/busMarketMember/list',
        list: "/rooms/cesHourRoomRule/list",
        delete: "/rooms/cesHourRoomRule/delete",
        deleteBatch: "/rooms/cesHourRoomRule/batchDelete",
        exportXlsUrl: "/rooms/cesRoomLayout/exportXls",
        importExcelUrl: "rooms/cesRoomLayout/importExcel",
      },

      dictOptions: {},
      superFieldList: [],
      selectedRowKeys: [],
      isorter: {
        column: "createTime",
        order: "desc",
      },
    };
  },
  created() {
    // this.loadData()
  },
  methods: {
    handleRelation(data) {
        this.$refs.relationForm.setData(data);
    },
    relationOk() {
        this.$refs.relationForm.visible = false
        this.loadData()
    },
    onSaveOk() {
      this.loadData();
    },
    onPriceSave() {},
    handlePriceManager(record) {
      this.$refs.priceModal.setRaw(record.id, record.name);
      this.$refs.priceModal.visible = true;
    },
    handleDetailSetting(record) {
      this.$refs.detailModal.setRaw(record);
      this.$refs.detailModal.visible = true;
    },
    changeState(e) {
      console.log(e);
    },
    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>