<template>
  <el-tabs
    v-model="activeName"
    @tab-click="handleClick"
    type="card"
    v-loading="loading"
  >
    <el-tab-pane label="查询实际参数" name="1">
      <div class="captureBoxVideo">
        <div class="flexnr">
          <div class="wt80">通道:</div>
          <el-select
            v-model="selaccess"
            placeholder="请选择"
            class="mr20"
            @change="clearform"
          >
            <el-option
              v-for="item in accesslist"
              :key="item.channelid"
              :label="
                item.alias !== null && item.alias !== ''
                  ? item.alias
                  : item.channelname
              "
              :value="item.channelid"
            >
              {{
                item.alias !== null && item.alias !== ""
                  ? item.alias
                  : item.channelname
              }}
            </el-option>
          </el-select>
        </div>
        <div class="flexno bt30">
          <div class="wt80">视频分辨率:</div>
          <el-select
            v-model="capturenr.videoFormat"
            class="wt280"
            :disabled="true"
          >
            <el-option
              v-for="item in ratiolist"
              :key="item.id"
              :label="item.id + '  (' + item.name + ')'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="flexno bt30">
          <div class="wt80">预置位:</div>
          <el-input
            v-model="capturenr.preset"
            :disabled="true"
            class="wt280"
          ></el-input>
        </div>
        <div class="flexno bt30">
          <div class="wt80">录制时长:</div>
          <el-input
            v-model="capturenr.videoTime"
            :disabled="true"
            class="wt280"
          >
            <template slot="append">s (秒)</template>
          </el-input>
        </div>
        <!-- <el-button
          class="searchImage"
          :loading="searchloading"
          type="primary"
          @click="inquirebtn()"
          >查询</el-button
        > -->
        <el-button
          class="searchImage"
          :loading="searchloading"
          type="primary"
          @click="videoSearch()"
          >查询</el-button
        >
        <p class="looktime" v-if="lookTime && showLookTime">
          {{ $moment(lookTime).format("YYYY-MM-DD HH:mm:ss") }}
        </p>
      </div>
    </el-tab-pane>
    <el-tab-pane label="设置参数" name="2">
      <div class="captureBoxVideo">
        <el-form
          :model="setForm"
          :rules="rules"
          ref="setForm"
          label-width="120px"
          class="captureForm"
        >
          <el-form-item label="通道" prop="channelId">
            <el-select
              v-model="setForm.channelId"
              placeholder="请选择"
              class="mr20"
            >
              <el-option
                v-for="item in accesslist"
                :key="item.channelid"
                :label="
                  item.alias !== null && item.alias !== ''
                    ? item.alias
                    : item.channelname
                "
                :value="item.channelid"
              >
                {{
                  item.alias !== null && item.alias !== ""
                    ? item.alias
                    : item.channelname
                }}
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="视频分辨率" prop="videoFormat">
            <el-select
              v-model="setForm.videoFormat"
              placeholder="请选择"
              class="mr20"
            >
              <el-option
                v-for="item in ratiolist"
                :key="item.id"
                :label="item.id + '  (' + item.name + ')'"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预置位" prop="preset">
            <el-input-number
              v-model="setForm.preset"
              :min="1"
              :max="255"
              :disabled="true"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="录制时长 s(秒)" prop="videoTime">
            <el-input-number
              v-model="setForm.videoTime"
              :min="1"
              :max="100"
            ></el-input-number>
          </el-form-item>
          <el-form-item>
            <!-- <el-button type="primary" @click="submitbtn('setForm')"
              >确认</el-button
            > -->
            <el-button
              type="primary"
              @click="videoSet('setForm')"
              :loading="setloading"
              >确认</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-tab-pane>
  </el-tabs>
</template>
<script>
import {
  getChannelListJoggle,
  getVideoParamApi,
  setVideoParamApi,
  getVideoParamnrApi,
  getResolutionRatio,
  setTermCamera,
  getTermCameraRequest,
} from "@/utils/api/index";
export default {
  data() {
    return {
      loading: false,
      isShow: false,
      searchloading: false,
      setloading: false,
      activeName: "1", //选项卡
      accesslist: [], //通道选择器
      ratiolist: [],
      hnratiolist: [
        {
          id: 1,
          name: "480P",
        },
        {
          id: 2,
          name: "720p",
        },
        {
          id: 3,
          name: "1080P",
        },
        {
          id: 4,
          name: "QVGA",
        },
        {
          id: 5,
          name: "VGA",
        },
      ], //分辨率
      selaccess: "", //选中的通道
      capturenr: {}, //查询后的内容
      setForm: {
        preset: 255,
      }, //设置参数
      rules: {
        channelId: [{ required: true, message: "请选择通道", trigger: "blur" }],
        videoTime: [
          { required: true, message: "请输入录制时长", trigger: "blur" },
        ],
        preset: [{ required: true, message: "请输入预置位", trigger: "blur" }],
        videoFormat: [
          { required: true, message: "请选择视频分辨率", trigger: "blur" },
        ],
      },
      timer: null,
      i: 0,
      seltermid: "", //装置id
      selcmdId: "", //cmdId
      requestId: "",
      onlinestatus: "",
      lookTime: "",
      showLookTime: false,
    };
  },
  mounted() {},
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
      this.capturenr = {};
      this.lookTime = "";
    },
    //获取设备通道
    getSingleAccess(val) {
      this.setForm = {
        preset: 255,
      };
      console.log(val);
      this.seltermid = val.id;
      this.selcmdId = val.cmdId;
      this.onlinestatus = val.onlinestatus;
      getChannelListJoggle({ termid: this.seltermid })
        .then((res) => {
          this.accesslist = res.data.list;
          this.selaccess = res.data.list[0].channelid;
          this.$set(this.setForm, "channelId", res.data.list[0].channelid);
        })
        .catch((err) => {});
    },
    //获取分辨率
    getRatio() {
      getResolutionRatio({ type: 1 })
        .then((res) => {
          this.ratiolist = this.hnratiolist;
          this.$set(this.setForm, "videoFormat", this.ratiolist[0].id);
        })
        .catch((err) => {});
    },

    //命令调用videoSearch
    // --act=videoparams [0xD5] --flag=[Request Set Flag, default is set=1, 0: request] --channel=[Channel No] --preset=[Preset No] format=[Video Format: 1 QVGA 2 VGA 3 480p 4 720P 5 1080P] time=[seconds]
    videoSearch() {
      if (this.onlinestatus == 1) {
        this.searchloading = true;
        let params = [
          {
            name: "act",
            value: "videoparams",
          },
          {
            name: "flag",
            value: 0,
          },
          {
            name: "channel",
            value: this.selaccess,
          },
          {
            name: "preset",
            value: 255,
          },
          {
            name: "format",
            value: "",
          },
          {
            name: "time",
            value: "",
          },
        ];
        this.setTermFn(params);
      } else {
        this.$message({
          duration: 1500,
          showClose: true,
          message: "装置下线，发送指令失败",
          type: "error",
        });
      }
    },
    videoSet(setForm) {
      console.log(this.setForm);
      if (this.onlinestatus == 1) {
        this.setloading = true;
        let params = [
          {
            name: "act",
            value: "videoparams",
          },
          {
            name: "flag",
            value: 1,
          },
          {
            name: "channel",
            value: this.setForm.channelId,
          },
          {
            name: "preset",
            value: 255,
          },
          {
            name: "format",
            value: this.setForm.videoFormat,
          },
          {
            name: "time",
            value: this.setForm.videoTime,
          },
        ];
        this.setTermFn(params);
      } else {
        this.$message({
          duration: 1500,
          showClose: true,
          message: "装置下线，发送指令失败",
          type: "error",
        });
      }
    },
    setTermFn(dataParams) {
      setTermCamera({
        termId: this.seltermid,
        list: dataParams,
      })
        .then((res) => {
          console.log(res);
          this.requestid = res.data.requestId;
          this.lookTime = res.data.date;
          //this.getinfo1();

          clearInterval(this.timer);
          this.timer = window.setInterval(() => {
            this.getinfo();
            this.i++;
          }, 1000);
          // this.timer = setInterval(() => {
          //   this.getinfo();
          //   this.i++;
          // }, 1000);
        })
        .catch((err) => {});
    },
    getinfo() {
      getTermCameraRequest({ requestid: this.requestid })
        .then((res) => {
          if (res.data.success == 1) {
            window.clearInterval(this.timer);
            this.timer = null;
            this.i = 0;
            console.log("终止轮询");
            // && res.data.result == 255
            this.searchloading = false;
            this.setloading = false;
            console.log(JSON.parse(res.data.data));
            this.capturenr = JSON.parse(res.data.data);
            this.$message({
              duration: 1500,
              showClose: true,
              message: "信息已更新",
              type: "success",
            });
            this.showLookTime = true;
          } else if (this.i > 9) {
            window.clearInterval(this.timer);
            this.timer = null;
            this.i = 0;
            this.searchloading = false;
            this.setloading = false;
            this.$message({
              duration: 1500,
              showClose: true,
              message: "暂未获取到信息，请稍后再试！！",
              type: "warning",
            });
          }
        })
        .catch();
    },
    clearform() {
      console.log("清除其他数据");
      this.capturenr = {};
      this.setForm = {};
      this.searchloading = false;
      this.setloading = false;
    },
    clearData() {
      this.capturenr = {};
      this.setForm = {};
      this.searchloading = false;
      this.setloading = false;
      this.lookTime = "";
      this.showLookTime = false;
    },
  },
};
</script>
<style lang="less">
.captureBoxVideo {
  width: 400px;
  margin: 0 auto;
  margin-top: 24px;
  .flexnr {
    display: flex;
    height: 32px;
    line-height: 32px;
    margin-bottom: 18px;
    .wt80 {
      text-align: right;
      float: left;
      font-size: 14px;
      color: #606266;
      margin: 0 12px 0 0;
      box-sizing: border-box;
      width: 100px;
      display: block;
    }
    .mr20 {
      flex: 1;
    }
  }
  .searchImage {
    margin-left: 110px;
  }
  .looktime {
    margin-top: 6px;
    margin-left: 110px;
  }
  .flexno {
    display: flex;
    height: 32px;
    line-height: 32px;
    margin-bottom: 18px;
    .wt80 {
      text-align: right;
      float: left;
      font-size: 14px;
      color: #606266;
      margin: 0 12px 0 0;
      box-sizing: border-box;
      width: 100px;
      display: block;
    }
    .wt280 {
      flex: 1;
    }
  }
  .captureForm {
    .el-form-item__content {
    }
  }
}
</style>
