<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="标签名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入标签名称"
          clearable
          @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"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="listTable"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="标签名称" :show-overflow-tooltip="true" width="260"></el-table-column>
      <el-table-column prop="sort" label="排序" width="260"></el-table-column>
      <el-table-column prop="type" label="属性" :show-overflow-tooltip="true"></el-table-column>
      >
      <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)"
          >修改
          </el-button>
          <el-button
            v-show="scope.row.level != 3"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-button
            v-show="scope.row.level == 3"
            size="mini"
            type="text"
            icon="el-icon-video-camera"
            @click="labellayer(scope.row)"
          >配置摄像头
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="580px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="上级标签">
              <treeselect
                :disabled="disabled"
                v-model="form.parentId"
                :options="menuOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级标签"
              />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="标签名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入菜单名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="标签属性" prop="type">
              <el-input v-model="form.type" placeholder="请输入标签属性"/>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="可用系统" prop="labelTypeList">
              <el-select v-model="form.labelTypeList" multiple placeholder="请选择用途" style="width: 100%">
                <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-col>
          <el-col :span="18">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入该标签序号" @input="validateNote"/>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="图片" prop="imgUrl">
              <image-upload :limit="1" v-model="form.imgUrl"/>
            </el-form-item>
          </el-col>
        </el-row>
      </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>
    <el-dialog :title="title" v-if="showImages" :visible.sync="showImages" :id="id" width="850px" @close="closeDialogs">
      <labelList ref="camera" :labelId="id" style="width: 100%;height: 100%;"></labelList>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTable,
  getlabel,
  deleteLabel,
  addLabel,
  Searchtags,
  updateLabel
} from '@/api/centerMonitor/LabelManagement/setLabel'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import IconSelect from '@/components/IconSelect'
import labelList from './configurationTab'

export default {
  name: 'Menu',
  dicts: ['sys_show_hide', 'sys_normal_disable', 'front_type', 'camera_system'],
  components: { Treeselect, IconSelect, labelList },
  data() {
    return {
      id: '',
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      listTable: [],
      imageList: [],
      // 菜单树选项
      menuOptions: [],
      showImages: false,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: undefined,
        type: undefined,
        sort: undefined,
        id: undefined,
        parentId: undefined,
        sbNotes: undefined
      },
      // 表单参数
      form: {
        imgUrl: []
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '标签名称不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '标签顺序不能为空', trigger: 'blur' }
        ],
        labelTypeList: [
          { required: true, message: '可用系统不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '标签属性不能为空', trigger: 'blur' }
        ]
      },
      disabled: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      listTable(this.queryParams).then(response => {
        this.listTable = this.handleTree(response.data, 'id')
        this.loading = false
      })
    },
    //搜索按钮
    SetSearchtags(row) {
      this.reset()
      if (this.queryParams.name) {
        Searchtags(this.queryParams.name).then(response => {
          this.listTable = response.rows
        })
      }
    },
    //重置
    closeDialogs() {
      this.getList()
      this.showImages = false
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },

    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listTable().then(response => {
        this.menuOptions = []
        const menu = { id: 0, name: '主标签', children: [] }
        menu.children = this.handleTree(response.rows, 'id')
        this.menuOptions.push(menu)
      })
    },
    //排序方法
    validateNote() {
      const inputValue = this.form.sort
      if (!/^\d{1,3}$/.test(inputValue)) {
        this.form.sort = ''
        return
      }
      this.form.sort = parseInt(inputValue, 10)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: 0,
        name: undefined,
        icon: undefined,
        orderNum: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.SetSearchtags()
    },
    //重置按钮
    handleQuerys() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuerys()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.id) {
        this.form.parentId = row.id
      } else {
        this.form.parentId = 0
      }
      this.open = true
      this.title = '添加菜单'
    },
    //设置标签
    labellayer(row) {
      const id = row.id
      this.id = id
      this.title = '配置摄像头标签列表'
      this.showImages = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disabled = true
      this.reset()
      this.getTreeselect()
      getlabel(row.id).then(response => {
        this.form = response.data
        this.form.labelTypeList = this.form.labelTypeList.map(item => item.type)
        this.open = true
        this.title = '修改菜单'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let dict = this.dict.type.camera_system
          let formList = this.form.labelTypeList
          let list = []
          for (let i = 0; i < formList.length; i++) {
            let typeName = ''
            for (let j = 0; j < dict.length; j++) {
              if (dict[j].value == formList[i]) {
                typeName = dict[j].label
                break
              }
            }
            list.push({
              type: formList[i],
              typeName: typeName
            })
          }
          this.form.labelTypeList = list
          if (this.form.id != undefined) {
            updateLabel(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.getList()
            })
            this.open = false
          } else {
            addLabel(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.getList()
            })
            this.open = false
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('删除此标签，所有下级标签及配置都将被清除，是否继续？').then(function() {
        return deleteLabel(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
