<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="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-color-picker v-model="form.color" show-alpha></el-color-picker>
        </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 lineTypeTableOptions"
        :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">
          <div
            style="width:20px;height:20px"
            v-if="item.prop === 'color'"
            :style="{'background': scope.row.color, 'border': '1px solid black', 'display': 'inline-block'}"
          />
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'pipe:linetype:edit', 'pipe:linetype: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 crudLineType from '@/api/pipe/lineType'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import { getOptions } from '@/api/pipe/public'

import {
  isPosNumber,
  isNegNumber,
  isNumber,
  isPosInteger,
  isNegInteger,
  isInteger,
} from '@/utils/validate'

import crudComps from '@crud'
import { lineTypeTableOptions } from '../tableConfig'

const defaultForm = {
  id: '',
  name: '',
  color: '',
  width: 1
}

export default {
  name: 'PipeLineType',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: '管道类型',
      url: 'pipe/lineType/getList',
      listOrder: [],
      crudMethod: { ...crudLineType }
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    }
  },
  data() {
    var chackWidth = (rule, value, callback) => {
      if (!value) return callback(new Error('请输入管道显示宽度'))
      if (!isPosNumber(value)) return callback(new Error('请输入正确的管道显示宽度(正数)'))
      return callback()
    }
    return {
      lineTypeTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'pipe:linetype:add'],
        edit: ['admin', 'pipe:linetype:edit'],
        del: ['admin', 'pipe:linetype:del']
      },
      rules: {
        name: [{ required: true, message: '请输入管道类型名称', trigger: 'blur' }],
        width: [{ required: true, validator: chackWidth, trigger: 'blur' }],
        color: [{ required: true, message: '请输入管道显示颜色', trigger: 'blur' }]
      },
    }
  },
  created() {
  },
  methods: {
  }
}
</script>

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