<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">模糊搜索</label>
        <el-input v-model="query.search" clearable placeholder="项点编码或名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="475px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="120px">
          <el-form-item label="项点编码" prop="item_code">
            <el-input v-model="form.item_code" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="项点名称" prop="item_name">
            <el-input v-model="form.item_name" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="顺序号" prop="order_seq">
            <el-input-number v-model="form.order_seq" controls-position="center" style="width: 250px;text-align: left" :min="1" />
          </el-form-item>
          <el-form-item label="默认值数据类型" prop="data_type">
            <el-select
              v-model="form.data_type"
              placeholder=""
              style="width: 250px"
            >
              <el-option
                v-for="item in dict.ITEM_VALUE_TYPE"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="默认值">
            <el-select
              v-if="form.data_type === '01'"
              v-model="form.default_value"
              style="width: 250px;"
              placeholder="请先选择默认值数据类型"
            >
              <el-option
                v-for="item in dict.IS_OR_NOT"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-input v-else-if="form.data_type === '02'" type="number" v-model="form.default_value" style="width: 250px;" />
            <el-input v-else v-model="form.default_value" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 250px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="mini" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="item_code" label="项点编码" />
        <el-table-column prop="item_name" label="项点名称" />
        <el-table-column prop="order_seq" label="顺序号" />
        <el-table-column prop="default_value" label="默认值">
          <template slot-scope="scope">
            {{ is_or_no(scope.row.default_value, scope.row.data_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="data_type" label="默认值数据类型">
          <template slot-scope="scope">
            {{ dict.label.ITEM_VALUE_TYPE[scope.row.data_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDeviceitem from '@/api/wms/pdm/deviceitem'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { data_type: '01', item_id: null, item_code: null, item_name: null, default_value: null, remark: null, order_seq: null }
export default {
  name: 'Deviceitem',
  dicts: ['ITEM_VALUE_TYPE', 'IS_OR_NOT'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '生产设备扩展项', url: 'api/deviceitem', idField: 'item_id', sort: 'item_id,desc',
      optShow: {
        add: true,
        edit: true,
        del: true,
        reset: true,
        download: false
      },
      crudMethod: { ...crudDeviceitem }})
  },
  data() {
    return {
      permission: {
      },
      rules: {
        item_code: [
          { required: true, message: '项点编码不能为空', trigger: 'blur' }
        ],
        item_name: [
          { required: true, message: '项点名称不能为空', trigger: 'blur' }
        ],
        data_type: [
          { required: true, message: '默认值数据类型不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'item_code', display_name: '项点编码' }
      ]
    }
  },
  methods: {
    is_or_no(val, type) {
      if (type === '01' && val === '1') {
        return '是'
      } else if (type === '01' && val === '0') {
        return '否'
      } else {
        return val
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
