<template>
  <div>
    <div v-if="query.dictName === ''">
      <div class="my-code">点击编码查看详情</div>
    </div>
    <div v-else>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input
            v-model="query.label"
            clearable
            size="mini"
            placeholder="输入编码标签查询"
            style="width: 200px;"
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
          <rrOperation />
        </div>
      </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="mini" label-width="80px">
          <el-form-item label="编码类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择" @change="changeType">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <div v-if="form.type === '01' || form.type === '03'">
            <el-form-item label="初始值" prop="init_value">
              <el-input v-model="form.init_value" style="width: 370px;" />
            </el-form-item>
          </div>
          <div v-if="form.type === '02'">
            <el-form-item label="格式" prop="format">
              <el-input v-model="form.format" style="width: 370px;" placeholder="输入日期格式例：yyyyMMdd" />
            </el-form-item>
          </div>
          <div v-if="form.type === '03'">
            <el-form-item label="步长" prop="step">
              <el-input v-model="form.step" style="width: 370px;" />
            </el-form-item>
            <el-form-item label="填充值" prop="fillchar">
              <el-input v-model="form.fillchar" style="width: 370px;" />
            </el-form-item>
            <el-form-item label="长度" prop="length">
              <el-input v-model="form.length" style="width: 370px;" />
            </el-form-item>
            <el-form-item label="最大值" prop="max_value">
              <el-input v-model="form.max_value" style="width: 370px;" />
            </el-form-item>
          </div>
          <el-form-item label="排序" prop="sort_num">
            <el-input-number
              v-model.number="form.sort_num"
              :min="0"
              :max="999"
              controls-position="right"
              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
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column label="所属编码">
          {{ query.dictName }}
        </el-table-column>
        <el-table-column prop="type" label="类型" :formatter="typeFormate" />
        <el-table-column prop="init_value" label="初始值" />
        <el-table-column prop="current_value" label="当前值" />
        <el-table-column
          v-permission="['admin','dict:edit','dict: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>
  </div>
</template>

<script>
import crudCodeDetail from '@/api/tools/codeDetail'
import CRUD, { presenter, header, form } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'

const defaultForm = {
  id: null,
  type: '01',
  init_value: null,
  format: null,
  step: null,
  fillchar: null,
  length: null,
  sort_num: 0
}

export default {
  components: { pagination, rrOperation, udOperation },
  cruds() {
    return [
      CRUD({
        title: '编码详情', url: 'api/codeDetail', query: { dictName: '' }, sort: ['sort_num,asc'],
        crudMethod: { ...crudCodeDetail },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false
        },
        queryOnPresenterCreated: false
      })
    ]
  },
  mixins: [
    presenter(),
    header(),
    form(function() {
      return Object.assign({ dict: { id: this.dictId }}, defaultForm)
    })],
  data() {
    return {
      options: [{
        value: '01',
        label: '固定'
      }, {
        value: '02',
        label: '日期'
      }, {
        value: '03',
        label: '顺序'
      }],
      value: '',
      dictId: null,
      rules: {
        type: [
          { required: true, message: '请输入编码标签', trigger: 'blur' }
        ],
        init_value: [
          { required: true, message: '请输入初始值', trigger: 'blur' }
        ],
        sort_num: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ],
        format: [
          { required: true, message: '请输入日期格式', trigger: 'blur' }
        ],
        max_value: [
          { required: true, message: '请输入最大值', trigger: 'blur' }
        ]
      },
      permission: {
        add: ['admin', 'dict:add'],
        edit: ['admin', 'dict:edit'],
        del: ['admin', 'dict:del']
      },
      typeFormate(row, index) {
        if (row.type === '01') {
          return '固定'
        } else if (row.type === '02') {
          return '时间'
        } else if (row.type === '03') {
          return '顺序'
        }
      }
    }
  },
  methods: {
    changeType(val) {
      // 新增的时候，并且类型为顺序类型的时候赋初值操作
      if (this.crud.status.add === 1) {
        if (val === '03') {
          this.form.step = 1
          this.form.length = '3'
          this.form.fillchar = '0'
        } else {
          this.form.step = null
          this.form.length = null
          this.form.fillchar = null
        }
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
