<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form
          inline
          onsubmit="return false"
          :label-width="60"
          class="search-form">
          <label class="label-sign"></label>
          <Form-item class="search-item" label="前缀:">
            <el-input v-model="formObj.pre" placeholder="请输入前缀" @keyup.enter.native="_formSearch" clearable></el-input>
          </Form-item>
          <Form-item class="search-btn">
            <el-button type="primary" @click="_formSearch">搜索</el-button>
          </Form-item>
        </Form>
      </el-col>
      <el-col :span="24" style="margin-bottom: 10px;">
        <div class="rule-color">
          <b>et</b> 指当前编号对象， <b>kt</b> 指 CodeKeyUtils 工具类 。 不同规则使用符号 <b>&</b> 分割，前缀 <b>@</b> 表示日期规则，前缀 <b>$</b> 属性规则，
          <b>SN</b> 表示自增序列号。<br/>
          规则需查询字典使用符号 <b>?</b> 分割 ，例如： <font class="blue-color">分类?${et.type}</font><br/>
          规则: <font class="red-color">{{ruleString}}</font><br/>
          例如：前缀设置为 HT 合同类型 1 名称为 “ 测试 ” 时间 2018年 5月 下一个序列号为3长度为5 <br/>
          结果为：<font class="blue-color">HT1CS20180500003</font>（ 标签 assign 语法为调用 kt 工具类的方法并执行 ）<br/>
        </div>
      </el-col>
      <!--按钮-->
      <el-col :span="24">
        <btn-list :msg="btn"  @on-result-change="_btnClick"/>
      </el-col>
      <!--table-->
      <el-col :span="24">
        <element-table
          :pageColumns="pageColumns"
          :tableHeight="tableHeight"
          @on-result-change="_tableResultChange"
          ref="pageTable"
          :getPage="getPage"
          :optColWidth="80"
          :iconMsg="iconMsg">
          <el-table-column
            show-overflow-tooltip
            sortable
            :prop="item.key"
            :label="item.title"
            :width="item.width"
            :min-width="200"
            :fixed="item.fixed?item.fixed:undefined"
            v-for="item in pageColumns"
            :key="item.key">
            <template slot-scope="scope">
              <span v-if="item.type" class="blue-color">{{scope.row[item.key] === undefined ? '': scope.row[item.key].display}}</span>
              <span v-else-if="item.detail"><a
                @click.stop="_detailModal(scope.row.id)">{{scope.row[item.key]}}</a></span>
              <span v-else>{{scope.row[item.key]}}</span>
            </template>
          </el-table-column>
        </element-table>
      </el-col>
    </el-row>
    <!--添加编辑-->
    <codeRuleEdit ref="editModal" @on-result-change="_page"></codeRuleEdit>
    <!--详情-->
    <codeRuleDetail ref="detailModal"></codeRuleDetail>
    <!--编号规则-->
    <codeRuleRecord ref="recordModal"></codeRuleRecord>
  </div>
</template>
<script>
import { codeRule } from '../../../api'
import codeRuleEdit from './codeRuleEdit'
import codeRuleDetail from './codeRuleDetail'
import codeRuleRecord from './codeRuleRecord'
export default {
  components: { codeRuleEdit, codeRuleDetail, codeRuleRecord },
  data() {
    return {
      btn: [{ type: 'success', id: '', name: '添加' }],
      formObj: {
        pre: ''
      },
      pageColumns: [
        { title: '类型', key: 'type', width: 150, type: true },
        { title: '前缀', key: 'pre', width: 150 },
        { title: '长度', key: 'length', width: 100 },
        { title: '规则', key: 'rule', detail: true }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-edit', id: '', name: '编辑' },
        { type: 'pt-a-clock', id: '', name: '查看编号记录' }
      ],
      tableHeight: this.$tableHeight('ruleHeight'),
      ruleString:
        '$' +
        '{et.type}' +
        '&' +
        '<' +
        '#' +
        'assign ' +
        'name=kt.getFirstSpell' +
        '("$' +
        '{et.name}"' +
        ',0,true)' +
        '>&@yyyyMM&SN'
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await codeRule.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '查看编号记录':
          this.$refs.recordModal._open(data.id)
          break
      }
    },
    _editModal: async function(id) {
      const result = await codeRule.getById(id)
      this.$refs.editModal._open(result)
    },
    // 详情
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    }
  }
}
</script>
