<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix" v-hasPermi="['platform:retail:store:people:list']">
        <div class="container">
          <el-form size="small" label-width="100px">
            <div class="time-keyword-search">
              <div style="width: 60%;">
                <el-form-item label="时间选择：">
                  <el-radio-group
                    v-model="searchInfo.dateLimit"
                    type="button"
                    class="mr20"
                    size="small"
                    @change="searchList"
                  >
                    <el-radio-button v-for="(item, i) in fromList.fromTxt" :key="i" :label="item.val">{{
                      item.text
                    }}</el-radio-button>
                  </el-radio-group>
                  <el-date-picker
                    v-model="timeVal"
                    value-format="yyyy-MM-dd"
                    format="yyyy-MM-dd"
                    size="small"
                    type="daterange"
                    placement="bottom-end"
                    placeholder="自定义时间"
                    style="width: 250px"
                    @change="onchangeTime"
                  />
                </el-form-item>
              </div>
              <div class="keyword-search">
                <el-form-item label="分销类型">
                  <el-select v-model="searchInfo.userType" placeholder="请选择" @change="searchList">
                    <el-option label="全部" :value="-1"></el-option>
                    <el-option label="分销商(企业)" :value="1"></el-option>
                    <el-option label="分销员" :value="2"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="所属企业">
                  <el-input
                    v-model.trim="searchInfo.org"
                    placeholder="请输入企业名称"
                    class="selWidth"
                    size="small"
                    clearable
                  >
                    <el-button
                      v-hasPermi="['platform:retail:store:people:list']"
                      slot="append"
                      icon="el-icon-search"
                      size="small"
                      @click="searchList"
                    />
                  </el-input>
                </el-form-item>
              </div>
            </div>
            <el-form-item label="关键字：">
              <el-input
                v-model.trim="searchInfo.keywords"
                placeholder="请输入姓名、电话、UID"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button
                  v-hasPermi="['platform:retail:store:people:list']"
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  @click="searchList"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="distributorList"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
        @selection-change="selectOptions"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="80" align='center' />
        <el-table-column label="头像" min-width="80" align="center">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="分销员姓名" min-width="130" align="center" />
        <el-table-column prop="phone" label="电话" min-width="130" align="center"  />
        <el-table-column prop="brokerageUserOrg" label="所属企业" min-width="150" align="center" />
        <el-table-column label="分销类型" min-width="80" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brokerageUserType === 1 ? '企业' : '个人' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="spreadCount" label="推广人" min-width="100" align="center" />
        <el-table-column
          label="推广订单数量(笔)"
          prop="spreadOrderNum"
          min-width="150"
          align="center"
        />
        <el-table-column
          label="推广订单金额(元)"
          min-width="150"
          prop="spreadOrderTotalPrice"
          align="center"
        />
        <el-table-column prop="promoterTime" label="加入时间" min-width="100" align="center" />
        <el-table-column prop="spreadNickname" label="上级" min-width="150" align='center' />
        <el-table-column
          label="佣金总金额"
          min-width="120"
          prop="totalBrokeragePrice"
          align="center"
        />
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="onSpread(scope.row.id, 'man', '推广人')"
              v-hasPermi="['platform:retail:store:sub:user:list']"
              >推广人</el-button
            >
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click.native="onSpread(scope.row.id, 'order', '推广订单')"
              v-hasPermi="['platform:retail:store:sub:user:list']"
              >推广订单</el-button
            >
            <!-- <el-dropdown>
              <span class="el-dropdown-link"> 更多<i class="el-icon-arrow-down el-icon--right" /> </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  @click.native="onSpread(scope.row.id, 'order', '推广订单')"
                  v-if="checkPermi(['platform:retail:store:promotion:order:list'])"
                  >推广订单</el-dropdown-item
                >
                <el-dropdown-item
                  @click.native="clearSpread(scope.row)"
                  v-if="scope.row.spreadNickname && scope.row.spreadNickname !== '无' && false"
                  v-hasPermi="['platform:retail:store:clean:user:spread']"
                  >清除上级推广人</el-dropdown-item
                >
                <el-dropdown-item
                  @click.native="openSubordinateDialog('add')"
                  v-hasPermi="['platform:retail:store:clean:user:spread']"
                  >添加下级</el-dropdown-item
                >
                <el-dropdown-item
                  @click.native="openSubordinateDialog('clear')"
                  v-hasPermi="['platform:retail:store:clean:user:spread']"
                  >清除下级</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="limit"
          :current-page="page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <!-- 推广人 -->
    <promoterList :showPromoterListDialog="showPromoterListDialog" :titleName="titleName" :uid="uid" :onName="onName" @closePromoterListDialog="closePromoterListDialog" />
    <!-- 添加、清除下级 -->
    <AddSubordinateDialog :showSubordinateDialog="showSubordinateDialog" :type="type" />
  </div>
</template>

<script>
// +---------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +---------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +---------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +---------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +---------------------------------------------------------------------
import { promoterListApi } from '@/api/distribution';
import cardsData from '@/components/cards/index';
import promoterList from '@/components/distribution/promoterList'
import addSubordinateDialog from '@/components/distribution/addSubordinateDialog';
import { checkPermi } from '@/utils/permission'; // 权限判断函数
import AddSubordinateDialog from '@/components/distribution/addSubordinateDialog.vue';
export default {
  name: 'AccountsUser',
  components: { cardsData, promoterList, addSubordinateDialog, AddSubordinateDialog },

  data() {
    return {
      listLoading: false,
      showPromoterListDialog: false,
      showSubordinateDialog: false,
      titleName: '',
      uid: '',
      onName: '',
      fromList: this.$constants.fromList,
      distributorList: [],
      timeVal: [],
      checkList: [],
      searchInfo: {
        dateLimit: '',
        keywords: '',
        userType: null,
        org: ''
      },
      limit: 20,
      total: 0,
      page: 1,
      type: ''
    };
  },

  mounted() {
    if (checkPermi(['platform:retail:store:people:list'])) this.getList();
    console.info('debug');
  },

  methods: {
    checkPermi,

    getList() {
      this.listLoading = true;
      let params = {
        page: this.page,
        limit: this.limit
      }

      this.searchInfo.dateLimit ? params.dateLimit = this.searchInfo.dateLimit : null;
      this.searchInfo.keywords ? params.keywords = this.searchInfo.keywords : null;
      this.searchInfo.org ? params.org = this.searchInfo.org : null;
      this.searchInfo.userType > 0 ? params.userType = this.searchInfo.userType : null;

      promoterListApi(params)
        .then((res) => {
          let { limit, list, page, total, totalPage } = res;

          this.distributorList = list;
          this.limit = limit;
          this.total = total;
          this.page = page;
          this.totalPage = totalPage
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },

    searchList() {
      this.page = 1;
      this.getList();
    },

    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.searchInfo.dateLimit = e ? this.timeVal.join(',') : '';
      this.getList();
    },

    selectOptions(selection) {
      this.checkList = selection;
    },

    onSpread(uid, n, p) {
      this.onName = n;
      this.titleName = p;
      this.uid = uid;
      this.showPromoterListDialog = true;
    },

    // 清除
    // clearSpread(row) {
    //   this.$modalSure('解除【' + row.nickname + '】的上级推广人吗').then(() => {
    //     spreadClearApi(row.uid).then((res) => {
    //       this.$message.success('清除成功');
    //       this.getList(1);
    //     });
    //   });
    // },

    openSubordinateDialog(type) {
      this.type = type;
      this.showSubordinateDialog = true;
    },

    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },

    pageChange(page) {
      this.page = page;
      this.getList();
    },

    closePromoterListDialog() {
      this.showPromoterListDialog = false;
    }
  },
};
</script>

<style lang="scss" scoped>
.selWidth {
  width: 350px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}

/* update  */
.time-keyword-search {
  display: flex;
  flex-direction: row;
  align-items: center;

  .keyword-search {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
</style>
