<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="额度库编码：">
        <el-input v-model="form.quotaNo" placeholder="请输入额度库编码" clearable />
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getDialog('新增')"
          v-hasPermi="['service:quota:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="额度库编码" prop="packetNo" align="center" show-overflow-tooltip />
      <el-table-column label="总额度 / 天" prop="totalQuota" align="center" />
      <el-table-column label="额度剩余 / 天" prop="mayQuota" align="center" />
      <el-table-column label="生效起始时间" prop="startDate" align="center" />
      <el-table-column label="生效结束时间" prop="expireDate" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog('查看', scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="600px" :before-close="getCancel">
      <el-form :model="dialogForm" ref="dialogForm" label-width="120px" :disabled="disabled">
        <el-form-item label="额度库时间：" prop="totalQuota">
          <el-input-number v-model="dialogForm.totalQuota" :min="1" step-strictly :controls="false" /> / 天
        </el-form-item>
        <el-form-item label="生效起始时间：" prop="startDate">
          <el-date-picker v-model="dialogForm.startDate" type="datetime" placeholder="请选择生效起始时间"
            :picker-options="pickerOptions" value-format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="getCancel">取消</el-button>
        <el-button v-if="!disabled" type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, detail, create } from '@/api/service/quota'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      // 弹窗名称
      title: '新增',
      // 弹窗表单
      dialogForm: {},
      // 当前状态
      statusOptions: [{
        value: 0,
        label: '待生效'
      }, {
        value: 1,
        label: '生效中'
      }, {
        value: 2,
        label: '已失效'
      }, {
        value: 3,
        label: '已过期'
      }],
      // 定义时间选择器
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < (Date.now() - (24 * 60 * 60 * 1000))
        }
      },
      // 只读
      disabled: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 新增 / 查看
    getDialog(title, id) {
      this.dialogVisible = true
      this.title = title
      this.disabled = title === '查看' ? true : false
      if (id) {
        detail(id).then(res => {
          this.dialogForm = res.data
        })
      }
    },

    // 取消
    getCancel() {
      this.dialogVisible = false
      this.dialogForm = {}
    },

    // 确定
    getSubmit() {
      create(this.dialogForm).then(res => {
        if (res.code === 0) {
          this.$message.success('新增成功！')
          this.getCancel()
          this.getList()
        }
      })
    },

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>