<template>
  <div>
    <div v-if="query.dictName === ''">
      <div class="my-code">{{ $t('Code_detail.msg.m2') }}</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="$t('Code_detail.msg.m3')"
            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="90px">
          <el-form-item :label="$t('Code_detail.dialog.type')" prop="type">
            <el-select v-model="form.type" :placeholder="$t('common.Please_select')" @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="$t('Code_detail.dialog.init_value')" 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="$t('Code_detail.dialog.format')" prop="format">
              <el-input v-model="form.format" style="width: 370px;" :placeholder="$t('Code_detail.msg.m1')" />
            </el-form-item>
          </div>
          <div v-if="form.type === '03'">
            <el-form-item :label="$t('Code_detail.dialog.step')" prop="step">
              <el-input v-model="form.step" style="width: 370px;" />
            </el-form-item>
            <el-form-item :label="$t('Code_detail.dialog.fillchar')" prop="fillchar">
              <el-input v-model="form.fillchar" style="width: 370px;" />
            </el-form-item>
            <el-form-item :label="$t('Code_detail.dialog.length')" prop="length">
              <el-input v-model="form.length" style="width: 370px;" />
            </el-form-item>
            <el-form-item :label="$t('Code_detail.dialog.max_value')" prop="max_value">
              <el-input v-model="form.max_value" style="width: 370px;" />
            </el-form-item>
          </div>
          <el-form-item :label="$t('Code_detail.dialog.sort_num')" 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">{{ $t('common.Cancel') }}</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">{{ $t('common.Confirm') }}</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="$t('Code_detail.table.dictName')">
          {{ query.dictName }}
        </el-table-column>
        <el-table-column prop="type" :label="$t('Code_detail.table.type')" :formatter="typeFormate" />
        <el-table-column prop="init_value" :label="$t('Code_detail.table.init_value')" />
        <el-table-column prop="current_value" :label="$t('Code_detail.table.current_value')" />
        <el-table-column
          v-permission="['admin','dict:edit','dict:del']"
          :label="$t('common.Operate')"
          width="160px"
          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 '@/views/tools/codeGen/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'
import i18n from '@/i18n'

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: i18n.t('Code_detail.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({ code_rule_id: this.dictId }, defaultForm)
    })],
  data() {
    return {
      options: [{
        value: '01',
        label: i18n.t('Code_detail.options.o1')
      }, {
        value: '02',
        label: i18n.t('Code_detail.options.o2')
      }, {
        value: '03',
        label: i18n.t('Code_detail.options.o3')
      }],
      value: '',
      dictId: null,
      rules: {
        type: [
          { required: true, message: i18n.t('Code_detail.rules.r1'), trigger: 'blur' }
        ],
        init_value: [
          { required: true, message: i18n.t('Code_detail.rules.r2'), trigger: 'blur' }
        ],
        sort_num: [
          { required: true, message: i18n.t('Code_detail.rules.r3'), trigger: 'blur', type: 'number' }
        ],
        format: [
          { required: true, message: i18n.t('Code_detail.rules.r4'), trigger: 'blur' }
        ],
        max_value: [
          { required: true, message: i18n.t('Code_detail.rules.r5'), trigger: 'blur' }
        ]
      },
      permission: {
        add: ['admin', 'dict:add'],
        edit: ['admin', 'dict:edit'],
        del: ['admin', 'dict:del']
      },
      typeFormate(row, index) {
        if (row.type === '01') {
          return i18n.t('Code_detail.options.o1')
        } else if (row.type === '02') {
          return i18n.t('Code_detail.options.o2')
        } else if (row.type === '03') {
          return i18n.t('Code_detail.options.o3')
        }
      }
    }
  },
  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>
