<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading" @selection-change="handleSelectionChange">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" v-if="checkPermission(['numberRule:add'])" type="primary"
            @click="handleAddNumber">新增</el-button>
          <el-button size="mini" v-if="checkPermission(['numberRule:del'])" type="danger" @click="handleDeleteNumber()"
            :disabled="multipleSelection.length === 0">删除</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入关键字搜索" />
        </div>
      </div>
      <template slot="table-columns">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="规则名称" />
        <el-table-column label="业务类型">
          <template slot-scope="scope">
            {{ seqTypeType(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column label="生成编码方式">
          <template slot-scope="scope">
            {{ seqTypeInfo(scope.row.seq_generate_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="seq_begin_number" label="开始顺序号" />
        <el-table-column label="是否启用">
          <template slot-scope="{ row }">{{ row.open_flag === true ? '是' : '否' }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" v-if="checkPermission(['numberRule:edit'])"
              @click="handleEditNumber(scope.$index, scope.row)" type="primary">编辑</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
    <el-dialog :title="numberForm.id ? '编辑规则' : '添加规则'" :visible.sync="dialogVisible" width="800px">
      <el-form :model="numberForm" :rules="numberRules" ref="numberForm" label-width="120px" inline style="width:800px">
        <el-form-item label="规则名称" prop="name">
          <el-input v-model="numberForm.name" :minlength="2" :maxlength="20" clearable placeholder="请输入规则名称"></el-input>
        </el-form-item>
        <el-form-item label="业务类型" prop="type">
          <el-select v-model="numberForm.type">
            <el-option v-for="item in businessType" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="生成规则方式" prop="seq_generate_type">
          <el-select v-model="numberForm.seq_generate_type">
            <el-option v-for="item in seqType" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始顺序号" prop="seq_begin_number">
          <el-input v-model="numberForm.seq_begin_number" :minlength="2" :maxlength="20" clearable
            placeholder="请输入开始顺序号"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="open_flag">
          <el-radio v-model="numberForm.open_flag" :label="true">是</el-radio>
          <el-radio v-model="numberForm.open_flag" :label="false">否</el-radio>
        </el-form-item>
        <el-form-item label="规则类型">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline" @click="roleTypeData.push({
            type: roleType.find(function(sc) { return sc.default_flag === true }) ? roleType.find(function(sc) { return sc.default_flag === true }).label : '',
            split: roleSplit.find(function(sc) { return sc.default_flag === true }) ? roleSplit.find(function(sc) { return sc.default_flag === true }).label : ''
          })"></el-button>
          <el-table :data="roleTypeData" border :cell-style="{ textAlign: 'center' }"
            :header-cell-style="{ textAlign: 'center', background: '#f4f4f4' }" style="width: 100%; margin-top: 20px;">
            <el-table-column label="常量" width="150">
              <template slot-scope="scope">
                <el-select v-model="scope.row.type">
                  <el-option v-for="item in roleType" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column>
              <template slot-scope="scope">
                <span v-if="scope.row.type === 'Const'">值</span>
                <span v-else-if="scope.row.type === 'Seq'">位数</span>
              </template>
            </el-table-column>
            <el-table-column width="150">
              <template slot-scope="scope">
                <el-input v-if="scope.row.type === 'Const'" v-model="scope.row.const_value" :minlength="2"
                  :maxlength="20"></el-input>
                <el-input v-if="scope.row.type === 'Seq'" v-model="scope.row.seq_length" :minlength="2"
                  :maxlength="20"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="分隔符" width="150">
              <template slot-scope="scope">
                <el-select v-model="scope.row.split">
                  <el-option v-for="item in roleSplit" :key="item.label" :label="item.label" :value="item.label" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button size="mini" type="danger" @click="roleTypeData.splice(scope.$index, 1)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitNumberForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_BasicSetting from '@/api/basicSetting'
import * as API_Setting from '@/api/setting'
export default {
  name: 'numberRule',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 规则表单
      numberForm: {},
      roleType: [],
      roleSplit: [],
      seqType: [],
      roleTypeData: [],
      businessType: [],
      // 规则表单 规则
      numberRules: {},
      // 规则表单 dialog
      dialogVisible: false,
      multipleSelection: []
    }
  },
  mounted() {
    this.GET_NoGenerateRuleList()
    this.GET_DictDataInfo()
  },
  methods: {
    seqTypeType(val) {
      if (val) {
        return this.businessType.find(function(sc) { return sc.value === val }).label
      }
    },
    seqTypeInfo(val) {
      if (val) {
        return this.seqType.find(function(sc) { return sc.value === val }).label
      }
    },
    GET_DictDataInfo() {
      API_Setting.getDictDataInfo('erp_no_general_rule_item_split').then(response => {
        this.roleSplit = response
        API_Setting.getDictDataInfo('erp_no_general_rule_item_type').then(response => {
          this.roleType = response
          this.roleTypeData.push({
            type: this.roleType.find(function(sc) { return sc.default_flag === true }) ? this.roleType.find(function(sc) { return sc.default_flag === true }).value : '',
            split: this.roleSplit.find(function(sc) { return sc.default_flag === true }) ? this.roleSplit.find(function(sc) { return sc.default_flag === true }).label : ''
          })
        })
      })
      API_Setting.getDictDataInfo('erp_no_general_rule_seq_type').then(res => {
        this.seqType = res
      })
      API_Setting.getDictDataInfo('erp_no_business_type').then(res => {
        this.businessType = res
      })
    },
    // 单选或者全选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_NoGenerateRuleList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_NoGenerateRuleList()
    },

    /** 添加规则 */
    handleAddNumber() {
      this.numberForm = {
        open_flag: true,
        type: this.businessType.find(function(sc) { return sc.default_flag === true }) ? this.businessType.find(function(sc) { return sc.default_flag === true }).value : '',
        seq_generate_type: this.seqType.find(function(sc) { return sc.default_flag === true }) ? this.seqType.find(function(sc) { return sc.default_flag === true }).value : ''
      }
      this.dialogVisible = true
    },

    /** 编辑规则 */
    handleEditNumber(index, row) {
      this.numberForm = this.MixinClone(row)
      this.roleTypeData = row.item_list
      this.dialogVisible = true
    },

    /** 删除规则 */
    handleDeleteNumber() {
      this.$confirm('确定要删除这个规则吗？', '提示', { type: 'warning' }).then(() => {
        const ids = this.multipleSelection.map(item => item.id)
        API_BasicSetting.deleteNoGenerateRule(ids).then(() => {
          this.$message.success('删除成功！')
          this.GET_NoGenerateRuleList()
        })
      }).catch(() => { })
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['numberForm'].clearValidate() })
    },

    /** 提交规则表单 */
    submitNumberForm() {
      this.$refs['numberForm'].validate((valid) => {
        if (valid) {
          const { id } = this.numberForm
          const params = this.MixinClone(this.numberForm)
          if (this.roleTypeData && this.roleTypeData.length) {
            params.item_list = this.roleTypeData
          }
          if (!params.type) {
            params.type = null
          }
          if (id) {
            API_BasicSetting.editNoGenerateRule(id, params).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_NoGenerateRuleList()
            })
          } else {
            API_BasicSetting.addNoGenerateRule(params).then(() => {
              this.dialogVisible = false
              this.$message.success('添加成功！')
              this.GET_NoGenerateRuleList()
            })
          }
        } else {
          this.$message.error('表单填写有误，请核对！')
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        name: data
      }
      this.params.page_no = 1
      this.GET_NoGenerateRuleList()
    },

    /** 获取规则列表 */
    GET_NoGenerateRuleList() {
      this.loading = true
      API_BasicSetting.getNoGenerateRuleList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
