<template>
  <div>
    <div v-if="query.code === ''">
      <div class="my-code">{{ $t('Dict.detail.see_title') }}</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('Dict.detail.input_tip')"
            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="computedLabelWidth"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" :label-width="computedFormLabelWidth">
          <el-form-item :label="$t('Dict.detail.table.label')" prop="label">
            <el-input v-model="form.label" style="width: 240px;" />
          </el-form-item>
          <el-form-item :label="$t('Dict.detail.table.value')" prop="value">
            <el-input v-model="form.value" style="width: 240px;" />
          </el-form-item>
          <el-form-item :label="$t('Dict.detail.table.sort')" prop="dict_sort">
            <el-input-number
              v-model.number="form.dict_sort"
              :min="0"
              :max="999"
              controls-position="right"
              style="width: 240px;"
            />
          </el-form-item>
          <el-form-item :label="$t('Dict.detail.table.param1')" prop="para1">
            <el-input v-model="form.para1" style="width: 240px;" />
          </el-form-item>
          <el-form-item :label="$t('Dict.detail.table.param2')" prop="para2">
            <el-input v-model="form.para2" style="width: 240px;" />
          </el-form-item>
          <el-form-item :label="$t('Dict.detail.table.param3')" prop="para3">
            <el-input v-model="form.para3" style="width: 240px;" />
          </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('Dict.detail.table.code')" min-width="150" show-overflow-tooltip>
          {{ query.code }}
        </el-table-column>
        <el-table-column
          prop="label"
          :label="$t('Dict.detail.table.label')"
          align="center"
          :min-width="flexWidth('label',crud.data,$t('Dict.detail.table.label'))"
          show-overflow-tooltip
        />
        <el-table-column
          prop="value"
          :label="$t('Dict.detail.table.value')"
          :min-width="flexWidth('value',crud.data,$t('Dict.detail.table.value'))"
          align="center"
        />
        <el-table-column
          prop="dict_sort"
          :label="$t('Dict.detail.table.sort')"
          :min-width="flexWidth('dict_sort',crud.data,$t('Dict.detail.table.sort'))"
          align="center"
        />
        <el-table-column
          prop="para1"
          :label="$t('Dict.detail.table.param1')"
          :min-width="flexWidth('para1',crud.data,$t('Dict.detail.table.param1'))"
          align="center"
        />
        <el-table-column
          prop="para2"
          :label="$t('Dict.detail.table.param2')"
          :min-width="flexWidth('para2',crud.data,$t('Dict.detail.table.param2'))"
          align="center"
        />
        <el-table-column
          prop="para3"
          :label="$t('Dict.detail.table.param3')"
          :min-width="flexWidth('para3',crud.data,$t('Dict.detail.table.param3'))"
          align="center"
        />
        <el-table-column
          v-permission="['admin','dict:edit','dict:del']"
          :label="$t('common.Operate')"
          align="center"
          width="165px"
          fixed="right"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDictDetail from '@/views/system/dict/dictDetail'
import CRUD, { form, header, presenter } 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 = {
  dict_id: null,
  name: null,
  label: null,
  value: null,
  dict_sort: null,
  dictType: null,
  para1: null,
  para2: null,
  para3: null,
  createId: null,
  createName: null,
  createTime: null,
  updateId: null,
  updateName: null,
  updateTime: null
}

export default {
  components: { pagination, rrOperation, udOperation },
  cruds() {
    return [
      CRUD({
        title: i18n.t('Dict.table.dict_detail'),
        url: 'api/dict/dictDetail',
        query: { code: '' },
        idField: 'dict_id',
        sort: ['dict_sort,asc', 'dict_id,desc'],
        crudMethod: { ...crudDictDetail },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false
        },
        queryOnPresenterCreated: false
      })
    ]
  },
  mixins: [
    presenter(),
    header(),
    form(function() {
      return Object.assign({ dict: { dict_id: this.dict_id }}, defaultForm)
    })],
  data() {
    return {
      dict_id: null,
      rules: {
        label: [
          { required: true, message: i18n.t('Dict.detail.rules.r1'), trigger: 'blur' }
        ],
        value: [
          { required: true, message: i18n.t('Dict.detail.rules.r2'), trigger: 'blur' }
        ],
        dict_sort: [
          { required: true, message: i18n.t('Dict.detail.rules.r3'), trigger: 'blur', type: 'number' }
        ]
      },
      permission: {
        add: ['admin', 'dict:add'],
        edit: ['admin', 'dict:edit'],
        del: ['admin', 'dict:del']
      }
    }
  },
  computed: {
    computedLabelWidth() {
      const item = localStorage.getItem('lang')
      if (item === 'en') {
        return `450px`
      }
      return `420px`
    },
    computedFormLabelWidth() {
      const item = localStorage.getItem('lang')
      if (item === 'en') {
        return `140px`
      }
      return `100px`
    }
  }
}
</script>

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