<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-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column
        prop="hotelCode"
        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="cityName"
        header-align="center"
        align="center"
        label="酒店所在城市"
      >
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="联系电话"
      >
      </el-table-column>
      <el-table-column
        prop="hotelStatus"
        header-align="center"
        align="center"
        label="酒店营业状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hotelStatus === 0" size="small" type="success"
            >营业中</el-tag
          >
          <el-tag v-else size="small" type="warning">未营业</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="匹配状态">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            trigger="click"
            @show="getMatchHotelInfo(scope.row)"
            v-if="scope.row.shid"
          >
            <el-row>
              <el-form ref="form" :model="htAliHotelData" label-width="100px">
                <el-col :span="12">
                  <el-form-item label="酒店名称：">
                    {{ htAliHotelData.name }}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="标准酒店id：">
                    {{ htAliHotelData.shid }}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="卖家酒店id：">
                    {{ htAliHotelData.hid }}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="酒店地址：">
                    {{ htAliHotelData.address }}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="联系电话：">
                    {{ htAliHotelData.tel }}
                  </el-form-item>
                </el-col>
              </el-form>
            </el-row>
            <el-tag
              size="small"
              slot="reference"
              type="success"
              style="cursor: pointer"
            >
              已匹配
            </el-tag>
          </el-popover>
          <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">
          <span v-if="scope.row.hotelStatus == 0">
            <el-button type="text" size="small" @click="startMatch(scope.row)">
              人工匹配
            </el-button>
            <el-button type="text" size="small" @click="hotelRoom(scope.row)">
              房型
            </el-button>
          </span>
        </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="matchDialog" width="80%">
      <span>
        <el-row>
          <el-form ref="form" :model="waitMatchHotel" label-width="120px">
            <el-col :span="8">
              <el-form-item label="酒店名称：">
                {{ waitMatchHotel.hotelName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="酒店编码：">
                {{ waitMatchHotel.hotelCode }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="酒店地址：">
                {{ waitMatchHotel.address }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="酒店城市：">
                {{ waitMatchHotel.cityName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系电话：">
                {{ waitMatchHotel.phone }}
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <el-table
          :data="matchHotelData"
          border
          v-loading="dataListLoading"
          style="width: 100%;"
        >
          <el-table-column
            prop="name"
            align="center"
            label="酒店名称"
          ></el-table-column>
          <el-table-column
            prop="shid"
            align="center"
            label="酒店编码"
          ></el-table-column>
          <el-table-column
            prop="city"
            align="center"
            label="酒店城市"
          ></el-table-column>
          <el-table-column
            prop="address"
            align="center"
            label="地址"
          ></el-table-column>
          <el-table-column
            prop="tel"
            align="center"
            label="联系方式"
          ></el-table-column>
          <el-table-column
            prop="status_desc"
            align="center"
            label="酒店状态"
          ></el-table-column>
          <el-table-column prop="score" align="center" label="匹配度">
            <template slot-scope="scope">
              {{ getScore(scope.row.score) }}
            </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.status_code == 0 &&
                    scope.row.shid != waitMatchHotel.shid
                "
                @click="handleMatch(scope.row)"
              >
                确认匹配
              </el-button>

              <el-tag
                size="small"
                slot="reference"
                type="success"
                v-if="scope.row.shid == waitMatchHotel.shid"
              >
                已匹配
              </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 "./hthotelinfo-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        key: "",
        name: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      htAliHotelData: {},
      matchDialog: false,
      matchHotelData: [],
      waitMatchHotel: {}
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
    // this.$http({
    //   url: this.$http.adornUrl("/dt/store/list"),
    //   method: "get",
    //   params: this.$http.adornParams({
    //     page: 1,
    //     limit: 100
    //   })
    // }).then(({ data }) => {
    //   if (data && data.code === 0) {
    //     this.storeList = data.page.list || [];
    //   }
    // });
  },
  methods: {
    getScore(score) {
      return score ? Number(score).toFixed(2) : 0.0;
    },
    startMatch(row) {
      this.matchHotelData = [];
      this.matchDialog = true;
      this.dataListLoading = true;
      this.waitMatchHotel = JSON.parse(JSON.stringify(row));
      this.$http({
        url: this.$http.adornUrl(`/ht/hthotelinfo/getMatchHotels`),
        method: "get",
        params: this.$http.adornParams({
          hotelName: row.hotelName,
          address: row.address,
          city: row.city
        })
      }).then(({ data }) => {
        this.dataListLoading = false;
        if (data && data.code === 0) {
          this.matchHotelData = data.list || [];
        }
      });
    },
    handleMatch(row) {
      this.$confirm("确认匹配?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ht/hthotelinfo/manualMatchHotel"),
          method: "get",
          params: this.$http.adornParams({
            hotelCode: this.waitMatchHotel.hotelCode,
            partnerId: this.waitMatchHotel.partnerId,
            matchHotelData: row
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              type: "success",
              message: "匹配成功!"
            });
            this.matchDialog = false;
            this.getDataList();
          }
        });
      });
    },
    getMatchHotelInfo(row) {
      this.$http({
        url: this.$http.adornUrl(`/ht/hthotelinfo/matchInfo/${row.hotelCode}`),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 0 && data.htAliHotel) {
          this.htAliHotelData = data.htAliHotel;
        }
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/ht/hthotelinfo/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key
        })
      }).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();
    },
    // 房型
    hotelRoom(data) {
      localStorage.setItem("hotelCode", data.hotelCode);
      this.$router.push({
        name: "ht-htphysicalroom"
      });
    }
  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
