<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <SearchBar :collapse-span="12" :show-more="false">
        <template #header>
          <el-col :span="6">
            <el-form-item label="业务对象" prop="businessCode">
              <el-select
                v-model="queryParams.businessCode"
                placeholder="请选择业务对象"
                clearable
                disabled
              >
                <el-option
                  v-for="dict in getDictDatas(DICT_TYPE.EIM_BUSINESS_OBJECT)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #buttons>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </template>
      </SearchBar>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['eim:num-rule-details:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['eim:num-rule-details:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="业务对象" align="center" prop="businessCode">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.EIM_BUSINESS_OBJECT" :value="scope.row.businessCode" />
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="orderNum" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.EIM_BUSINESS_OBJECT_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="设置值" align="center" prop="value" />
      <el-table-column label="长度" align="center" prop="len" />
      <el-table-column label="起始值" align="center" prop="initial" />
      <el-table-column label="步长" align="center" prop="stepSize" />
      <el-table-column label="补位符" align="center" prop="fillKey" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['eim:num-rule-details:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['eim:num-rule-details:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="text-danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="序号" prop="orderNum">
          <el-input v-model.number="form.orderNum" type="number" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in getDictDatas(DICT_TYPE.EIM_BUSINESS_OBJECT_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设置值" prop="value">
          <el-input
            v-if="form.type !== '2'"
            v-model="form.value"
            :disabled="['3','4','5'].includes(form.type)"
            placeholder="请输入设置值"
          />
          <!-- <el-select v-else v-model="form.value" placeholder="请选择日期类型">
            <el-option
              v-for="dict in getDictDatas(DICT_TYPE.EIM_BUSINESS_OBJECT_DATE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select> -->
          <DictSelect
            v-else
            v-model="form.value"
            placeholder="请选择日期类型"
            dict-type="EIM_BUSINESS_OBJECT_DATE"
          />
        </el-form-item>
        <el-form-item label="长度" prop="len">
          <el-input
            v-model.number="form.len"
            :disabled="form.type === '1' || form.type === '2'"
            type="number"
            placeholder="请输入长度"
          />
        </el-form-item>
        <el-form-item label="起始值" prop="initial">
          <el-input
            v-model.number="form.initial"
            :disabled="form.type === '1' || form.type === '2'"
            type="number"
            placeholder="请输入起始值"
          />
        </el-form-item>
        <el-form-item label="步长" prop="stepSize">
          <el-input
            v-model.number="form.stepSize"
            :disabled="form.type === '1' || form.type === '2'"
            type="number"
            placeholder="请输入步长"
          />
        </el-form-item>
        <el-form-item label="补位符" prop="fillKey">
          <el-input v-model="form.fillKey" :disabled="form.type === '1' || form.type === '2'" placeholder="请输入补位符" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
            :autosize="{ minRows: 2, maxRows: 4}"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>

        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createNumRuleDetails, updateNumRuleDetails, deleteNumRuleDetails, getNumRuleDetails, getNumRuleDetailsPage, exportNumRuleDetailsExcel } from '@/api/system/encode/details'
import SearchBar from '@/components/SearchBar'

export default {
  name: 'SystemEncodeDetails',
  components: {
    SearchBar
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 编号规则明细列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        ruleId: null,
        businessCode: null,
        orderNum: null,
        type: null,
        value: null,
        len: null,
        initial: null,
        stepSize: null,
        fillKey: null,
        createBy: null,
        createTime: [],
        updateBy: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNum: [{ required: true, message: '序号不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类型不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.initPage()
    this.getList()
  },
  methods: {
    initPage() {
      this.queryParams.businessCode = this.$route.query.businessCode
      this.queryParams.ruleId = this.$route.query.id
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getNumRuleDetailsPage(this.queryParams).then(response => {
        this.list = response.data.tableDataInfo.rows
        this.total = response.data.tableDataInfo.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        ruleId: this.$route.query.id,
        businessCode: this.$route.query.businessCode,
        orderNum: undefined,
        type: undefined,
        value: undefined,
        len: undefined,
        initial: undefined,
        stepSize: undefined,
        fillKey: undefined,
        createBy: undefined,
        updateBy: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加编号规则明细'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getNumRuleDetails(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改编号规则明细'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id) {
          updateNumRuleDetails(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createNumRuleDetails(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除编号规则明细编号为"' + id + '"的数据项?').then(function() {
        return deleteNumRuleDetails(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有编号规则明细数据项?').then(() => {
        this.exportLoading = true
        return exportNumRuleDetailsExcel(params)
      }).then(response => {
        this.$download.excel(response, '编号规则明细.xls')
        this.exportLoading = false
      }).catch(() => {})
    }
  }
}
</script>
