<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable prefix-icon="el-icon-document" style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="批次名称" prop="batchName">
          <el-input v-model="queryParams.batchName" placeholder="请输入批次名称" clearable prefix-icon="el-icon-search" style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="所属企业" prop="companyName">
          <el-select v-model="queryParams.companyName" placeholder="请选择企业" clearable>
            <el-option v-for="company in companyData" :key="company.id" :label="company.name" :value="company.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="过期时间" prop="lastExpireTime">
          <el-date-picker v-model="queryParams.lastExpireTime" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
            value-format="yyyy-MM-dd" style="width: 240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card table-card" shadow="never">
      <div slot="header" class="clearfix">
        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </div>

      <Page uri="/mapi/recharge/card/batchRecord/findPage" :request-params="queryParams" ref="pagination">
        <el-table-column label="编号" align="center" prop="id" min-width="100" />
        <el-table-column label="批次号" align="center" prop="batchNo" min-width="160" :show-overflow-tooltip="true" />
        <el-table-column label="批次名称" align="center" prop="batchName" min-width="160" :show-overflow-tooltip="true" />
        <el-table-column label="所属企业" align="center" prop="companyName" min-width="130" :show-overflow-tooltip="true" />
        <el-table-column label="单卡实收金额" align="center" prop="actualAmount" min-width="130" :show-overflow-tooltip="true" />
        <el-table-column label="备注" align="center" prop="remark" min-width="200" :show-overflow-tooltip="true" />
        <el-table-column label="激活过期时间" align="center" prop="lastExpireTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.lastExpireTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createBy" width="120" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!--                        <el-button-->
            <!--                            type="text"-->
            <!--                            icon="el-icon-edit"-->
            <!--                            @click="handleUpdateExpireTime(scope.row)"-->
            <!--                            v-hasPermi="['recharge:batchRecord:edit']"-->
            <!--                        >修改过期时间</el-button>-->
            <el-button type="text" icon="el-icon-tickets" @click="handleCardDetail(scope.row)" v-hasPermi="['recharge:batchRecord:edit']">充值卡明细
            </el-button>
            <el-divider direction="vertical" />
            <el-button type="text" icon="el-icon-document" @click="handleUseRecord(scope.row)" v-hasPermi="['recharge:batchRecord:edit']">使用记录
            </el-button>
          </template>
        </el-table-column>
      </Page>
    </el-card>

    <!-- 修改过期时间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-card class="box-card" shadow="never">
          <div slot="header">
            <span>基本信息</span>
          </div>
          <el-descriptions :column="1" border>
            <el-descriptions-item>
              <template slot="label">批次号</template>
              {{ form.batchNo }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">批次名称</template>
              {{ form.batchName }}
            </el-descriptions-item>
          </el-descriptions>

          <el-divider content-position="left">过期时间设置</el-divider>

          <el-form-item label="激活过期时间" prop="lastExpireTime">
            <el-date-picker v-model="form.lastExpireTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width: 100%" />
          </el-form-item>
        </el-card>

        <el-card class="box-card permission-card" shadow="never">
          <div slot="header">
            <span>权限设置</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="洗衣权限">
                <el-radio-group v-model="form.isSupportCloth">
                  <el-radio-button label="Y">有</el-radio-button>
                  <el-radio-button label="N">无</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品权限">
                <el-radio-group v-model="form.isSupportGoods">
                  <el-radio-button label="Y">有</el-radio-button>
                  <el-radio-button label="N">无</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生活服务权限">
                <el-radio-group v-model="form.isSupportLife">
                  <el-radio-button label="Y">有</el-radio-button>
                  <el-radio-button label="N">无</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="汽车美容权限">
                <el-radio-group v-model="form.isSupportCar">
                  <el-radio-button label="Y">有</el-radio-button>
                  <el-radio-button label="N">无</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findBatchRecordPage, getOneByBatchNo, updateExpireTimeByBatchNo, updateUseLevelByBatchNo } from '../../../api/recharge/batchRecord'
import { getGoods } from '@/api/core/goods'

export default {
  name: 'BatchRecord',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值批次记录表格数据
      companyData: [],
      batchRecordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        batchNo: null,
        batchName: null,
        lastExpireTime: null,
        lastUseLevel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lastExpireTime: [{ required: true, message: '到期时间不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    this.getCompanyData()
  },
  methods: {
    getCompanyData() {
      this.getGroupData("company_info").then((res) => {
        this.companyData = res.data
      })
    },
    /** 查询充值批次记录列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        batchNo: null,
        batchName: null,
        lastExpireTime: null,
        lastUseLevel: null,
        createById: null,
        createBy: null,
        createTime: null,
        updateById: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 过期时间按钮操作 */
    handleUpdateExpireTime(row) {
      getOneByBatchNo(row.batchNo).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改批次号信息'
      })
    },
    /** 使用情况按钮操作 */
    handleUseRecord(row) {
      this.$tab.openPage(row.batchName + '批次的使用情况', '/recharge/useRecord/' + row.batchNo)
    },
    /** 卡明细按钮操作 */
    handleCardDetail(row) {
      this.$tab.openPage(row.batchName + '批次的充值卡明细', '/recharge/cardDetail/' + row.batchNo)
    },
    handleUpdateUseLevel(row) {
      var form = { batchNo: row.batchNo, useLevel: row.lastUseLevel }
      let text = row.lastUseLevel == '1' ? '启用' : '停用'
      this.$confirm('确认要' + text + ' ' + row.batchName + ' 优先扣款吗？')
        .then(function () {
          return updateUseLevelByBatchNo(form)
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功')
        })
        .catch(function () {
          row.status = row.status == '0' ? '1' : '0'
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var params = {
            batchNo: this.form.batchNo,
            expireTime: this.form.lastExpireTime,
            useLevel: this.form.lastUseLevel,
            starLevel: this.form.starLevel,
            isSupportCloth: this.form.isSupportCloth,
            isSupportGoods: this.form.isSupportGoods,
            isSupportLife: this.form.isSupportLife,
            isSupportCar: this.form.isSupportLife
          }
          updateExpireTimeByBatchNo(params).then((response) => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    checkClose(done) {
      this.$confirm('是否关闭表单，关闭后数据将丢失？')
        .then(function () {
          done()
        })
        .then(() => { })
        .catch(() => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
    padding: 20px;
    background-color: #f5f7fa;

    .box-card {
        margin-bottom: 20px;

        &.table-card {
            margin-bottom: 0;
        }

        &.permission-card {
            margin-top: 20px;
        }
    }

    :deep(.el-card__header) {
        padding: 15px 20px;
        border-bottom: 1px solid #ebeef5;
    }

    :deep(.el-card__body) {
        padding: 20px;
    }

    .dialog-footer {
        text-align: center;
    }

    .el-divider__text {
        font-size: 14px;
        font-weight: bold;
    }

    :deep(.el-descriptions) {
        margin: 20px 0;

        .el-descriptions-item__label {
            width: 120px;
            font-weight: bold;
        }
    }
}
</style>
