<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('op:htlgroup:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('op:htlgroup:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
        <el-button
          v-if="isAuth('op:htlgroup:delete')"
          type="danger"
          @click="deleteHandleStore()"
          :disabled="dataListSelections.length <= 0"
          >批量店铺删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="dtName"
        header-align="center"
        align="center"
        label="分销商名称"
      >
      </el-table-column>
      <el-table-column
        prop="stName"
        header-align="center"
        align="center"
        label="店铺名称"
      >
      </el-table-column>
      <el-table-column
        prop="groupName"
        header-align="center"
        align="center"
        label="分组名称"
      >
      </el-table-column>
      <el-table-column
        prop="hotelName"
        header-align="center"
        align="center"
        label="酒店名称"
      >
      </el-table-column>
      <el-table-column
        prop="address"
        header-align="center"
        align="center"
        label="地址"
      >
      </el-table-column>
      <el-table-column
        prop="isUp"
        header-align="center"
        align="center"
        label="0未推送/1已推送"
      >
      </el-table-column>

      <el-table-column align="center" label="匹配状态">
        <template slot-scope="scope">
          <el-tag size="small" type="success" v-if="scope.row.shid">
            已匹配
          </el-tag>
          <el-tag v-else size="small" type="warning">未匹配</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="hotelRoom(scope.row)">
            房型
          </el-button>
          <el-button type="text" size="small" @click="push(scope.row)">
            推送
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandleStore(scope.row.hotelId)"
            >店铺删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-dialog title="房型信息" :visible.sync="roomDialog" width="80%">
      <span>
        <el-table
          :data="roomList"
          border
          v-loading="dataListLoading"
          style="width: 100%;"
        >
          <el-table-column prop="roomName" align="center" label="房型名称">
          </el-table-column>
          <el-table-column prop="maxNum" align="center" label="最大入住人数">
          </el-table-column>
          <el-table-column prop="roomSize" align="center" label="房间大小">
          </el-table-column>
          <el-table-column prop="roomFloor" align="center" label="房间所在楼层">
          </el-table-column>
          <el-table-column prop="windows" align="center" label="房间窗户情况">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.windows === 0" size="small" type="warning"
                >无窗</el-tag
              >
              <el-tag v-if="scope.row.windows === 1" size="small" type="success"
                >有窗</el-tag
              >
              <el-tag v-if="scope.row.windows === 2" size="small" type="success"
                >部分有窗</el-tag
              >
              <el-tag v-if="scope.row.windows === 3" size="small" type="success"
                >暗窗</el-tag
              >
              <el-tag v-if="scope.row.windows === 4" size="small" type="success"
                >部分暗窗</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column align="center" width="80" label="匹配状态">
            <template slot-scope="scope">
              <el-tag size="small" v-if="scope.row.srid" type="success">
                已匹配
              </el-tag>
              <el-tag size="small" v-else type="warning">
                未匹配
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" width="120" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="startMatch(scope.row)"
              >
                人工匹配
              </el-button></template
            >
          </el-table-column>
        </el-table>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="roomDialog = false">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="人工匹配" :visible.sync="matchDialog" width="80%">
      <span>
        <el-row>
          <el-form ref="form" :model="waitMatchRoom" label-width="120px">
            <el-col :span="6">
              <el-form-item label="房型名称：">
                {{ waitMatchRoom.roomName }}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="房型编码：">
                {{ waitMatchRoom.roomCode }}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="最大入住人数：">
                {{ waitMatchRoom.maxNum }}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="床型：">
                {{ waitMatchRoom.bedType }}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="所在楼层：">
                {{ waitMatchRoom.roomFloor }}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="房间窗户情况：">
                {{
                  waitMatchRoom.windows == 0
                    ? "无窗"
                    : waitMatchRoom.windows == 1
                    ? "有窗"
                    : waitMatchRoom.windows == 2
                    ? "部分有窗"
                    : waitMatchRoom.windows == 3
                    ? "暗窗"
                    : waitMatchRoom.windows == 4
                    ? "部分暗窗"
                    : ""
                }}
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <el-table
          :data="matchRoomData"
          border
          v-loading="dataListLoading"
          style="width: 100%;"
        >
          <el-table-column
            prop="name"
            align="center"
            label="房型名称"
          ></el-table-column>
          <el-table-column
            prop="srid"
            align="center"
            label="房型编码"
          ></el-table-column>
          <el-table-column
            prop="bed_type"
            align="center"
            label="床型"
          ></el-table-column>
          <el-table-column
            prop="window_type"
            align="center"
            label="房间窗户情况"
          >
            <template slot-scope="scope">
              {{
                scope.row.window_type == 0
                  ? "无窗"
                  : scope.row.window_type == 1
                  ? "有窗"
                  : scope.row.window_type == 2
                  ? "部分有窗"
                  : scope.row.window_type == 3
                  ? "暗窗"
                  : scope.row.window_type == 4
                  ? "部分暗窗"
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" width="150" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                v-if="scope.row.srid != waitMatchRoom.srid"
                @click="handleMatch(scope.row)"
              >
                确认匹配
              </el-button>

              <el-tag size="small" slot="reference" type="success" v-else>
                已匹配
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="matchDialog = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./htlgroup-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        key: "",
        groupid: this.$route.params.group.id,
        blonPlantform: this.$route.params.group.blonPlantform,
        storeid: this.$route.params.group.storeId,
        dtid: this.$route.params.group.dtId
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      roomDialog: false,
      roomData: {},
      roomList: [],
      matchDialog: false,
      waitMatchRoom: {},
      matchRoomData: []
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    let group = JSON.parse(JSON.stringify(this.$route.params.group));
    if (group) {
      this.dataForm = {
        key: "",
        groupid: group.id,
        blonPlantform: group.blonPlantform,
        storeid: group.storeId,
        dtid: group.dtId
      };
      this.getDataList();
    }
  },
  methods: {
    // 推送
    push(row) {
      this.$confirm(`确定推送吗`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/op/htlgroup/push`),
          method: "get",
          params: this.$http.adornParams({
            hotelCode: row.hotelId,
            storeId: this.dataForm.storeid
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success("推送成功");
            this.getDataList();
          } else {
            this.$message.error("推送异常");
          }
        });
      });
    },
    handleMatch(row) {
      this.$confirm("确认匹配?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ht/hthotelinfo/manualMatchRoom"),
          method: "get",
          params: this.$http.adornParams({
            hotelCode: this.waitMatchRoom.hotelCode,
            storeId: this.dataForm.storeid,
            roomCode: this.waitMatchRoom.roomCode,
            matchRoomData: row
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              type: "success",
              message: "匹配成功!"
            });
            this.matchDialog = false;
            this.hotelRoom(this.roomData);
          }
        });
      });
    },
    // 房型
    hotelRoom(row) {
      this.roomDialog = true;
      this.roomData = JSON.parse(JSON.stringify(row));
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/ht/htphysicalroom/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 100,
          hotelCode: row.hotelId
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.roomList = data.page.list;
        }
        this.dataListLoading = false;
      });
    },
    startMatch(row) {
      this.matchDialog = true;
      this.waitMatchRoom = JSON.parse(JSON.stringify(row));
      this.$http({
        url: this.$http.adornUrl("/ht/hthotelinfo/getMatchRooms"),
        method: "get",
        params: this.$http.adornParams({
          storeId: this.dataForm.storeid,
          hotelCode: row.hotelCode
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.matchRoomData = data.list;
        }
        this.dataListLoading = false;
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/op/htlgroup/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          groupId: this.dataForm.groupid,
          dtId: this.dataForm.dtid,
          storeId: this.dataForm.storeid,
          blonPlantform: this.dataForm.blonPlantform
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle() {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(
          this.dataForm.dtid,
          this.dataForm.storeid,
          this.dataForm.groupid,
          this.dataForm.blonPlantform
        );
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/op/htlgroup/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 店铺删除
    deleteHandleStore(hotelId) {
      var hotelIds = hotelId
        ? [hotelId]
        : this.dataListSelections.map(item => {
            return item.hotelId;
          });
      var dict = {
        hotelIds: hotelIds,
        groupId: this.dataForm.groupid
      };
      console.info(hotelIds);
      this.$confirm(
        `确定对[id=${hotelIds.join(",")}]进行[${
          hotelId ? "删除" : "批量删除"
        }]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/op/htlgroup/deleteStore"),
          method: "post",
          data: this.$http.adornData(dict, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
