<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div>
      <div class="thetopbox">
        <el-form ref="queryParams" :model="queryParams" label-width="100px">
          <el-row>
            <el-col :span="4">
              <el-form-item :label="'会员编号'" prop="memberCode">
                <el-input clearable v-model="queryParams.memberCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'会员姓名'" prop="memberName">
                <el-input clearable v-model="queryParams.memberName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'联系方式'" prop="phone">
                <el-input clearable v-model="queryParams.phone"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4" style="margin-left: 20px">
              <div class="searchbox">
                <el-button class="searchbtn" @click="getSearch()">
                  {{ '搜索' }}</el-button
                >
                <el-button @click="reChongzhi"> {{ '重置' }}</el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="maintop">
        <div class="mainbtn1">
          <el-button
            size="small"
            class="thebtn1"
            @click="updateCode()"
            v-hasButtons="['MemberRenewalAdd']"
            >会员续约</el-button
          >
          <el-button
            size="small"
            v-hasButtons="['MemberRenewalExport']"
            @click="handleExport"
            class="thebtn2"
          >
            {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="main">
        <div class="maintable">
          <el-table
            :data="tableData"
            height="750px"
            v-loading="loading"
            style="width: 100%"
            :header-cell-style="{ background: '#EEEEEE' }"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>

            <el-table-column
              align="center"
              prop="memberCode"
              :label="'会员编号'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="memberName"
              :label="'会员姓名'"
            >
            </el-table-column>

            <el-table-column align="center" prop="phone" :label="'联系方式'">
            </el-table-column>
            <el-table-column
              align="center"
              prop="expireDateOld"
              label="变更前有效期"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="stopDateOld"
              label="变更前休止期"
            >
            </el-table-column>
            <el-table-column align="center" prop="years" label="续约年限（年）">
            </el-table-column>

            <el-table-column
              align="center"
              prop="approveStatusVal"
              :label="'审核状态'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="modifiedTime"
              :label="'变更日期'"
            >
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getSearch"
      />
      <el-dialog
        title="会员续约"
        :visible.sync="updateShow"
        width="80%"
        :close-on-click-modal="false"
        center
        class="updateClass"
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item :label="'操作类型'">
            <el-select
              clearable
              :placeholder="'请选择'"
              v-model="form.czlx"
            >
              <el-option
                v-for="item in cztypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-row style="margin-top: 10px">
            <el-col :span="5">
              <el-form-item :label="'会员编号'">
                <el-input
                  clearable
                  style="width: 200px"
                  v-model="form.memberCode"
                  @blur="checkName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item :label="'会员姓名'">
                <el-input
                  clearable
                  style="width: 200px"
                  v-model="form.memberName"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button
                @click="getDataList"
                style="background-color: #c8161d; color: #fff"
              >
                {{ '搜索' }}</el-button
              >
              <el-button @click="reset"> {{ '重置' }}</el-button>
            </el-col>
          </el-row>
        </el-form>
        <template v-if="form.czlx == 2">
          <div class="mainbtn">
            <el-button
              size="small"
              class="thebtn2"
              style="background-color: #ff6723; color: #fff"
              @click="handleImport"
            >
              {{ '导入' }}</el-button
            >
            <el-button
              size="small"
              @click="importTemplate"
              style="background-color: #009b58; color: #fff; margin-left: 15px"
              class="thebtn2"
            >
              {{ '下载模板' }}</el-button
            >
          </div>
        </template>
        <el-table
          :data="codeData"
          height="400px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          border
        >
          <el-table-column
            align="center"
            prop="memberCode"
            :label="'会员编号'"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="memberName"
            :label="'会员姓名'"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="phone"
            :label="'联系方式'"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="expireDateOld"
            label="变更前有效期"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="stopDateOld"
            label="变更前休止期"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="editNewPlaceMemberName"
            label="续约年限（年）"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.years"
                disabled="disabled"
                type="number"
                min="0"
              >
              </el-input>
            </template>
          </el-table-column>

          <el-table-column align="center" prop="" :label="'操作'">
            <template slot-scope="scope">
              <el-button
                @click="handleDelete(scope.row, scope.$index, codeData)"
                type="text"
                size="small"
                style="color: #c73030"
              >
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer">
          <div style="margin: 0 auto">
            <el-button class="thebtn0" @click="saveDate(0)">
              {{ '取消' }}</el-button
            >
            <el-button
              v-hasButtons="['MemberRenewalExemptSign']"
              class="thebtn1"
              @click="saveDate(1)"
              >{{ '免签' }}</el-button
            >
            <!-- <el-button
              v-hasButtons="['MemberRenewalExemptPay']"
              class="thebtn2"
              @click="saveDate(2)"
              >{{ '免付' }}</el-button
            >
            <el-button
              v-hasButtons="['MemberRenewalExemptAll']"
              class="thebtn3"
              @click="saveDate(3)"
              >{{ '全免' }}</el-button
            > -->
            <el-button
              v-hasButtons="['MemberRenewalConfirm']"
              class="thebtn4"
              @click="saveDate(4)"
            >
              {{ '确认' }}</el-button
            >
          </div>
        </div>
      </el-dialog>
      <el-dialog
        :title="upload.title"
        :close-on-click-modal="false"
        :visible.sync="upload.open"
        width="600px"
        append-to-body
      >
        <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-exceed="handleExceed"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            {{ '将文件拖到此处，或' }}
            <em>{{ '点击上传' }}</em>
          </div>
          <!--          <div class="el-upload__tip" slot="tip">-->
          <!--            <el-link type="info" style="font-size: 12px" @click="importTemplate"-->
          <!--            > {{ '下载模板' }}</el-link-->
          <!--            >-->
          <!--          </div>-->
          <div class="el-upload__tip" style="color: red" slot="tip">
            {{ '提示：仅允许导入“xls”或“xlsx”格式文件！' }}
          </div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">{{
            '确认'
          }}</el-button>
          <el-button @click="upload.open = false">{{
            '取消'
          }}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
  
  <script>
import topBar from "@/components/topBar";
import * as spe from "@/api/specialBusiness";
import { getPubEnumsProcessTyepe } from "@/api/business.js";
import { getToken } from "@/utils/auth";
import { getMemberName } from "@/api/financial";
import * as man from "@/api/manage.js";
export default {
  name: "MemberRenewal",
  components: { topBar },
  data() {
    return {
      loading: false,
      moren: "memberRenewal",
      topList: [
        {
          name: "会员续约",
          path: "memberRenewal",
        },
      ],
      cztypeList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        memberName: "",
        memberCode: "",
      },
      total: 0,
      tableData: [],
      updateShow: false,
      processTypeList: [],
      memberChargingDetailList: [],
      upload: {
        // 是否显示弹出层（物流导入）
        open: false,
        // 弹出层标题（物流导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的物流数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/member/manager/updateExpireStop/importData",
      },
      codeData: [],
      form: {
        approvalBusiness: 136,
        memberCode: "",
        memberName: "",
        czlx: 1,
      },
      czlx: 1,
    };
  },
  created() {
    this.getSearch();
    this.getPubEnumsProcessTyepe();
  },
  methods: {
    getDataList() {
      //根据选中的会员查询会员业绩
      spe.getSpecialBusinessQuery(this.form).then((res) => {
        this.codeData = [];
        this.codeData.push(res.data);
      });
    },
    reset() {
      this.form.memberCode = "";
      this.form.memberName = "";
      this.codeData = [];
    },
    //根据会员编号查询会员姓名
    checkName() {
      getMemberName({ memberCode: this.form.memberCode }).then((res) => {
        this.form.memberName = res.memberName;
      });
    },

    //下载模板
    importTemplate() {
      this.download(
        "/member/manager/updateExpireStop/download",
        {},
        `${'模板'}${new Date().getTime()}.xlsx`
      );
    },
    handleDelete(row, index, rows) {
      this.$confirm('是否删除').then((_) => {
        rows.splice(index, 1);
      });
    },
    saveDate(index) {
      if (index == 0) {
        this.updateShow = false;
      } else if (index == 1) {
        man.submitMemberRenewal({uesList: this.codeData,processType:1} ).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.updateShow = false;
            this.getSearch();
          }
        });
      } else {
        this.$router.push({
          path: "specialZxqc",
          query: {
            data: JSON.stringify(this.codeData),
            index: index,
            businessType: 136,
          },
        });
      }
    },
    updateCode() {
      if (this.memberChargingDetailList.length > 0) {
        spe
          .getMemberChargingDetail({
            memberChargingDetailList: this.memberChargingDetailList,
          })
          .then((res) => {
            this.codeData = res.data;
          });
      }

      this.updateShow = true;
    },
    getPubEnumsProcessTyepe() {
      getPubEnumsProcessTyepe().then((res) => {
        this.processTypeList = res.data;
      });
      spe.getEmusBatchType().then((res) => {
        this.cztypeList = res.data;
      });
    },
    //列表多选
    handleSelectionChange(selection) {
      this.memberChargingDetailList = selection;
    },
    getSearch() {
      this.loading = true;
      spe.getmmeberRenewalList(this.queryParams).then((res) => {
        this.loading = false;
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        memberName: "",
        memberCode: "",
      };
      this.getSearch();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '修改直推';
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      var fileName = response.fileName;
      if (fileName != undefined && fileName != "") {
      }
      if (response.code == 200) {
        this.codeData = response.data;
      }

      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, '导入结果', {
        dangerouslyUseHTMLString: true,
      });
      // this.getList();
    },
    // 上传文件数量超出后的动作
    handleExceed() {
      this.upload.isUploading = false;
      this.msgInfo('仅可上传一个文件哦');
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/member/manager/updateExpireStop/export",
          Object.assign({}, this.queryParams, this.select),
          `会员续约${new Date().getTime()}.xlsx`
        );
      });
    },
  },
};
</script>
  
  <style lang="scss" scoped>
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.updateClass {
  .footer {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 15px 0;

    .thebtn0 {
      width: 120px;
      height: 38px;
    }
    .thebtn1 {
      width: 120px;
      height: 38px;
      background: rgba(0, 155, 88, 1);
      color: #ffffff;
      border: none;
    }
    .thebtn2 {
      width: 120px;
      height: 38px;
      background: rgba(243, 169, 0, 1);
      border: none;
      color: #ffffff;
    }
    .thebtn3 {
      width: 120px;
      height: 38px;
      border: none;
      color: #ffffff;
      background: rgba(228, 27, 27, 1);
    }
    .thebtn4 {
      width: 120px;
      height: 38px;
      border: none;
      color: #ffffff;
      background: rgba(72, 178, 253, 1);
    }
    .thebtn {
      width: 120px;
      height: 38px;
      border: none;
      color: #ffffff;
      background: rgba(204, 204, 204, 1);
    }
  }
}
::v-deep .el-form-item {
  margin-bottom: 10px;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding-top: 20px;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #c8161d;
        color: #ffffff;
      }
    }
  }
  .mainbtn {
    margin-bottom: 20px;
    .thebtn1 {
      background: rgba(255, 103, 35, 1);
      color: #ffffff;
    }
  }
  .maintop {
    display: flex;
    padding: 0px 0;
    justify-content: space-between;
    align-items: center;
    .mainbtn1 {
      .thebtn1 {
        background: rgba(72, 178, 253, 1);
        color: #ffffff;
      }
      .thebtn2 {
        background: #ffad41;
        color: #ffffff;
      }
    }
  }
  .main {
    // margin-top: 20px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
  }
}
</style>
  