<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入控制点类型名称"
          style="width: 160px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission"></crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="110px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" style="width: 340px;" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-select
            v-model="form.icon"
            clearable
            filterable
            size="small"
            placeholder="请选择图标"
            class="filter-item"
            style="width: 340px"
            @change="handleIconChange"
          >
            <el-option
              v-for="item in iconOptions"
              :key="item.id"
              :label="item.typeStr + ' / ' + item.name"
              :value="item.id"
            >
              <span style="float: left">{{ item.typeStr + ' / ' + item.name }}</span>
              <img
                style="float:right;width: 34px;height:34px;"
                :src="VUE_APP_BASE_API + item.path"
              />
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="">
          <img
            style="width: 60px;height:60px;"
            v-if="selectedIcon"
            :src="VUE_APP_BASE_API + selectedIcon"
            alt=""
          />
        </el-form-item>
      </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 controlpointTypeTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
        <template slot-scope="scope">
          <img
            style="width: 32px;height:32px;"
            v-if="item.prop === 'iconPath'"
            v-show="scope.row.iconPath"
            :src="VUE_APP_BASE_API + scope.row.iconPath"
            alt=""
          />
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'pipe:controlpointtype:edit', 'pipe:controlpointtype: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 crudControlpointType from '@/api/pipe/controlpointType'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { controlpointTypeTableOptions } from '../tableConfig'
import { initData } from '@/api/data'
const defaultForm = {
  id: '',
  icon: undefined,
  name: ''
}

export default {
  name: 'PipeControlpointType',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: '控制点类型',
      url: 'pipe/controlpointType/getList',
      listOrder: [],
      crudMethod: { ...crudControlpointType }
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    selectedIcon() {
      let iconObj = this.iconOptions.find(item => item.id == this.form.icon)
      return iconObj?.path
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    }
  },
  created() {
    this.getIcons()
  },
  data() {
    return {
      controlpointTypeTableOptions,
      permission: {
        add: ['admin', 'pipe:controlpointtype:add'],
        edit: ['admin', 'pipe:controlpointtype:edit'],
        del: ['admin', 'pipe:controlpointtype:del']
      },
      rules: {
        name: [{ required: true, message: '请输入控制点类型名称', trigger: 'blur' }],
        //icon: [{ required: true, message: '请选择图标', trigger: 'blur' }]
      },
      iconOptions: []
    }
  },
  methods: {
    getIcons() {
      initData('system/icon/getListJson', {}).then(res => {
        this.iconOptions = res.data.content
      })
    },
    handleIconChange(currentValue, oldValue){
      if (currentValue == 0) this.form.icon = undefined
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
