<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div v-if="topList[1].changed == true">
      <div class="thetopbox">
        <el-form ref="queryParams" :model="queryParams" label-width="100px">
          <el-row>
            <el-col :span="4">
              <el-form-item :label="'会员编号'" prop="name">
                <el-input clearable v-model="queryParams.memberCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'会员姓名'" prop="shortName">
                <el-input clearable v-model="queryParams.memberName"></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="['ModifyDirectpushUpdate']"
            >{{ '修改' }}{{ '直推' }}</el-button
          >
          <el-button
            size="small"
            v-hasButtons="['ModifyDirectpushExport']"
            @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="parMemberCode"
              :label="'变更前推荐编号'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="parMemberName"
              :label="'变更前推荐姓名'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="editNewPlaceMemberCode"
              :label="'变更后推荐编号'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="editNewPlaceMemberName"
              :label="'变更后推荐姓名'"
            >
            </el-table-column>

            <el-table-column
              align="center"
              prop="editNumber"
              :label="'变更次数'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="approveStatusVal"
              :label="'审核状态'"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="editDate"
              :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="parMemberCode"
            disabled="disabled"
            :label="'变更前推荐编号'"
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.parMemberCode" disabled="disabled">
              </el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="parMemberName"
            disabled="disabled"
            :label="'变更前推荐姓名'"
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.parMemberName" disabled="disabled">
              </el-input>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="editNewPlaceMemberCode"
            :label="'变更后推荐编号'"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.editNewPlaceMemberCode"
                @blur="checkCode(scope.row)"
              >
              </el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="editNewPlaceMemberName"
            :label="'变更后推荐姓名'"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.editNewPlaceMemberName"
                disabled="disabled"
              >
              </el-input>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="pkAccountKeyVal"
            :label="'扣款币种'"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="cost"
            :label="'单笔扣款金额' + '(¥)'"
          ></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
              v-for="(item, index) in processTypeList"
              :key="index"
              :class="`thebtn${item.value}`"
              @click="saveDate(item.value)"
              >{{ item.label }}</el-button
            > -->
            <el-button class="thebtn0" @click="saveDate(0)">
              {{ '取消' }}</el-button
            >
            <el-button
              v-hasButtons="['ModifyDirectpushExemptSign']"
              class="thebtn1"
              @click="saveDate(1)"
              >{{ '免签' }}</el-button
            >
            <el-button
              v-hasButtons="['ModifyDirectpushExemptPay']"
              class="thebtn2"
              @click="saveDate(2)"
              >{{ '免付' }}</el-button
            >
            <el-button
              v-hasButtons="['ModifyDirectpushExemptAll']"
              class="thebtn3"
              @click="saveDate(3)"
              >{{ '全免' }}</el-button
            >
            <el-button
              v-hasButtons="['ModifyDirectpushConfirm']"
              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,
  specialBusinessQuery,
} from "@/api/business.js";
import { getToken } from "@/utils/auth";
import { getRouters } from "@/api/settle";
import { getMemberName } from "@/api/financial";
import { gettheMemberInfo } from "@/api/affiliatedteam";
export default {
  name: "modifyDirectpush",
  components: { topBar },
  data() {
    return {
      loading:false,
      moren: "modifyDirectpush",
      topList: [
        {
          name: '空单注册',
          path: "emptyOrder",
          changed: false,
        },
        {
          name: '修改直推',
          path: "modifyDirectpush",
          changed: false,
        },
        {
          name: '修改安置',
          path: "modifyPlacement",
          changed: false,
        },
        {
          name: '注水业绩',
          path: "waterPv",
          changed: false,
        },
        {
          name: '修改编号',
          path: "modifyCode",
          changed: false,
        },
        {
          name: '真实奖衔控制',
          path: "awardControl",
          changed: false,
        },
        {
          name: '修改结算国家',
          path: "modifySettleCountry",
          changed: false,
        },
        {
          name: '政策币业务',
          path: "policyMoney",
          changed: false,
        },
        {
          name: '分红配置',
          path: "bonusConfig",
          changed: false,
        },
      ],
      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/manage/update-parent/import-service-log",
      },
      codeData: [],
      form: {
        approvalBusiness: 111,
        memberCode: "",
        memberName: "",
        czlx: 1,
      },
      czlx: 1,
    };
  },
  created() {
    this.getSearch();
    this.getPubEnumsProcessTyepe();
    this.getUserRoute();
  },
  methods: {
    getDataList() {
      //根据选中的会员查询会员业绩
      specialBusinessQuery(this.form).then((res) => {
        this.codeData = [];
        this.codeData.push(res.data);
      });
    },
    reset() {
      this.form.memberCode = "";
      this.form.memberName = "";
      this.codeData = [];
      // this.getDataList();
    },
    //根据会员编号查询会员姓名
    checkName() {
      getMemberName({ memberCode: this.form.memberCode }).then((res) => {
        this.form.memberName = res.memberName;
      });
    },
    // 根据会员编号查询会员姓名
    checkCode(data) {
      gettheMemberInfo(data.editNewPlaceMemberCode).then((res) => {
        // data.editNewPlaceMemberName = res.data.memberName;
        // this.$forceUpdate();

        if (res.data) {
          // data.editNewPlaceMemberName = res.data.memberName;
          this.$set(
            this.codeData[0],
            "editNewPlaceMemberName",
            res.data.memberName
          );
          this.$forceUpdate();
        } else {
          data.editNewPlaceMemberName = "";
          this.$forceUpdate();
        }
      });
    },
    getUserRoute() {
      getRouters().then((res) => {
        res.data.forEach((item) => {
          this.topList.forEach((items) => {
            if (item.path == items.url) {
              items.changed = true;
            }
          });
        });
      });
    },
    //下载模板
    importTemplate() {
      this.download(
        "/member/manage/update-parent/export",
        {},
        `${'模板'}${new Date().getTime()}.xlsx`
      );
    },
    handleDelete(row, index, rows) {
      this.$confirm('是否删除').then((_) => {
        rows.splice(index, 1);
      });
    },
    saveDate(index) {
      if (index == 0) {
        this.updateShow = false;
      } else {
        spe
          .checkUpdateParent({
            updateParentParamList: this.codeData,
            processType: index,
          })
          .then((res) => {
            if (res.code == 200) {
              if (index == 2 || index == 4) {
                this.codeData.forEach((item) => {
                  item.newValue = item.editNewPlaceMemberCode;
                  item.oldValue = item.parMemberCode;
                });
                this.$router.push({
                  path: "specialZxqc",
                  query: {
                    data: JSON.stringify(this.codeData),
                    index: index,
                    businessType: 111,
                  },
                });
              } else {
                spe
                  .updateParent({
                    updateParentParamList: this.codeData,
                    processType: index,
                  })
                  .then((res) => {
                    if (res.code == 200) {
                      this.$message({
                        message: res.msg,
                        type: "success",
                      });
                      this.updateShow = false;
                      this.getSearch();
                    }
                  });
              }
            }
          });
      }
    },
    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.getUpdateParentList(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/manage/update-parent/export-list",
          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>
