<template>
  <com-head :options="headList" showCtrl>
    <el-form label-width="90px" inline="true">
      <el-form-item label="S/N：">
        <el-input v-model="search.state.snCode" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="所属公司：">
        <com-company v-model="search.state.departmentId" />
        <!-- <el-input v-model="search.state.searchKey" placeholder="请输入"></el-input> -->
      </el-form-item>
      <el-form-item label="激活时间：">
        <el-date-picker
          v-model="time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="searh-btns">
        <el-button @click="search.reset">重置</el-button>
        <el-button type="primary" @click="search.submit">查询</el-button>
      </el-form-item>
    </el-form>
  </com-head>

  <div class="body-layer">
    <div class="body-head">
      <h3>相机列表</h3>
      <div class="table-ctrl-right">
        <!-- <el-input placeholder="关键词" suffix-icon="el-icon-search" class="search-scene" v-model="search.state.searchKey" style="width: auto" /> -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="oper.readyInsert"
          v-if="auth.add"
          >添加相机</el-button
        >
      </div>
    </div>
    <el-table
      ref="multipleTable"
      :data="dataList.state"
      tooltip-effect="dark"
      style="width: 100%"
      @row-click="dataList.selectRow"
    >
      <el-table-column label="S/N" prop="snCode"></el-table-column>
      <el-table-column label="Wi-Fi名称" prop="wifiName"></el-table-column>
      <el-table-column label="激活时间" prop="activatedTime"></el-table-column>
      <el-table-column label="所属架构" prop="departmentName"></el-table-column>
      <el-table-column label="拍摄VR数量" prop="sceneCount"></el-table-column>
      <el-table-column label="最后拍摄时间" prop="lastTime"></el-table-column>
      <el-table-column label="操作" v-slot:default="{ row }" v-if="auth.unbind || auth.update">
        <span class="oper-span" @click="oper.readyUpdate(row)" v-if="auth.update">编辑</span>
        <span class="oper-span" 
          @click="unbindCamrea(row)" 
          v-if="auth.unbind && row.departmentId" 
          style="color: var(--primaryColor)">
         解绑
        </span>
      </el-table-column>
    </el-table>

    <com-pagination
      @size-change="pag.sizeChange"
      @current-change="pag.currentChange"
      :current-page="pag.state.currPage"
      :page-size="pag.state.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pag.state.total"/>
  </div>
  
  <com-dialog
    :title="(oper.state.id ? '修改' : '添加') + '相机'"
    v-model:show="oper.state.show"
    @submit="submit"
  >
    <el-form ref="form" :model="form" label-width="100px" class="camera-from">
      <!-- <el-form-item label="相机类型：" class="mandatory">
        <el-select v-model="oper.state.region" placeholder="相机类型：">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="Wi-Fi名称：" class="mandatory">
        <el-input :modelValue="oper.state.wifiName" @update:modelValue="val => oper.state.wifiName = val.trim()"></el-input>
      </el-form-item>
      <el-form-item label="物理地址：" class="mandatory">
        <el-input :modelValue="oper.state.childName" @update:modelValue="val => oper.state.childName = val.trim()"></el-input>
      </el-form-item>
      <el-form-item label="S/N：" class="mandatory">
        <el-input :modelValue="oper.state.snCode" @update:modelValue="val => oper.state.snCode = val.trim()"></el-input>
      </el-form-item>
      <el-form-item label="所属架构：" class="mandatory">
        <com-company :modelValue="oper.state.departmentId" @update:modelValue="val => oper.state.departmentId = val.trim()" hideAll />
      </el-form-item>
    </el-form>
  </com-dialog>

</template>

<script>
import { ref, watch } from "vue";
import getTableState from "@/state/tableRef";
import auth from "@/state/viewAuth";
import comDialog from "@/components/dialog";
import comCompany from "@/components/company-select";
import comPagination from "@/components/pagination";
import comHead from "@/components/head";
import { dateFormat } from '@/util'
import {
  getCameraList,
  insertCamera,
  updateCamera,
  deleteCamera,
  unbindCamera
} from '@/request/config'
import axios from 'axios';

export default {
  name: 'camera',
  setup() {
    const state = getTableState({
      updateUrl: updateCamera,
      insertUrl: insertCamera,
      getUrl: getCameraList,
      delUrl: deleteCamera,
      searchAttr: { startTime: "", endTime: '', departmentId: '', snCode: '' },
      operAttr: {
        wifiName: '',
        snCode: '',
        childName: '',
        departmentId: ''
      },
      pagAttr: {totalNum: 'total'}
    });
    const headList = ref([{ name: "我的相机", value: 2 }]);
    const time = ref(null)

    watch(time, () => {
      if (time.value) {
        state.search.value.state.startTime = dateFormat(new Date(time.value[0]), 'yyyy-MM-dd hh:mm:ss')
        state.search.value.state.endTime = dateFormat(new Date(time.value[1]), 'yyyy-MM-dd 23:59:59')
      } else {
        state.search.value.state.startTime = state.search.value.state.endTime = ''
      }
    })
    watch(
      [() => state.search.value.state.startTime, () => state.search.value.state.endTime],
      () => {
        if (!state.search.value.state.startTime || !state.search.value.state.endTime) {
          time.value = null
        }
      }
    )
    
    watch(
      () => state.pag.value.state.total,
      () => headList.value[0].name = '我的相机（' + state.pag.value.state.total +'）'
    )


    return { ...state, headList, time, auth };
  },
  methods: {
    async unbindCamrea(data) {
      if (data.departmentId && (await this.$confirm('确定要解绑此相机吗？', '提示'))) {
        await axios.post(unbindCamera, data)
        data.departmentName = data.departmentId = null
      }

      this.dataList.refer()
    },
    submit() {
      if (!this.oper.state.wifiName.trim()) {
        return this.$alert('Wi-Fi名称不能为空！', '提示')
      } else if (!this.oper.state.snCode.trim()) {
        return this.$alert('S/N不能为空！', '提示')
      } else if (!this.oper.state.childName.trim()) {
        return this.$alert('物理地址不能为空！', '提示')
      } else if (!this.oper.state.departmentId.trim()) {
        return this.$alert('所属架构不能为空！', '提示')
      }

      this.oper.state.id ? this.oper.update() : this.oper.insert()
    }
  },
  components: {
    "com-dialog": comDialog,
    "com-company": comCompany,
    "com-head": comHead,
    "com-pagination": comPagination
  },
};
</script>

<style lang="scss" scoped>
.table-ctrl-right {
  .search-scene {
    margin: 0 20px 0 26px;
  }
  i {
    margin-left: 20px;
    font-size: 1.7rem;
    vertical-align: middle;
    cursor: pointer;

    &.active {
      color: var(--primaryColor);
    }
  }
}

.camera-from {
  width: 320px;
  margin: 0 auto;
}
</style>
