<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.code"
          clearable
          size="small"
          placeholder="输入AI设备编号"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入AI设备名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.type"
          filterable
          clearable
          size="small"
          placeholder="请选设备类型"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.AIType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.enable"
          clearable
          size="small"
          placeholder="请选择是否启用"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in 2"
            :key="item"
            :label="item == '1' ? '启用' : '禁用'"
            :value="item == '1' ? true : false"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!-- 表单组件 -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" size="small" class="filter-item" style="width: 280px" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" size="small" class="filter-item" style="width: 280px" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            type="textarea"
            :rows="2"
            v-model="form.description"
            size="small"
            class="filter-item"
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select
            v-model="form.type"
            filterable
            size="small"
            class="filter-item"
            style="width: 280px"
          >
            <el-option
              v-for="item in optionsObj.AIType"
              :key="item.value"
              :label="item.label"
              :value="+item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="序列号" prop="serial">
          <el-input v-model="form.serial" size="small" class="filter-item" style="width: 280px" />
        </el-form-item>
        <el-form-item label="IP地址" prop="ip">
          <el-input v-model="form.ip" size="small" class="filter-item" style="width: 280px" />
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port" size="small" class="filter-item" style="width: 280px" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" size="small" class="filter-item" style="width: 280px" />
        </el-form-item>
        <el-form-item label="启用" prop="enable">
          <el-switch
            v-model="form.enable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
          >
          </el-switch>
        </el-form-item>
        <div v-for="(item, index) in form.cameraOfChannels" :key="index" style="margin-top: 40px">
          <el-form-item
            :label="'通道ID' + (index + 1)"
            :prop="'cameraOfChannels[' + index + '].channelId'"
            :rules="{ required: true, message: '请输入通道ID', trigger: 'blur' }"
          >
            <el-input
              v-model="item.channelId"
              size="small"
              class="filter-item"
              style="width: 280px"
            />
          </el-form-item>
          <el-form-item
            label="绑定摄像头"
            :prop="'cameraOfChannels[' + index + '].cameraId'"
            :rules="{ required: true, message: '请选择摄像头', trigger: 'blur' }"
          >
            <el-select
              v-model="item.cameraId"
              filterable
              size="small"
              class="filter-item"
              style="width: 280px"
            >
              <el-option
                v-for="item in optionsObj.Camera"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <div style="width: 360px; text-align: right; margin-top: 20px">
            <el-button
              type="danger"
              size="mini"
              @click="deleteChannel(index)"
              v-if="form.cameraOfChannels"
            >
              删除通道
            </el-button>
            <el-button
              v-if="index == form.cameraOfChannels.length - 1"
              type="primary"
              size="mini"
              @click="addChannel"
            >
              增加通道
            </el-button>
          </div>
        </div>
        <div
          style="width: 360px; text-align: right; margin-top: 20px"
          v-if="!form.cameraOfChannels || form.cameraOfChannels.length == 0"
        >
          <el-button type="primary" size="mini" @click="addChannel"> 增加通道 </el-button>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU"> 取消 </el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">
          确认
        </el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        v-for="item in aiConfigTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      ></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'ai:config:edit', 'ai:config:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudAI from '@/api/ai/config'
import { getOptions } from '@/api/ai/public'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import DateRangePicker from '@/components/DateRangePicker'
import { aiConfigTableOptions } from '../tableConfig'
import { initData } from '@/api/data'

const defaultForm = {
  id: '0',
  code: '', //编号
  name: '', //名称
  description: '', //描述
  serial: '', //序列号
  type: '', //设备类型
  ip: '', //ip地址
  port: '', //端口号
  cameraOfChannels: [{}], //AI通道的对应摄像头
  remark: '', //备注
  enable: true, //启用
}

export default {
  name: 'AIConfig',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: 'AI设备',
      url: 'ai/config/getPageListJson',
      listOrder: [],
      crudMethod: { ...crudAI },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
  },
  data() {
    return {
      aiConfigTableOptions,
      permission: {
        add: ['admin', 'ai:config:add'],
        edit: ['admin', 'ai:config:edit'],
        del: ['admin', 'ai:config:del'],
      },
      rules: {
        code: [{ required: true, message: '请选输入编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        serial: [{ required: true, message: '请输入序列号', trigger: 'blur' }],
        ip: [{ required: true, message: '请输入IP地址', trigger: 'blur' }],
      },
      optionsObj: {
        Camera: [],
        AIType: [],
      },
    }
  },
  created() {
    this.getOptions()
  },
  methods: {
    addChannel() {
      if (!this.form.cameraOfChannels) this.form.cameraOfChannels = []
      this.form.cameraOfChannels.push({})
    },
    deleteChannel(index) {
      this.form.cameraOfChannels.splice(index, 1)
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
  },
}
</script>

<style></style>
