<template>
  <el-dialog
    :title="pointDialogDataName"
    :visible.sync="pointDialogShow"
    width="70%"
    :before-close="bindingMonitorPointClose"
  >
    <el-row :gutter="20">
      <el-col :span="5">
        <div class="bindingMonitorPointTree">
          <el-tree
            :data="dialogPointTree"
            :props="defaultPropsMonitor"
            @node-click="handleMonitor"
          ></el-tree>
        </div>
      </el-col>
      <el-col :span="13">
        <div class="bindingMonitorPointDiv">
          <videoPlayer />
        </div>
      </el-col>
      <el-col :span="6">
        <div style="background: #eee; padding: 5px">
          {{ $t("site_management.PTZ") }}
        </div>
        <div class="PTZ-content">
          <img src="@/assets/images/PTZImg.png" alt="" class="PTZImg" />
          <div class="r-ti2-pos">
            <div @click="ptzBtn('UP', 'UP_STOP')"></div>
            <div @click="ptzBtn('UP_RIGHT', 'UP_RIGHT_STOP')"></div>
            <div @click="ptzBtn('RIGHT', 'RIGHT_STOP')"></div>
            <div @click="ptzBtn('DOWN_RIGHT', 'DOWN_RIGHT_STOP')"></div>
            <div @click="ptzBtn('DOWN', 'DOWN_STOP')"></div>
            <div @click="ptzBtn('DOWN_LEFT', 'DOWN_LEFT_STOP')"></div>
            <div @click="ptzBtn('LEFT', 'LEFT_STOP')"></div>
            <div @click="ptzBtn('UP_LEFT', 'UP_LEFT_STOP')"></div>
          </div>
        </div>
        <div class="r-ti3">
          <div class="r-ti3-d1">
            <img src="@/assets/images/searchImg.png" alt="" />
            <div class="r-ti3-d2">
              <div @click="ptzBtn('ZOOM_OUT', 'ZOOM_OUT_STOP')"></div>
              <div @click="ptzBtn('ZOOM_IN', 'ZOOM_IN_STOP')"></div>
            </div>
          </div>
          <div class="r-ti3-d1">
            <img src="@/assets/images/r2Img.png" alt="" />
            <div class="r-ti3-d2">
              <div @click="fiBtn('nearfocus', 'nearfocusstop')"></div>
              <div @click="fiBtn('farfocus', 'farfocusstop')"></div>
            </div>
          </div>
          <div class="r-ti3-d1">
            <img src="@/assets/images/r3Img.png" alt="" />
            <div class="r-ti3-d2">
              <div @click="fiBtn('apertureoff', 'apertureoffstop')"></div>
              <div @click="fiBtn('apertureon', 'apertureonstop')"></div>
            </div>
          </div>
        </div>
        <div class="r-ti4">
          <div style="display: flex">
            <el-select
              v-model="presetDotNum"
              :placeholder="$t('site_management.PleaseSelectAPresetPoint')"
              @change="presetDotBtn"
            >
              <el-option
                :label="item.dictLabel"
                :value="item.dictValue"
                v-for="item in listByEqIdAndChanneData"
                :key="item.dictValue"
              >
              </el-option>
            </el-select>
            <img
              src="@/assets/images/playImg.png"
              @click="editPresetDot1"
              title="编辑预置点名称"
            />
            <img src="@/assets/images/addImg.png" alt="" @click="popoverOpen" />
            <el-popover
              placement="top"
              width="300"
              v-model="popovervisible"
              trigger="click"
            >
              <el-input placeholder="请输入内容" v-model="newpresetName" />
              <div class="popover-footer">
                <el-button
                  size="mini"
                  type="primary"
                  @click="popovervisibleShow"
                  >取消</el-button
                >
                <el-button size="mini" type="primary" @click="setPresetDot1"
                  >确定</el-button
                >
              </div>
            </el-popover>
          </div>
          <div style="display: flex; margin-top: 20px">
            <el-input
              v-model="presetName"
              :placeholder="$t('site_management.PleaseEnterTheContent')"
            ></el-input>
            <img
              src="@/assets/images/setImg.png"
              alt=""
              title="编辑预置点名称"
              @click="editPresetDot2"
            />
          </div>
        </div>
      </el-col>
    </el-row>
    <el-table
      :data="bindingMonitorPointTableData"
      style="width: 100%"
      @header-click="clickFun"
    >
      <el-table-column :label="$t('site_management.ActionType')" align="center">
        <template slot-scope="scope">
          <el-select v-model="scope.row.actionType">
            <el-option label="拍照" value="1" />
            <el-option label="录像" value="2" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('site_management.RecordingDuration')"
        align="center"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.videoTime"
            v-if="scope.row.actionType == 2"
            class="centered-input"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('site_management.NumberOfPhotosTaken')"
        align="center"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.photoCount"
            v-if="scope.row.actionType == 1"
            class="centered-input"
          ></el-input>
          <!-- <span v-if="scope.row.actionType == 1">1</span> -->
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('site_management.PhotoInterval')"
        align="center"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.photoSeper"
            v-if="scope.row.actionType == 1"
            class="centered-input"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :render-header="(h, obj) => renderHeader(h, obj, $t('share.addTo'))"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="removeAction(scope.$index)"
            >移除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div class="add-btn add-bth1 card-page">
        <el-button type="primary" @click="saveDot1">确定</el-button>

        <el-button @click="bindingMonitorPointClose">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import videoPlayer from "@/components/videoPlayer/index.vue";
import {
  vl,
  listByEqIdAndChannelId,
  realtime,
  PZT,
  fi,
  goto,
  issue,
  bindingPreset,
} from "@/api/point/site_management";
export default {
  components: { videoPlayer },
  props: ["pointDialogDataName", "pointDialogShow", "dialogProp"],
  data() {
    return {
      bindingMonitorPointTitle: "绑定可见光检测点",
      // pointDialogShow: false, 
      dialogPointTree: [],
      defaultPropsMonitor: {
        children: "children",
        label: "name",
      },
      listByEqIdAndChanneData: [],
      bindingMonitorPointTableData: [],
      presetDotNum: "",
      PZTData: {},
      presetName: "",
      popovervisible: false,
      newpresetName: "",
      presetData: {
        presetPosCode: "",
        channelId: "",
        eqpbookId: "",
        patrolpointId: "",
        stdparolPointId: "",
        deviceId: "",
        presetPosName: "",
        stopTime: "",
        channelImg: "",
        description: "",
        presetActionList: [],
        presetParamList: [],
        presetPointList: [],
      },
    };
  },
  methods: {
    bindingMonitorPointClose() {
      // this.pointDialogShow = false;
      this.$emit("site_videoDialogClose",false);
    },
    // 绑定可见光监测点点击树事件
    handleMonitor(e) {
      console.log(e, 111, this.dialogProp);
      if (e.eqpbookId) {
        this.presetData.patrolpointId = this.dialogProp.patrolpointId;
        this.presetData.stdparolPointId = this.dialogProp.stdparolPointId;
        this.presetData.deviceId = this.dialogProp.deviceId;
        this.presetData.eqpbookId = e.eqpbookId;
        this.presetData.channelId = e.channelId;

        this.PZTData = e;
        listByEqIdAndChannelId({
          channelCode: e.channelCode,
          patroldeviceCode: e.patroldeviceCode,
        }).then((res) => {
          if (res.code == 200) {
            this.listByEqIdAndChanneData = res.data;
          }
        });
        realtime({
          patroldeviceCode: e.patroldeviceCode,
          channelCode: e.channelCode,
        }).then((res) => {
          if (res.code == 200) {
            // this.bindingMonitorPointTableData=res.data;
            console.log(res, 1111);
          }
        });
      }
    },
    renderHeader(h, { column, $index }, type) {
      let that = this;
      return h("div", [
        // 列名称
        h("span", column.label),
        // 按钮
        h(
          "el-button",
          {
            props: {
              type: "text",
              size: "small",
            },
            style: "margin-left: 5px;",
            on: {
              click: function () {
                that.clickButton(type);
              },
            },
          },
          this.$t("share.addTo")
        ),
      ]);
    },
    clickButton(type) {
      // console.log("我点击了" + type + "的列");
      this.bindingMonitorPointTableData.push({
        actionType: "",
        videoTime: "",
        photoCount: "1",
        photoSeper: "",
      });
    },
    clickFun() {},

    init() {
      vl().then((res) => {
        if (res.code == 200) {
          this.dialogPointTree = res.data;
        }
      });
    },
    ptzBtn(start, stop) {
      let startData = {
        speed: 5,
        patroldeviceCode: this.PZTData.patroldeviceCode,
        channelCode: this.PZTData.channelCode,
        ptzControlType: start,
      };
      let stopData = {
        speed: 5,
        patroldeviceCode: this.PZTData.patroldeviceCode,
        channelCode: this.PZTData.channelCode,
        ptzControlType: stop,
      };
      if (startData.patroldeviceCode && startData.channelCode) {
        PZT(startData).then((res) => {
          if (res.code) {
            console.log(res, "start");
          }
        });
        PZT(stopData).then((res) => {
          if (res.code) {
            console.log(res, "stop");
          }
        });
      }
    },
    fiBtn(start, stop) {
      let startData = {
        speed: 5,
        patroldeviceCode: this.PZTData.patroldeviceCode,
        channelCode: this.PZTData.channelCode,
        ptzControlType: start,
      };
      let stopData = {
        speed: 5,
        patroldeviceCode: this.PZTData.patroldeviceCode,
        channelCode: this.PZTData.channelCode,
        ptzControlType: stop,
      };
      if (startData.patroldeviceCode && startData.channelCode) {
        fi(startData).then((res) => {
          if (res.code) {
            console.log(res, "start");
          }
        });
        fi(stopData).then((res) => {
          if (res.code) {
            console.log(res, "stop");
          }
        });
      }
    },
    presetDotBtn(value) {
      this.presetData.presetPosCode = value;
      this.listByEqIdAndChanneData.forEach((item) => {
        if (item.dictValue == this.presetDotNum) {
          this.presetName = item.dictLabel;
        }
      });
    },
    // 编辑预置点名称
    editPresetDot1() {
      let data = {
        patroldeviceCode: this.PZTData.patroldeviceCode,
        channelCode: this.PZTData.channelCode,
        presetDotNum: this.presetDotNum,
        name: this.presetName,
      };
      goto(data).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    popovervisibleShow() {
      this.popovervisible = false;
      this.newpresetName = "";
    },
    setPresetDot1() {
      let data = {
        presetCode: this.presetDotNum,
        name: this.presetName,
        channelCode: this.PZTData.channelCode,
        patroldeviceCode: this.PZTData.patroldeviceCode,
      };
      issue(data).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.popovervisible = false;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    popoverOpen() {
      this.popovervisible = true;
    },
    // 设置预设点名称
    editPresetDot2() {
      let data = {
        presetCode: this.presetDotNum,
        name: this.newpresetName,
        channelCode: this.PZTData.channelCode,
        patroldeviceCode: this.PZTData.patroldeviceCode,
      };
    },
    // 表格按钮移除
    removeAction() {
      this.bindingMonitorPointTableData.splice(index, 1);
    },
    // 保存
    saveDot1() {
      this.presetData.presetPosName = this.presetName;
      this.presetData.presetActionList = this.bindingMonitorPointTableData;
      bindingPreset(this.presetData).then((res) => {
        console.log(res, 11111);
        if (res.code == 200) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.presetData = {
            presetPosCode: "",
            channelId: "",
            eqpbookId: "",
            patrolpointId: "",
            stdparolPointId: "",
            deviceId: "",
            presetPosName: "",
            stopTime: "",
            channelImg: "",
            description: "",
            presetActionList: [],
            presetParamList: [],
          };
          this.pointDialogShow=false;
          this.$emit('site_saveDot1',true)
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="scss" scoped>
.bindingMonitorPointTree {
  width: 100%;
  height: 37vh;
  overflow-y: scroll;
}
.bindingMonitorPointDiv {
  width: 100%;
  height: 40vh;
  // background: red;
}
.PTZ-content {
  width: 170px;
  margin: 10px auto;
  position: relative;
  img {
    width: 100%;
  }
}
.r-ti2-pos {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  div {
    position: absolute;
    z-index: 999;
    width: 20px;
    height: 15px;
    cursor: pointer;
  }
  div:first-child {
    top: 32px;
    left: 75px;
  }
  div:nth-child(2) {
    top: 50px;
    left: 102px;
  }
  div:nth-child(3) {
    top: 77px;
    right: 30px;
  }
  div:nth-child(4) {
    bottom: 56px;
    left: 102px;
  }
  div:nth-child(5) {
    bottom: 36px;
    left: 75px;
  }
  div:nth-child(6) {
    bottom: 56px;
    right: 100px;
  }
  div:nth-child(7) {
    bottom: 80px;
    left: 30px;
  }
  div:nth-child(8) {
    top: 50px;
    right: 102px;
  }
}
.r-ti3 {
  width: 100%;
  display: flex;
  padding-top: 10px;
  -webkit-box-pack: justify;
  justify-content: space-between;
}
.r-ti3-d1 {
  width: 76px;
  position: relative;
  img {
    width: 100%;
  }
  .r-ti3-d2 {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    div:first-child {
      width: 50%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      cursor: pointer;
    }
    div:nth-child(2) {
      width: 50%;
      height: 100%;
      position: absolute;
      top: 0;
      right: 0;
      cursor: pointer;
    }
  }
}
.r-ti4 {
  padding-top: 20px;
  img {
    width: 24px;
    height: 24px;
    position: relative;
    top: 6px;
    cursor: pointer;
  }
}
.popover-footer {
  width: 100%;
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>