<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.videoclouddevice_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,pageByBuilding} from "../../api/smartapplication/videoclouddevice.js";
  import {mapGetters} from "vuex";

  export default {
    props: {
      buildingId: {
        type: String
      }
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          menu: false,
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "设备编号",
              prop: "deviceId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备编号",
                trigger: "blur"
              }]
            },
            {
              label: "设备名称",
              prop: "deviceName",
              rules: [{
                required: true,
                message: "请输入设备名称",
                trigger: "blur"
              }]
            },
            {
              label: "设备号",
              prop: "deviceNum",
              rules: [{
                required: true,
                message: "请输入设备号",
                trigger: "blur"
              }]
            },
            {
              label: "设备密码",
              prop: "devicePassword",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备密码",
                trigger: "blur"
              }]
            },
            {
              label: "设备类型",
              prop: "deviceType",
              search: true,
              dataType: "number",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=video_device_type",
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              rules: [{
                required: true,
                message: "请输入设备类型(132:IPC;111:DVR;118:NVR,114:解码器)",
                trigger: "blur"
              }]
            },
            {
              label: "型号",
              prop: "modelSequence",
              hide: true,
              rules: [{
                required: true,
                message: "请输入型号",
                trigger: "blur"
              }]
            },
            {
              label: "协议类型(1:GB28181，2:ONVIF)",
              prop: "protocolType",
              hide: true,
              rules: [{
                required: true,
                message: "请输入协议类型(1:GB28181，2:ONVIF)",
                trigger: "blur"
              }]
            },
            {
              label: "是否在线",
              prop: "isOnline",
              dataType: "number",
              type: "select",
              search: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=online_or_offline",
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              rules: [{
                required: true,
                message: "请输入是否在线(0:不在线，1:在线)",
                trigger: "blur"
              }]
            },
            {
              label: "是否支持云存储(0:不支持，1:支持)",
              prop: "isSupportCloudStorage",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否支持云存储(0:不支持，1:支持)",
                trigger: "blur"
              }]
            },
            {
              label: "是否支持本地存储(0:不支持，1:支持)",
              prop: "isSupportLocalStorage",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否支持本地存储(0:不支持，1:支持)",
                trigger: "blur"
              }]
            },
            {
              label: "是否支持云台",
              prop: "isSupportPtz",
              dataType: "number",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=is_support",
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              rules: [{
                required: true,
                message: "请输入是否支持云台(0:不支持，1:支持)",
                trigger: "blur"
              }]
            },
            {
              label: "是否支持移动侦测(0:不支持，1:支持)",
              prop: "isSupportMotionDetection",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否支持移动侦测(0:不支持，1:支持)",
                trigger: "blur"
              }]
            },
            {
              label: "是否支持手动报警(0:不支持，1:支持)",
              prop: "isSupportManualAlarm",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否支持手动报警(0:不支持，1:支持)",
                trigger: "blur"
              }]
            },
            {
              label: "是否支持外部开关报警(0:不支持，1:支持)",
              prop: "isSupportExternalAlarmSwitch",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否支持外部开关报警(0:不支持，1:支持)",
                trigger: "blur"
              }]
            },
            {
              label: "设备是否有反转倒置能力(0:否，1:是)",
              prop: "isHaveInversion",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备是否有反转倒置能力(0:否，1:是)",
                trigger: "blur"
              }]
            },
            {
              label: "云存储类别",
              prop: "cloudStorageType",
              type: "select",
              dataType: "number",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=cloud_storage_type",
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              rules: [{
                required: true,
                message: "请输入云存储类别(0:无云存储，1:电信分布式存储，2:21cn存储)",
                trigger: "blur"
              }]
            },
            {
              label: "云存储配置id",
              prop: "cloudStorageConfId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入云存储配置id",
                trigger: "blur"
              }]
            },
            {
              label: "视频流获取方式(0:主动推流，1:主动拉取流)",
              prop: "streamType",
              hide: true,
              rules: [{
                required: true,
                message: "请输入视频流获取方式(0:主动推流，1:主动拉取流)",
                trigger: "blur"
              }]
            },
            {
              label: "设备地址",
              prop: "address",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备地址",
                trigger: "blur"
              }]
            },
            {
              label: "是否拥有者(0:否，1:是)",
              prop: "isOwner",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否拥有者(0:否，1:是)",
                trigger: "blur"
              }]
            },
            {
              label: "所属NVR的编号",
              prop: "parentNvrNum",
              hide: true,
              rules: [{
                required: true,
                message: "请输入所属NVR的编号",
                trigger: "blur"
              }]
            },
            {
              label: "告警状态（0：正常；1：告警）",
              prop: "alarmStatus",
              hide: true,
              rules: [{
                required: true,
                message: "请输入告警状态（0：正常；1：告警）",
                trigger: "blur"
              }]
            },
            {
              label: "经度",
              prop: "longitude",
              rules: [{
                required: true,
                message: "请输入经度",
                trigger: "blur"
              }]
            },
            {
              label: "纬度",
              prop: "latitude",
              rules: [{
                required: true,
                message: "请输入纬度",
                trigger: "blur"
              }]
            },
            {
              label: "维护人员信息",
              prop: "maintainer",
              hide: true,
              rules: [{
                required: true,
                message: "请输入维护人员信息",
                trigger: "blur"
              }]
            },
            {
              label: "是否封存，true封存",
              prop: "block",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否封存，true封存",
                trigger: "blur"
              }]
            },
            {
              label: "封存时间",
              prop: "blockTime",
              hide: true,
              rules: [{
                required: true,
                message: "请输入封存时间",
                trigger: "blur"
              }]
            },
            {
              label: "路由器id",
              prop: "routeId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入路由器id",
                trigger: "blur"
              }]
            },
            {
              label: "摄像头类型 G：枪机 D：球机",
              prop: "equipType",
              hide: true,
              rules: [{
                required: true,
                message: "请输入摄像头类型 G：枪机 D：球机",
                trigger: "blur"
              }]
            },
            {
              label: "设备最后状态更新时间",
              prop: "updateStateTimeString",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备最后状态更新时间",
                trigger: "blur"
              }]
            },
            {
              label: "设备监控位id",
              prop: "districtId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备监控位id",
                trigger: "blur"
              }]
            },
            {
              label: "存储位置（通过该字段快速查询，所有的子节点）机构树位置",
              prop: "orgPosition",
              hide: true,
              rules: [{
                required: true,
                message: "请输入存储位置（通过该字段快速查询，所有的子节点）机构树位置",
                trigger: "blur"
              }]
            },
            {
              label: "rtsp视频播放地址",
              prop: "rtspUrl",
              hide: true,
              rules: [{
                required: true,
                message: "请输入rtsp视频播放地址",
                trigger: "blur"
              }]
            },
            {
              label: "http-flv视频播放地址",
              prop: "httpFlvUrl",
              hide: true,
              rules: [{
                required: true,
                message: "请输入http-flv视频播放地址",
                trigger: "blur"
              }]
            },
            {
              label: "所属账户",
              prop: "accountName",
              hide: true,
              rules: [{
                required: true,
                message: "请输入所属账户",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.videoclouddevice_add, false),
          viewBtn: this.vaildData(this.permission.videoclouddevice_view, false),
          delBtn: this.vaildData(this.permission.videoclouddevice_delete, false),
          editBtn: this.vaildData(this.permission.videoclouddevice_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.buildingId = this.buildingId;
        pageByBuilding(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
