<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select
          v-model="query.connId"
          clearable
          size="small"
          placeholder="转发连接"
          class="filter-item"
          style="width: 170px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in forwardingConnOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.type"
          clearable
          size="small"
          placeholder="转发类型"
          class="filter-item"
          style="width: 170px"
          @change="handleForwardingTypeChange"
        >
          <el-option
            v-for="item in optionsObj.ForwardingType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-input
          v-model="query.tag"
          clearable
          size="small"
          placeholder="请输入标签名"
          style="width: 170px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.tagDescription"
          clearable
          size="small"
          placeholder="请输入标签描述"
          style="width: 170px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.tagConnId"
          clearable
          size="small"
          placeholder="标签所属连接"
          class="filter-item"
          style="width: 170px"
          @change="handleTagConnChange"
        >
          <el-option
            v-for="item in optionsObj.TagConn"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.tagDeviceId"
          clearable
          size="small"
          placeholder="标签所属设备"
          class="filter-item"
          style="width: 170px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in tagDeviceOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.tagDataType"
          clearable
          size="small"
          placeholder="标签数据类型"
          class="filter-item"
          style="width: 170px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.TagDataType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          v-permission="permission.del"
          class="filter-item"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :loading="crud.delAllLoading"
          @click="toDeleteFromParam"
        >
          删除所有查出条目
        </el-button>
      </crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="800px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="转发连接" prop="id">
          <el-select v-model="form.id" filterable placeholder="请选择" style="width: 190px">
            <el-option
              v-for="item in optionsObj.ForwardingConn"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <el-row style="margin-top: 20px">
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          style="margin-left: 20px"
          :disabled="tagSelection.length <= 0"
          @click="handleAddSelection"
          >新增本页所选标签
        </el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          style="margin-left: 20px"
          @click="handleAddQuery"
          >新增所有查出标签
        </el-button>
      </el-row>
      <el-divider></el-divider>
      <el-row style="margin-top: 20px">
        <tag-select :crudcu="crudCU" :query.sync="tagQuery" :selection.sync="tagSelection" />
      </el-row>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in forwardingTagTableOptions"
        :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',
            'datacommunicationmanage:forwardingtag:edit',
            'datacommunicationmanage:forwardingtag: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 crudTag from '@/api/dataComm/forwarding/tag'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { getOptions } from '@/api/dataComm/forwarding/options'
import TagSelect from './tagSelect'

// 表头配置
import { forwardingTagTableOptions } from '../../tableConfig'

const defaultForm = {
  id: '',
  tagIds: null,
  tagParam: null,
}

export default {
  name: 'ForwardingConn',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    TagSelect,
  },
  cruds() {
    return CRUD({
      title: '数据转发标签',
      url: 'dataCommunicationManage/forwarding/tag/getList',
      listOrder: [],
      crudMethod: { ...crudTag },
      optShow: {
        add: true,
        edit: false,
        del: true,
        reset: false,
        import: false,
        export: false,
      },
    })
  },
  data() {
    return {
      forwardingTagTableOptions,
      rules: {
        id: [{ required: true, message: '请选择转发连接', trigger: 'blur' }],
      },
      permission: {
        add: ['admin', 'datacommunicationmanage:forwardingtag:add'],
        edit: ['admin', 'datacommunicationmanage:forwardingtag:edit'],
        del: ['admin', 'datacommunicationmanage:forwardingtag:del'],
      },
      statusOpt: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
      optionsObj: {
        ForwardingConn: [],
        ForwardingType: [],
        TagConn: [],
        TagDataType: [],
      },
      tagDeviceOptions: [],
      forwardingConnOptions: [],
      tagQuery: {},
      tagSelection: [],
    }
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {
    this.getOptions()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.tagSelection = []
      this.tagQuery = {}
    },
    handleAddSelection() {
      this.form.tagParam = null
      this.form.tagIds = this.tagSelection
      this.crud.submitCU()
    },
    handleAddQuery() {
      this.form.tagIds = null
      this.form.tagParam = this.tagQuery
      this.crud.submitCU()
    },
    handleForwardingTypeChange(value) {
      getOptions('ForwardingConn', { value }).then((res) => {
        if (res.code === 20000) {
          this.query.connId = null
          this.forwardingConnOptions = res.data.content
          this.crud.toQuery()
        }
      })
    },
    handleTagConnChange(value) {
      getOptions('TagDevice', { id: value }).then((res) => {
        if (res.code === 20000) {
          this.query.tagDeviceId = null
          this.tagDeviceOptions = res.data.content
          this.crud.toQuery()
        }
      })
    },
    toDeleteFromParam() {
      this.$confirm(`确认删除${this.crud.page.total}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.crud.delAllLoading = true
          crudTag.delFromParam(this.query).then(res =>{
            this.crud.delAllLoading = false
            this.crud.page.page = 1
            this.crud.delSuccessNotify()
            this.crud.refresh()
          })
        })
        .catch(() => {})
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key, {})
          if (res.code === 20000) this.optionsObj[key] = res.data.content
          if (key == 'ForwardingConn') this.forwardingConnOptions = this.optionsObj[key]
        } catch (error) {
          console.log(error)
        }
      })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
::v-deep .el-input-group__append,
.el-input-group__prepend {
  padding: 0 8px;
}
</style>
