<template>
  <div class="weatherIceBox">
    <div class="weatherDiv">
      <div class="headTitle">
        <span>微气象转发列表（{{ weatherTable.length }}台）</span>
        <el-button type="primary" icon="el-icon-plus" @click="addWeather"
          >新增</el-button
        >
      </div>
      <el-table
        :data="weatherTable"
        border
        style="width: 100%"
        height="calc(100% - 42px)"
        v-loading="weatherLoading"
      >
        <el-table-column prop="cmdid" label="cmdid"> </el-table-column>
        <el-table-column prop="termId" label="termId"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="deleteText"
              @click.native.stop="handleWeatherDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="新增微气象转发"
      :visible.sync="weatherVisible"
      width="334px"
    >
      <el-form :model="weatherForm">
        <el-form-item label="装置编号">
          <el-autocomplete
            clearable
            v-model="weatherForm.cmdidVal"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="weatherVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmWeather">确 定</el-button>
      </span>
    </el-dialog>
    <div class="iceDiv">
      <div class="headTitle">
        <span>覆冰转发列表（{{ pullTable.length }}台）</span>
        <el-button type="primary" icon="el-icon-plus" @click="addIce"
          >新增</el-button
        >
      </div>
      <el-table
        :data="pullTable"
        border
        style="width: 100%"
        height="calc(100% - 42px)"
        v-loading="pullLoading"
      >
        <el-table-column prop="cmdid" label="cmdid"> </el-table-column>
        <el-table-column prop="termId" label="termId"> </el-table-column>
        <el-table-column prop="funcCode" label="funcCode"> </el-table-column>
        <el-table-column prop="point" label="point"> </el-table-column>
        <el-table-column prop="ccll" label="ccll"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="deleteText"
              @click.native.stop="handleIceDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="新增覆冰转发" :visible.sync="iceVisible" width="334px">
      <el-form :model="iceForm" label-width="80px">
        <el-form-item label="装置编号">
          <el-autocomplete
            clearable
            v-model="iceForm.cmdidVal"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="相位">
          <el-input
            placeholder="请输入内容"
            v-model="iceForm.funcCode"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label="point">
          <el-input placeholder="请输入内容" v-model="iceForm.point" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="初始拉力">
          <el-input placeholder="请输入内容" v-model="iceForm.ccll" clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="iceVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmIce">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getWeatherListApi,
  getPointListApi,
  addWeatherApi,
  addIcePointApi,
  delWeatherApi,
  delPointApi,
} from "@/utils/api/iceApi";
import { gettermAllList } from "@/utils/api/reportApi";
export default {
  name: "weatherIceConfig",
  data() {
    return {
      weatherTable: [], //天气
      weatherLoading: false,
      weatherVisible: false,
      weatherForm: {},
      termidOptions: [], //装置termid,
      selectVal: "",
      pullTable: [], //拉力
      pullLoading: false,
      iceVisible: false,
      iceForm: {},
    };
  },
  created() {
    this.getWeather();
    this.getPull();
  },
  computed: {},
  methods: {
    //查询天气转发
    getWeather() {
      this.weatherLoading = true;
      getWeatherListApi()
        .then((res) => {
          if (res.code == 200) {
            this.weatherTable = res.data;
            this.weatherLoading = false;
          } else {
            this.weatherLoading = false;
            this.$message({
              duration: 1500,
              showClose: true,
              message: res.msg,
              type: "success",
            });
          }
        })
        .catch((err) => {});
    },
    //查询拉力转发
    getPull() {
      this.pullLoading = true;
      getPointListApi()
        .then((res) => {
          if (res.code == 200) {
            this.pullLoading = false;
            this.pullTable = res.data;
          } else {
            this.pullLoading = false;
            this.$message({
              duration: 1500,
              showClose: true,
              message: res.msg,
              type: "success",
            });
          }
        })
        .catch((err) => {});
    },
    //添加天气
    addWeather() {
      this.weatherVisible = true;
    },

    //添加拉力
    addIce() {
      this.iceVisible = true;
    },
    //删除天气
    handleWeatherDelete(data) {
      console.log(data);
      this.$confirm("确定要删除该微气象装置的数据转发?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delWeatherApi({ id: data.id }).then((res) => {
            console.log(res.code);
            if (res.code == 200) {
              this.$message.success("删除成功");
              this.getWeather(); //刷新
            } else if (res.code == 400) {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    //删除覆冰拉力
    handleIceDelete(data) {
      console.log(data);
      this.$confirm("确定要删除该覆冰装置的数据转发?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delPointApi({ id: data.id }).then((res) => {
            console.log(res.code);
            if (res.code == 200) {
              this.$message.success("删除成功");
              this.getPull(); //刷新
            } else if (res.code == 400) {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    //获取装置termid
    // 异步查询建议
    async querySearch(queryString, cb) {
      // 调用您的接口函数，注意这里应该是模拟的接口调用，您需要根据实际情况调整
      this.getTermid({ pageNum: 1, pageSize: 30, cmdid: queryString })
        .then((response) => {
          // 假设接口返回的数据结构为 { data: { list: [{ id: ..., name: ... }, ...] } }
          const suggestions = response.data.list.map((item) => ({
            value: item.cmdid,
          }));
          cb(suggestions);
        })
        .catch((error) => {
          console.error("查询建议时出错:", error);
          cb([]);
        });
    },
    // 获取术语ID的函数，根据输入调用接口
    getTermid(params) {
      return new Promise((resolve, reject) => {
        // 这里模拟了一个延时调用，实际中应直接调用您的接口
        setTimeout(() => {
          gettermAllList(params)
            .then((res) => {
              console.log(res);
              this.termidOptions = res.data.list;
              resolve(res); // 解析返回的结果
            })
            .catch((err) => {
              console.error("接口调用失败:", err);
              reject(err); // 拒绝Promise
            });
        }, 100);
      });
    },
    // 处理选择建议
    handleSelect(item) {
      console.log(item);
      console.log(this.termidOptions);
      // 找到被选中建议的原始对象（这里假设termidOptions中保存了原始数据）
      const selectedOption = this.termidOptions.find(
        (option) => option.cmdid === item.value
      );
      if (selectedOption) {
        this.selectVal = selectedOption.id;
        console.log("选中的ID:", selectedOption.id); // 打印出选中的ID
      }
    },
    //新增微气象
    confirmWeather() {
      addWeatherApi({ termId: this.selectVal })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              duration: 1500,
              showClose: true,
              message: "新增转发成功",
              type: "success",
            });
            this.getWeather();
            this.selectVal = "";
            this.weatherVisible = false;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {});
    },
    //新增覆冰
    confirmIce() {
      addIcePointApi({
        termId: this.selectVal,
        ccll: this.iceForm.ccll,
        funcCode: this.iceForm.funcCode,
        point: this.iceForm.point,
      })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              duration: 1500,
              showClose: true,
              message: "新增转发成功",
              type: "success",
            });
            this.getPull();
            this.selectVal = "";
            this.iceVisible = false;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {});
    },
  },
};
</script>
<style lang="less">
.weatherIceBox {
  width: calc(100% - 24px);
  height: calc(100% - 24px);
  padding: 12px 12px;
  background: #fff;
  display: flex;
  .headTitle {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    justify-content: space-between;
  }
  .weatherDiv {
    width: 49%;
    margin-right: 2%;
  }
  .iceDiv {
    width: 49%;
  }
  .el-table::before,
  .el-table__fixed-right::before,
  .el-table__fixed::before {
    height: 1px;
  }
}
</style>
