<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
          <!--<el-form-item label="所属部门" prop="deptName">-->
              <!--<el-input-->
                      <!--v-model="queryParams.deptName"-->
                      <!--placeholder="请输入所属部门"-->
                      <!--clearable-->
                      <!--size="small"-->
                      <!--@keyup.enter.native="handleQuery"-->
              <!--/>-->
          <!--</el-form-item>-->
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['broadcast:broadcast:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['broadcast:broadcast:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['broadcast:broadcast:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['broadcast:broadcast:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          size="mini"
          @click="jump"
        >云广播</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="broadcastList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="所属区域" align="center" prop="position" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['broadcast:broadcast:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['broadcast:broadcast:remove']"
          >删除</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="jump"
            v-hasPermi="['broadcast:broadcast:control']"
          >操控</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-management"
            @click="handleCameraDept(scope.row)"
            v-hasPermi="['aidevicedept:aidevicedept:list']"
          >部门管理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改云广播设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name" label-width="120px">
          <el-input v-model="form.name" maxlength="40" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="型号" prop="model" label-width="120px">
          <el-input v-model="form.model" maxlength="60" placeholder="请输入型号" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude" label-width="120px">
              <el-input v-model="form.longitude" placeholder="双击此处选择经纬度" @dblclick.native="showMap"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude" label-width="120px">
              <el-input v-model="form.latitude" placeholder="双击此处选择经纬度" @dblclick.native="showMap"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="所属区域" prop="position" label-width="120px">
          <el-input v-model="form.position" maxlength="50" placeholder="请输入所属区域" />
        </el-form-item>
        <el-form-item label="可用系统" prop="broadcastSystem"label-width="120px">
          <el-select v-model="form.broadcastSystem" multiple placeholder="请选择可用系统">
            <el-option
              v-for="dict in dict.type.camera_system"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="云广播用途" prop="broadcastUse" label-width="120px">
          <el-select v-model="form.broadcastUse"  placeholder="请选择云广播用途">
            <el-option
              v-for="dict in dict.type.broadcast_use"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="120px">
          <el-input v-model="form.remark" maxlength="80" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="图片"  prop="broadcastImagesList" label-width="120px">
          <image-upload v-model="form.broadcastImagesList"  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 超图弹框 -->
    <ISuperMap ref="ISuperMap" v-if="ISuperMapvisible" @send="send"/>
    <!--<el-dialog :title="titleLongitude" :visible.sync="showLongitude">-->
      <!--<el-input v-show="false" v-model="form.longitude"></el-input>-->
      <!--<el-input v-show="false" v-model="form.latitude"></el-input>-->
      <!--<supermap style="width: 100%;height: 500px;" :mapDiv="'cameraMap'" :mapSite="{doubleClickZoom:false}"-->
                <!--:codes="['9fa5']" :isSideBySide="false" :showLatLng="showLatLng"></supermap>-->
      <!--<el-button type="primary" @click="showLongitude=false">确 定</el-button>-->
    <!--</el-dialog>-->

    <!-- 部门管理弹框 -->
    <el-dialog :title="titleBroadcastDept" :visible.sync="showBroadcastDept" @close="closeDialog">
      <broadcastdevicedept v-if="showBroadcastDept" ref="broadcastDept"   :broId="broIdValue" :broName="deviceNameValue"
                         style="width: 100%;height: 100%;"></broadcastdevicedept>
    </el-dialog>
  </div>
</template>

<script>
  import { listBroadcast,listBroadcastAll, getBroadcast, delBroadcast, addBroadcast, updateBroadcast } from "@/api/broadcast/broadcast";
  import supermap from '@/views/components/supermap'
  import ISuperMap from '@/views/components/ISuperMap'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import { treeselect } from '@/api/system/dept'
  import Treeselect from "@riophae/vue-treeselect";
  import broadcastdevicedept from '@/views/centerMonitor/aidevicedept/broadcastdevicedept'

  export default {
    name: "Broadcast",
    components: { Treeselect ,supermap,broadcastdevicedept,ISuperMap },
    dicts: ['camera_system','broadcast_use'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        deviceCode: null,
        deviceName: null,
        // 云广播设备表格数据
        broadcastList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 超图
        sign: 1,
        ISuperMapvisible: false,
        showLongitude: false,
        titleLongitude: '',
        // 部门
        showBroadcastDept: false,
        titleBroadcastDept:'',
        //传递到子页面摄像头ID
        broIdValue: null,
        deviceNameValue: null,
        // 部门树选项
        deptOptions: [],
        titleCameraDept: '',
        // 事件
        showCameraEvent: false,
        titleCameraEvent: '',
        //可用系统
        broadcastSystemList:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          deptName: null,
          model: null,
          longitude: null,
          latitude: null,
          position: null,
          broadcastUse:null,
        },
        // 表单参数
        form: {
          broadcastImages:[],
          broadcastImagesList:[],
        },
        // 表单校验
        rules: {
          longitude: [
            {required: true, message: "经度不能为空", trigger: "blur"},
          ],
          latitude: [
            {required: true, message: "纬度不能为空", trigger: "blur"},
          ],
          name: [
            {required: true, message: "名称不能为空", trigger: "blur"},
          ],
          broadcastSystem: [
            {required: true, message: "可用系统不能为空", trigger: "blur"},
          ],
          broadcastUse: [
            {required: true, message: "可用系统不能为空", trigger: "blur"},
          ],
        }
      };
    },
    mounted(){
      this.broadcastSystemList = this.dict.type.camera_system;
    },
    created() {
      this.getList();
      this.getTreeselect();
    },
    methods: {
      /** 查询云广播设备列表 */
      getList() {
        this.loading = true;
        listBroadcastAll(this.queryParams).then(response => {
          this.broadcastList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // showMap() {
      //   this.showLongitude = true
      // },
      showMap() {
        this.ISuperMapvisible = true;
        this.$nextTick(() => {
          this.$refs.ISuperMap.init(this.sign, {
            id: this.form.id,
            longitude: this.form.longitude,
            latitude: this.form.latitude,
            xiantude: this.form.longitude//this.form.longitude换成每个页面对应线或者面对应的字段
          })
        })
      },
      send(val) {
        if (val===true){
          this.ISuperMapvisible = false;
          return;
        }
        if (this.sign === 1) {
          this.form.longitude = val.longitude;
          this.form.latitude = val.latitude;
        }
        if (this.sign === 2 || this.sign === 3) this.form.longitude = val.xiantude;//this.form.longitude 换成对应的线或区域的字段
        this.ISuperMapvisible = false
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 转换部门数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        }
      },
      showLatLng: function (lat, lng) {
        this.form.latitude = lat;
        this.form.longitude = lng
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      //部门ID、部门名称赋值
      hx(node) {
        this.form.deptId = node.id;
        this.form.deptName = node.label
      },
      /** 部门管理列表 */
      handleCameraDept(row) {
        const id = row.id;
        this.broIdValue = id;
        this.deviceCode = row.deviceCode;
        this.deviceNameValue = row.name;
        this.showBroadcastDept = true;
        this.titleBroadcastDept = "部门管理列表";
      },
      closeDialog() {
        this.showBroadcastDept = false;
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          deptName: null,
          model: null,
          longitude: null,
          latitude: null,
          position: null,
          remark: null,
          broadcastSystem: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加云广播设备";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        let that = this
        this.reset();
        const id = row.id || this.ids
        getBroadcast(id).then(response => {
          this.form = response.data
          const updateList = [];
          for (let index in this.form.broadcastSystem) {
            updateList.push(this.form.broadcastSystem[index].type);
          }
          this.form.broadcastSystem = updateList;
          that.open = true;
          that.title = "修改云广播设备";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            const broadcastSystem = [];
            for (let item in this.form.broadcastSystem) {
              for (let items in this.broadcastSystemList) {
                console.log('====='+this.form.broadcastSystem)
                if (this.broadcastSystemList[items].value == this.form.broadcastSystem[item]) {
                  const broadcastType = {};
                  broadcastType.type = this.broadcastSystemList[items].value;
                  broadcastType.typeName = this.broadcastSystemList[items].label;
                  console.log("broadcastType.type=" + broadcastType.type + " broadcastType.typeName=" + broadcastType.typeName)
                  broadcastSystem.push(broadcastType);
                }
              }
            }
            console.log("broadcastSystem=", broadcastSystem)
            this.form.broadcastSystem = broadcastSystem;
            let that = this
            if (this.form.id != null) {
              that.form.broadcastImages = this.form.broadcastImagesList ?  typeof this.form.broadcastImagesList == 'string' ? this.form.broadcastImagesList.split(",") : this.form.broadcastImagesList : []
              that.form.broadcastImagesList = []
              updateBroadcast(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                // this.getList()
              })
            } else {
              that.form.broadcastImages = this.form.broadcastImagesList ?  typeof this.form.broadcastImagesList == 'string' ? this.form.broadcastImagesList.split(",") : this.form.broadcastImagesList : []
              that.form.broadcastImagesList = []
              addBroadcast(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delBroadcast(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('/center-monitor/broadcast/export', {
          ...this.queryParams
        }, `broadcast_${new Date().getTime()}.xlsx`)
      },
      /** 跳转云广播 */
      jump(){
        // window.location.href="https://sts.sty1818.com/"; //本页面跳转
        window.open('https://sts.sty1818.com/');   //新增页跳转
      }
    }
  };
</script>
