<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'订单日期'" prop="payTime">
              <el-date-picker
                v-model="queryParams.payTime"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="'请选择'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button
                class="my_search"
                @click="
                  () => {
                    getSearch();
                    getCzNum();
                  }
                "
              >
                {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi">
                {{ '重置' }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="small" class="thebtn1" @click="addData">
            {{ '添加' }}</el-button
          >
          <el-button size="small" class="thebtn2" @click="handleExport">
            {{ '导出' }}</el-button
          >
          <el-button
            size="small"
            :disabled="multiple"
            class="thebtn3"
            @click="deleteData"
          >
            {{ '删除' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
        v-loading="loading"
          :data="tableData"
          height="720px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="colsele" type="selection" width="55">
          </el-table-column>
          <el-table-column
            align="center"
            prop="vertexCode"
            :label="'体系编号'"
            min-width="100px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="vertexName"
            :label="'体系名称'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="firstPurchaseMo"
            min-width="120px"
            :label="'首购新增金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="firstPurchasePv"
            min-width="120px"
            :label="'首购新增业绩' + `(PV)`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseOrderMo"
            min-width="120px"
            :label="'复购专区新增金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseOrderPv"
            min-width="120px"
            :label="'复购专区新增业绩' + `(PV)`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseMo"
            min-width="120px"
            :label="'复购新增金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchasePv"
            :label="'复购新增业绩' + `(PV)`"
          >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="thirdOrderSumMo"
            min-width="120px"
            :label="'三方新增金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="thirdOrderSumPv"
            :label="'三方新增业绩' + `(PV)`"
          >
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="firstPurchaseSumMo"
            min-width="120px"
            :label="'首购累计金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="firstPurchaseSumPv"
            :label="'首购累计业绩' + `(PV)`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseOrderSumMo"
            min-width="120px"
            :label="'复购专区累计金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseOrderSumPv"
            :label="'复购专区累计业绩' + `(PV)`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseSumMo"
            min-width="120px"
            :label="'复购累计金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="repurchaseSumPv"
            :label="'复购累计业绩' + `(PV)`"
          >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="thirdOrderSumMo"
            min-width="120px"
            :label="'三方累计金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="thirdOrderSumPv"
            :label="'三方累计业绩' + `(PV)`"
          >
          </el-table-column> -->

          <el-table-column
            align="center"
            prop="payTime"
            min-width="120px"
            :label="'订单日期'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
            min-width="120px"
          >
            <template slot-scope="scope">
              <el-button
                class="scopebtn"
                @click.native.prevent="updateData(scope.row)"
                type="text"
                size="small"
                style="color: #ffad41"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                class="scopebtn"
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
                style="color: #c73030"
              >
                {{ '删除' }}
              </el-button>
            </template>
          </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="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'体系编号'" prop="vertexCode">
              <el-input
                clearable
                v-model="form.vertexCode"
                :placeholder="'请输入'"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'体系名称'" prop="vertexName">
              <el-input
                clearable
                v-model="form.vertexName"
                :placeholder="'请输入'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="flex_div">
          <div class="flexs1 flex_left">
            <div class="left_top">
              <div>
                <el-form-item :label="'会员编号'" prop="vertexCode">
                  <el-input
                    clearable
                    v-model="form.addmemberCode"
                    :placeholder="'请输入'"
                  ></el-input>
                </el-form-item>
                <el-form-item :label="'会员姓名'" prop="vertexName">
                  <el-input
                    clearable
                    v-model="form.addmemberName"
                    disabled
                    :placeholder="'请输入'"
                  ></el-input>
                </el-form-item>
              </div>
              <div style="margin-left: 20px">
                <el-button
                  @click.native.prevent="addLeftTag"
                  type="text"
                  size="small"
                  style="background: #c8161d; color: #ffffff"
                >
                  {{ '添加' }}
                </el-button>
              </div>
            </div>
            <div class="flex_main">
              <el-tag
                v-for="(aitem, aindex) in addList"
                :key="aindex"
                style="margin-right: 10px; margin-bottom: 15px"
                effect="plain"
                closable
                @close="closeLeftTag(aitem)"
              >
                {{ aitem.memberCode }}
              </el-tag>
            </div>
            <div class="atext">加区</div>
          </div>

          <div class="flexs1">
            <div class="left_top">
              <div>
                <el-form-item :label="'会员编号'" prop="vertexCode">
                  <el-input
                    clearable
                    v-model="form.popmemberCode"
                    :placeholder="'请输入'"
                  ></el-input>
                </el-form-item>
                <el-form-item :label="'会员姓名'" prop="vertexName">
                  <el-input
                    clearable
                    v-model="form.popmemberName"
                    disabled
                    :placeholder="'请输入'"
                  ></el-input>
                </el-form-item>
              </div>
              <div style="margin-left: 20px">
                <el-button
                  @click.native.prevent="addRightTag()"
                  type="text"
                  size="small"
                  style="background: #c8161d; color: #ffffff"
                >
                  {{ '添加' }}
                </el-button>
              </div>
            </div>
            <div class="flex_main">
              <el-tag
                v-for="(aitem, aindex) in popList"
                :key="aindex"
                style="margin-right: 10px; margin-bottom: 15px"
                effect="plain"
                closable
                @close="closeRightTag(aitem)"
              >
                {{ aitem.memberCode }}
              </el-tag>
            </div>
            <div class="atext">减区</div>
          </div>
        </div>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
        
  <script>
import topBar from "@/components/topBar";
import * as sta from "@/api/statistics";
import { gettheMemberInfo } from "@/api/affiliatedteam";
import { isLocals, isLocalSymbol } from "@/utils/numberToCurrency";
export default {
  name: "LcDataReportList",
  components: {
    topBar,
  },
  filters: {
    numFilter(value) {
      // 截取当前数据到小数点后两位
      let realVal = parseFloat(value).toFixed(2);
      return realVal;
    },
  },
  data() {
    return {
      moren: "lcDataReport",
      topList: [
        {
          name: '联创',
          path: "lcDataReport",
        },
        {
          name: '国内业绩查询',
          path: "chPvInquiry",
        },
        {
          name: '海外业绩汇总',
          path: "othPvInquiry",
        },
        {
            name: "新联创",
            path: "newLc",
          },
      ],
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      total: 0,
      multipleSelection: [],
      multiple: true,
      form: {},
      rules: {},
      dialogVisible: false,
      dialogTitle: "",
      addList: [],
      popList: [],
      loading: false
    };
  },
  created() {
    // this.getSearch();
  },
  methods: {
    isLocalSymbol,
    isLocals,

    // 加区添加
    addLeftTag() {
      gettheMemberInfo(this.form.addmemberCode).then((res) => {
        this.form.addmemberName = res.data.memberName;
        if (res.code == 200) {
          const tag1 = { memberCode: "" };
          tag1.memberCode = this.form.addmemberCode;
          this.addList.push(tag1);
        }
      });
    },
    //减区添加
    addRightTag() {
      gettheMemberInfo(this.form.popmemberCode).then((res) => {
        this.form.popmemberName = res.data.memberName;
        if (res.code == 200) {
          const tag2 = { memberCode: "" };
          tag2.memberCode = this.form.popmemberCode;
          this.popList.push(tag2);
        }
      });
    },
    //加区减掉tag
    closeLeftTag(row) {
      for (let i = this.addList.length - 1; i >= 0; i--) {
        if (this.addList[i].memberCode === row.memberCode) {
          this.addList.splice(i, 1);
        }
      }
    },
    //减区减掉tag
    closeRightTag(row) {
      for (let i = this.popList.length - 1; i >= 0; i--) {
        if (this.popList[i].memberCode === row.memberCode) {
          this.popList.splice(i, 1);
        }
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.addList = []
      this.popList = []
      this.form.addmemberCode = ''
      this.form.addmemberName = ''
      this.form.popmemberCode = ''
      this.form.popmemberName = ''
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    //单条删除
    deleteRow(row) {
      this.$confirm('是否确认操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        sta
          .deleteLcDataList({
            pkIdList: [row.pkId],
          })
          .then((res) => {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getSearch();
            this.getCzNum();
          });
      });
    },
    //添加
    addData() {
      this.form.pkId = null;
      // this.$refs["form"].clearValidate();
      // this.$refs["form"].resetFields();
      this.dialogTitle = '添加';
      this.dialogVisible = true;
     
    },
    //修改
    updateData(row) {
      this.dialogTitle = '修改';
      this.dialogVisible = true;

      sta.getOneLcData({ pkId: row.pkId }).then((res) => {
        this.form = res.data;
        // 去除末尾的逗号，并将字符串按逗号分割成数组
        const regionArray = res.data.addRegion.replace(/,$/, "").split(",");
        // 将数组中的每个元素转换为对象格式
        const newRegion = regionArray.map((code) => ({ memberCode: code }));
        this.addList = newRegion;

        const regionArray2 = res.data.reduceRegion.replace(/,$/, "").split(",");
        const newRegion2 = regionArray2.map((code) => ({ memberCode: code }));
        this.popList = newRegion2;
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 使用数组的map方法提取每个对象中的memberCode属性值，并将其组合成一个新的数组
          const memberCodes = this.addList.map((obj) => obj.memberCode);
          // 使用数组的join方法将数组中的元素用逗号连接成一个字符串
          this.form.addRegion = memberCodes.join(",");

          const memberCodes2 = this.popList.map((obj) => obj.memberCode);
          this.form.reduceRegion = memberCodes2.join(",");

          let url = "";
          if (this.form.pkId) {
            //修改
            url = sta.updateLcDataStat;
          } else {
            //添加
            url = sta.saveLcDataStat;
          }

          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getSearch();
              this.getCzNum();
              this.handleClose();
             
            }
          });
        }
      });
    },
    //导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/report/manager/cu-lianchuang-data-stat/export",
          { ...this.queryParams },
          `${'联创'}${new Date().getTime()}.xlsx`
        );
      });
    },

    deleteData() {
      this.$confirm('是否确认操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        let thepkIds = [];
        thepkIds = this.multipleSelection.map((item) => item.pkId);
        sta
          .deleteLcDataList({
            pkIdList: thepkIds,
          })
          .then((res) => {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getSearch();
            this.getCzNum();
          });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.multiple = !val.length;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
      };
      this.getSearch();
      this.getCzNum();
    },
    getSearch() {
      this.loading = true
      sta.getLianchuangDataList(this.queryParams).then((res) => {
        this.loading = false
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
        }
      });
    },
    getCzNum() {
      this.queryParams.pageNum = 1;
    },
  },
};
</script>
        
 <style lang="scss" scoped>
::v-deep .scopebtn {
  padding: 0;
}
::v-deep .el-image-viewer__close {
  right: 500px;
}
::v-deep .dizhi .el-cascader {
  width: 100%;
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 15px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .searchtitle {
      margin-right: 10px;
    }
    .searchbtn {
      background: #08143f;
      color: #ffffff;
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
          width: 68px;
          height: 32px;
        }
        .thebtn1 {
          color: #ffffff;
          background: #48b2fd;
          width: 68px;
          height: 32px;
        }
        .thebtn3 {
          color: #ffffff;
          background: #c8161d;
          width: 68px;
          height: 32px;
        }
      }
    }
  }
  .flex_div {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    border-top: 4px solid rgba(0, 0, 0, 0.1);
    .flex_left {
      border-right: 4px solid rgba(0, 0, 0, 0.1);
    }
    .flexs1 {
      flex: 1;
      padding: 10px;

      .left_top {
        display: flex;
        align-items: center;
      }
    }
  }
  .flex_main {
    border: 1px solid #cccccc;
    padding: 10px;
    border-radius: 10px 10px 10px 10px;
    min-height: 300px;
    margin: 10px 0;
  }
  .atext {
    text-align: center;
    font-size: 18px;
    font-family: PingFang SC-Semibold, PingFang SC;
    font-weight: 600;
    color: #000000;
  }
}
::v-deep .el-date-editor--date {
  width: 100%;
}
</style>
        