<template>
  <div class="page">
    <topBar v-if="topList.length > 0" :top-list="topList" :moren="moren" />
    <div class="thetopbox">
      <el-form label-width="auto">
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item :label="'团队编号'" prop="vertexName">
              <el-input
                v-model="queryParams.teamCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'团队名称'" prop="teamName">
              <el-input
                v-model="queryParams.teamName"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input
                v-model="queryParams.memberCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员姓名'" prop="memberName">
              <el-input
                v-model="queryParams.memberName"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <!--        <el-col :span="4">-->
          <!--          <el-form-item :label="'团队编码'" prop="teamCode">-->
          <!--            <el-input v-model="queryParams.teamCode" clearable placeholder="" />-->
          <!--          </el-form-item>-->
          <!--        </el-col>-->
          <el-col :span="4">
            <div class="searchbox" style="margin-left: 40px">
              <el-button class="my_search" @click="getTeamList">
                {{ '搜索' }}</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
            v-hasButtons="['AffiliatedteamAdd']"
            size="small"
            class="thebtn1"
            @click="adddate()"
            >{{ '添加' }}</el-button
          >
          <el-button
            v-hasButtons="['AffiliatedteamExport']"
            size="small"
            class="thebtn2"
          >
            {{ '导出' }}</el-button
          >
          <el-button
            v-hasButtons="['AffiliatedteamUpAll']"
            size="small"
            class="thebtn3"
            @click="addflushAll"
          >
            更新全部</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="670px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column
            align="center"
            prop="jxmc"
            :label="'国家'"
            width="180"
          >
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.pkCountryIcon" alt="" />
                <div>{{ scope.row.pkCountryName }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="teamCode"
            :label="'团队编号'"
            width="180"
          />
          <el-table-column
            align="center"
            prop="vertexName"
            :label="'体系名称'"
          />
          <el-table-column
            align="center"
            prop="teamName"
            :label="'团队名称'"
          />
          <el-table-column
            align="center"
            prop="memberCode"
            :label="'会员编号'"
          />
          <el-table-column
            align="center"
            prop="memberName"
            :label="'会员姓名'"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                style="color: #6962f6"
                class="button-s"
                type="text"
                size="small"
                @click.native.prevent="update(scope.row, tableData)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                class="button-s"
                type="text"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row, tableData)"
              >
                {{ '删除' }}
              </el-button>
              <el-button
                class="button-s"
                type="text"
                size="small"
                style="color: #48b2fd"
                @click.native.prevent="updateTeam(scope.row, tableData)"
              >
                {{ '更新团队' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getTeamList"
        />
      </div>
    </div>

    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        style="padding-bottom: 80px"
      >
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10" style="margin-left: -57px">
            <el-form-item :label="'团队编码'" prop="teamCode">
              <el-input
                v-model="form.teamCode"
                clearable
                :placeholder="'请输入团队编码'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item :label="'体系名称'" prop="pkBdVertex">
              <el-select
                v-model="form.pkBdVertex"
                clearable
                :placeholder="'请输入体系名称'"
              >
                <el-option
                  v-for="item in vertexList"
                  :key="item.pkId"
                  :label="item.vertexName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10" style="margin-left: -57px">
            <el-form-item :label="'团队名称'" prop="teamName">
              <el-input
                v-model="form.teamName"
                clearable
                :placeholder="'请输入团队名称'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input
                v-model="form.memberCode"
                clearable
                :placeholder="'请输入会员编号'"
                @blur="checkName"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item :label="'会员姓名'" prop="memberName">
              <el-input
                v-model="form.memberName"
                clearable
                :disabled="true"
                :placeholder="'请输入会员姓名'"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{
          '取消'
        }}</el-button>
        <el-button class="submit" type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import {
  getTeamList,
  deleteMemberTeam,
  getOneTeam,
  updateTeam,
  saveTeam,
  getVartexList,
  gettheMemberInfo,
  flushAll,
} from "@/api/affiliatedteam";
import { userCountryList } from "@/api/user";
import { updateTeams } from "@/api/settle";
import * as api from "@/api/product";
export default {
  name: "Lstd",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "lstd",
      topList: [
        {
          name: '隶属团队',
          path: "lstd",
        },
      ],
      sxsj: "",
      cjsj: "",
      tableData: [],
      loading: false,
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        vertexName: "", // 体系名称
        teamName: "", // 团队名称
        memberCode: "", // 会员编号
        memberName: "", // 会员姓名
        teamCode: "", // 团队编码
      },
      countryList: [], // 国家
      form: {
        pkBdVertex: "", // 体系名称
        teamName: "", // 团队名称
        memberCode: "", // 会员编号
        memberName: "", // 会员姓名
        teamCode: "", // 团队编码
      },
      rules: {
        pkBdVertex: [
          {
            required: true,
            message: '请选择体系名称',
            trigger: "change",
          },
        ],
        teamName: [
          {
            required: true,
            message: '请输入团队名称',
            trigger: "blur",
          },
        ],

        memberCode: [
          {
            required: true,
            message: '请输入会员编号',
            trigger: "blur",
          },
        ],

        teamCode: [
          {
            required: true,
            message: '请输入团队编码',
            trigger: "blur",
          },
        ],
      },
      updateShow: false,
      addOrEdit: "", // 添加还是修改 true添加 false修改
      updateTitle: "",
      vertexList: [],
    };
  },
  created() {
    this.getTeamList();
    this.getVartexList();
    this.userCountryList();
  },
  methods: {
    // 获取国家
    userCountryList() {
      userCountryList().then((res) => {
        this.countryList = res.data;
      });
    },
    // 根据会员编号查会员姓名
    checkName() {
      gettheMemberInfo(this.form.memberCode).then((res) => {
        this.form.memberName = res.data.memberName;
      });
    },
    // 体系名称下拉选
    getVartexList() {
      getVartexList().then((res) => {
        this.vertexList = res.data;
      });
    },
    // 点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addOrEdit == true) {
            url = saveTeam;
          } else {
            url = updateTeam;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getTeamList();
            }
            // else {
            //   this.$message({
            //     message: res.msg,
            //     type: "warning",
            //   });
            // }
          });
        }
      });
    },
    // 更新团队
    updateTeam(row) {
      this.$confirm('是否确认更新该团队?', "", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        updateTeams(row.pkId).then((res) => {
          if (res.code == "200") {
            this.$message({
              message: res.msg,
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    },
    addflushAll() {
      this.$confirm("是否确认更新全部?", "", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        flushAll().then((res) => {
          if (res.code == "200") {
            this.$message({
              message: res.msg,
              type: "success",
            });
          }
          loading.close();
        });
      });
    },
    // 修改
    update(row) {
      this.updateShow = true;
      this.updateTitle = '修改';
      this.addOrEdit = false;
      getOneTeam(row.pkId).then((res) => {
        this.form = res.data;
      });
    },
    // 添加
    adddate() {
      this.updateShow = true;
      this.updateTitle = '添加';
      this.addOrEdit = true;
      if (this.$refs.form) {
        this.form.pkId = null;
        this.$refs.form.clearValidate();
        this.$refs.form.resetFields();
      }
    },
    handleClose() {
      this.updateShow = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    // 重置
    reChongzhi() {
      this.queryParams.vertexName = "";
      this.queryParams.teamName = "";
      this.queryParams.memberCode = "";
      this.queryParams.memberName = "";
      this.queryParams.teamCode = "";
      this.getTeamList();
    },

    // 删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        deleteMemberTeam(row.pkId).then((res) => {
          this.getTeamList();
        });
      });
    },

    // 获取汇率配置列表
    getTeamList() {
      this.loading = true;
      getTeamList(this.queryParams).then((res) => {
        res.rows.forEach((element) => {
          this.countryList.forEach((child) => {
            if (element.pkCountry == child.pkCountry) {
              element.pkCountryName = child.name;
              element.pkCountryIcon = child.nationalFlag2;
            }
          });
        });
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    checkQiyong(row) {
      if (row.enableState == 0) {
        return '启用';
      } else {
        return '禁用';
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>

<style lang="scss" scoped>
.submit {
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}

.cancels {
  width: 209px;
  height: 48px;
  background: #cccccc;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}

::v-deep .button-s {
  padding: 3px 0;
}

.button-s {
  padding: 3px 0px;
}

::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}

::v-deep .el-table thead {
  color: #000000;
}

::v-deep .el-row {
  margin-top: 20px;
}

.tem {
  display: flex;
  justify-content: center;

  img {
    width: 28px;
    height: 19px;
    margin-right: 3px;
  }
}

.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;

  .thetopbox {
    padding: 0 20px 10px 20px;
    background: #ffffff;
    border-radius: 8px;

    .searchbox {
      display: flex;
      align-items: center;

      .searchtitle {
        margin-right: 10px;
        white-space: nowrap;
      }

      .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 {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }

        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }

        .thebtn3 {
          background: #53b11e;
          color: #ffffff;
          width: 84px;
          height: 32px;
        }
      }
    }
  }
}
</style>
