<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <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="600px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="设备类型名称" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="设备类型排序" prop="sort">
          <el-input-number
            v-model.number="form.sort"
            :min="0"
            :max="999"
            controls-position="right"
            style="width: 370px"
          />
        </el-form-item>
        <el-form-item label="所属类型" prop="parentId">
          <treeselect
            v-model="form.parentId"
            filterable
            :defaultExpandLevel="3"
            :options="crud.data"
            style="width: 370px"
            placeholder="选择所属类型"
          />
        </el-form-item>
        <el-form-item label="窗口显示基础字段" prop="windowDataFileds">
          <el-select v-model="form.windowDataFileds" multiple style="width: 370px" placeholder="选择窗口显示基础字段">
            <el-option
              v-for="item in optionsObj.EquipmentFiled"
              :key="item.value"
              :label="item.label"
              :value="+item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="表列显示基础字段" prop="tableColFileds">
          <el-select v-model="form.tableColFileds" multiple style="width: 370px" placeholder="表列显示基础字段">
            <el-option
              v-for="item in optionsObj.EquipmentFiled"
              :key="item.value"
              :label="item.label"
              :value="+item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检定管理" prop="calibrationEnable">
          <el-switch
            v-model="form.calibrationEnable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
          >
          </el-switch>
        </el-form-item>
        <el-form-item style="margin-bottom: 0" label="备注" prop="remark">
          <el-input v-model="form.remark" style="width: 370px" />
        </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
      v-loading="crud.loading"
      :data="crud.data"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'nohasChildren' }"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in typeTableOptions"
        :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', 'equipment:type:edit', 'equipment:type:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :disabled-dle="scope.row.id === 1"
            msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudType from '@/api/equipment/type'
import { getOptions } from '@/api/equipment/public'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'

// vue-treeselect
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

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

const defaultForm = {
  id: '',
  name: '',
  sort: 999,
  parentId: null,
  calibrationEnable: false,
  windowDataFileds: [],
  remark: '',
}
export default {
  name: 'EquipmentType',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    Treeselect,
  },
  cruds() {
    return CRUD({
      title: '设备类型',
      url: 'equipment/type/getTree',
      crudMethod: { ...crudType },
    })
  },
  data() {
    return {
      typeTableOptions,
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        sort: [
          {
            required: true,
            message: '请输入序号',
            trigger: 'blur',
            type: 'number',
          },
        ],
      },
      permission: {
        add: ['admin', 'equipment:type:add'],
        edit: ['admin', 'equipment:type:edit'],
        del: ['admin', 'equipment:type:del'],
      },
      optionsObj: { EquipmentFiled: [] },
    }
  },
  created() {
    this.getOptions()
  },
  watch: {},
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {},
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.parentId == '0') {
        form.parentId = null
      }
    },
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {
      if (this.form.parentId !== null && this.form.parentId === this.form.id) {
        this.$message({
          message: '上级区域不能是自身',
          type: 'warning',
        })
        return false
      }
      return true
    },
    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 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;
}
</style>
