<template>
  <div>
    <div class="flex justify-between align-center">
      <div>
        <el-button size="mini" type="primary" @click="edit(-1)">添加</el-button>
        <el-button size="mini" type="primary" @click="download()">下载模板</el-button>
      </div>
      <!-- <el-button size="mini" type="success" @click="exit()">导出</el-button> -->
      <div>
        <el-button size="mini" v-if="radioRowValueList.state != undefined" type="warning" @click="infoDialog = true">导入</el-button>
        <el-button size="mini" type="" @click="detail" :disabled="radioRowValueList.state == undefined">明细</el-button>
        <el-button size="mini" type="danger" @click="del()" v-if="radioRowValueList.state==0" plain>删除</el-button>
        <el-button size="mini" type="primary" @click="edit(0)" :disabled="
              radioRowValueList.state == undefined ||
              radioRowValueList.state == 1||
              radioRowValueList.state == 3
          " plain>修改</el-button>
        <el-button size="mini" type="primary" @click="edit(1)" :disabled="
              radioRowValueList.state == 3||
              radioRowValueList.state == 1||
              radioRowValueList.state == undefined
          ">送审
        </el-button>
        <el-button size="mini" type="primary" @click="edit(2)" :disabled="
              radioRowValueList.state == undefined ||
              radioRowValueList.state == 0||
              radioRowValueList.state == 3 ||
              radioRowValueList.state == 2||
              radioRowValueList.state == 4
          " plain>回收</el-button>
        <el-button size="mini" type="primary" @click="edit(3)" :disabled="
              radioRowValueList.state == undefined ||
              radioRowValueList.state == 0||
              radioRowValueList.state == 3 ||
              radioRowValueList.state == 2||
              radioRowValueList.state == 4
          ">
          审核</el-button>
        <el-button size="mini" type="primary" @click="edit(4)" :disabled="
              radioRowValueList.state == undefined ||
              radioRowValueList.state == 1||
              radioRowValueList.state == 0||
              radioRowValueList.state == 4
          " plain>反审核</el-button>
      </div>
    </div>
    <el-table class="margin-top-xs" border @row-click="rowClick" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @row-dblclick="rowdbClick" max-height="650" :header-cell-style="{background: 'linear-gradient(#6cb3ff, #1873d4)',color:'#eeeeee'}">
      <el-table-column type="index" width="40" align="center"></el-table-column>
      <el-table-column align="center" min-width="50" label="选择">
        <template slot-scope="scope">
          <el-radio v-model="radioRowValue" :label="scope.row.id" @change.native="scope.row">{{null}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="75" label="状态">
        <template slot-scope="scope">
          <el-tag size="medium" effect="dark" :type="
                  scope.row.state == 0
                    ? ''
                    : '' || scope.row.state ==1
                    ? 'success'
                    : '' || scope.row.state == 2
                    ? 'info'
                    : '' || scope.row.state ==3
                    ? 'success'
                    : '' || scope.row.state == 4
                    ? 'warning'
                    : ''
                ">
            <div>
              <span v-show="scope.row.state==0">新创建</span>
              <span v-show="scope.row.state==1">已送审</span>
              <span v-show="scope.row.state==2">已回收</span>
              <span v-show="scope.row.state==3">已审核</span>
              <span v-show="scope.row.state==4">反审核</span>
            </div>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="docNum" label="单据号"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="docDate" label="日期"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="rechargeTypeNum" label="充值编码"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="rechargeTypeName" label="充值名称"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="80" prop="rechargeMoney" label="金额"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="80" prop="discount" label="折扣"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="expireDate" sortable label="到期时间"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="80" prop="quantity" sortable label="份数"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="80" prop="remakr" sortable label="单位"></el-table-column>

      <el-table-column align="center" prop="inputBy" :show-overflow-tooltip="true" label="创建人" min-width="80"></el-table-column>
      <el-table-column align="center" prop="inputDate" :show-overflow-tooltip="true" label="创建时间" min-width="100"></el-table-column>
      <el-table-column align="center" prop="updateBy" :show-overflow-tooltip="true" label="修改人" min-width="80"></el-table-column>
      <el-table-column align="center" prop="updateDate" :show-overflow-tooltip="true" label="修改时间" min-width="100"></el-table-column>
      <el-table-column align="center" prop="auditBy" :show-overflow-tooltip="true" label="审核人" min-width="80"></el-table-column>
      <el-table-column align="center" prop="auditDate" :show-overflow-tooltip="true" label="审核时间" min-width="100"></el-table-column>
    </el-table>
    <div class="block margin-top-xs">
      <!-- 分页组件 -->
      <el-pagination align="left" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryForm.pageNum" :page-sizes="[10,20,30,40,50]" :page-size="queryForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
    <edit ref="edit" @editData="editData"></edit>
    <detail ref="detail" @detailData="detailData"></detail>
    <el-dialog title="导入表格" :visible.sync="infoDialog" :close-on-click-modal="false">
      <excel @fileData="fileData" @infoDialogV="infoDialogV"></excel>
    </el-dialog>
  </div>
</template>

<script>
import edit from "./components/edit.vue";
import detail from "./components/detail.vue";
import excel from "@/components/excel";

import {
  stateBaseGrouponCard,
  baseGrouponCardPage,
  importBaseGrouponList,
  downloadBaseGrouponList,
  delBaseGrouponCard,
} from "@/api/promotion.js";
export default {
  components: { edit, detail, excel },
  data() {
    return {
      form: {}, //主对象
      listLoading: false, //list加载提示开启动画
      elementLoadingText: "加载中...", //list加载提示文字
      list: [], //主数组
      queryForm: {
        //查询对象
        pageNum: 1,
        pageSziz: 10,
      },
      total: 0, //分页总条数
      radioRowValue: null, //选择列表值
      radioRowValueList: {}, //选择列表对象
      infoDialog: false, //上传文件弹窗开关
    };
  },
  methods: {
    //上传文件方法
    fileData(v) {
      var fileData = new FormData();
      if (v == null) {
        this.$alert("请选择需要上传的文件", "提示", {
          confirmButtonText: "确定",
          confirmButtonClass: "confirmbtnFalses",
          type: "warning",
          center: true,
          callback: (action) => {},
        });
      } else {
        fileData.append("multipartFile", v);
        fileData.append("grouponCardId", this.radioRowValueList.id);
        importBaseGrouponList(fileData).then((res) => {
          if (res.rows != undefined) {
            this.infoList = res.rows;
            this.infoErrorDialog = true;
          } else {
            if (res.code == "000000") {
              this.$message.success({
                message: res.message,
              });
              this.getData();
            } else {
              this.$alert(res.message, "提示", {
                confirmButtonText: "确定",
                confirmButtonClass: "confirmbtnFalses",
                type: "warning",
                center: true,
                callback: (action) => {},
              });
            }
          }
        });
      }
    },
    //返回上传文件弹窗状态
    infoDialogV(v) {
      this.infoDialog = v;
    },
    //下载模板方法
    download() {
      downloadBaseGrouponList().then((res) => {
        let blob = new Blob([res]);
        console.log(blob);
        var a = document.createElement("a");
        var url = window.URL.createObjectURL(blob);
        console.log(url);
        a.href = url;
        a.download = "团购名单.xlsx";
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },

    //导出方法
    exit() {
      if (this.searchForm.projectTypeName == "") {
        delete this.searchForm.projectTypeName;
      }
      proExport(this.searchForm).then((res) => {
        let blob = new Blob([res]);
        var a = document.createElement("a");
        var url = window.URL.createObjectURL(blob);
        console.log(url);
        a.href = url;
        a.download = "项目导出表.xlsx";
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    //点击选中
    rowClick(row) {
      this.radioRowValue = row.id;
      this.radioRowValueList = row;
    },

    edit(number) {
      if (number == -1) {
        this.$refs.edit.show("添加");
      } else if (number == 0) {
        this.$refs.edit.show(this.radioRowValueList);
      } else {
        let form = { id: this.radioRowValueList.id, state: number };
        stateBaseGrouponCard(form).then((res) => {
          if (res.code == "000000") {
            this.radioRowValueList.state = number;
            this.$message.success({
              message: "操作成功!",
            });
            this.getData();
          } else {
            this.$alert(res.message, "提示", {
              confirmButtonText: "确定",
              confirmButtonClass: "confirmbtnFalses",
              type: "warning",
              center: true,
              callback: (action) => {},
            });
          }
        });
      }
    },
    //明细方法
    detail() {
      this.$refs.detail.show(this.radioRowValueList, this.queryForm);
    },
    //修改返回方法
    editData(v) {
      this.getData();
    },
    //明细返回方法
    detailData(v) {
      this.getData();
    },
    //列表数组双击方法
    rowdbClick(row) {
      this.$refs.detail.show(row);
    },
    del() {
      var id = {
        id: this.radioRowValueList.id,
      };
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        cancelButtonClass: "cancelbtnFalses",
        confirmButtonClass: "confirmbtnFalses",
      })
        .then(() => {
          delBaseGrouponCard(id).then((res) => {
            if (res.code == "000000") {
              this.$message.success({
                message: "  删除成功!",
              });
              this.getData();
            } else {
              this.$alert(res.message, "提示", {
                confirmButtonText: "确定",
                confirmButtonClass: "confirmbtnFalses",
                type: "warning",
                center: true,
                callback: (action) => {},
              });
            }
          });
        })
        .catch(() => {
          this.$message.info({
            message: "已取消操作！",
          });
        });
    },
    //分页方法
    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.getData();
      //console.log(val);
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val;
      this.getData();
      //console.log(val);
    },
    //获取数据
    async getData() {
      this.listLoading = true;
      baseGrouponCardPage(this.queryForm).then((res) => {
        this.listLoading = false;
        if (res.code == "000000") {
          this.list = res.pageInfo.list;
          this.total = res.pageInfo.total;
          this.list=this.list.reverse()
          console.log(this.list)
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style>
</style>
