<template>
  <div class="divBox">
    <!--主要内容-->
    <el-card class="box-card">
      <!--顶部搜索栏-->
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline size="small" label-width="80px">
            <el-form-item label="">
              <el-input v-model="tableFrom.uid" placeholder="请输入用户ID" class="selWidth" clearable />
              <el-input v-model="tableFrom.contact_name" placeholder="请输入联系人姓名" class="selWidth" clearable />
              <el-input v-model="tableFrom.contact_phone" placeholder="请输入联系人电话" class="selWidth" clearable />
              <el-select v-model="tableFrom.agent_type" class="selWidth" clearable>
                <el-option label="总部发起人" :value="1"></el-option>
                <el-option label="总部外勤" :value="9"></el-option>
                <el-option label="总部内勤" :value="10"></el-option>
                <el-option label="省发起人" :value="2"></el-option>
                <el-option label="省公司外勤" :value="3"></el-option>
                <el-option label="省公司内勤" :value="4"></el-option>
                <el-option label="区县运营商" :value="5"></el-option>
                <el-option label="区县合伙人" :value="6"></el-option>
                <el-option label="餐厅" :value="7"></el-option>
                <el-option label="配送商" :value="8"></el-option>
                <el-option label="烟酒店代销商" :value="11"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-button size="small" type="primary" @click="getAgentList(1)">搜索</el-button>
          <el-button size="small" type="success" @click="showEditInfoPopup(0)">总部发起人管理</el-button>
        </div>
      </div>
      <!--表格信息-->
      <el-table v-loading="userListLoading" :data="tableData.data" style="width: 100%" size="mini">
        <el-table-column label="用户信息" min-width="280" align="center">
          <template slot-scope="scope">
            <div class="user-content">
              <div class="user-avatar">
                <img :src="scope.row.user.avatar || moren" />
              </div>
              <div class="user-info">
                <div class="nickname">
                  {{ scope.row.user.nickname }}
                  <div class="user-id">{{ scope.row.user.uid }}</div>
                </div>
                <div class="agent-type-text">
                  <!--角色信息-->
                  <el-tag type="info" effect="dark" size="small">{{ scope.row.agent_type_text || '' }}</el-tag>
                  <!--餐厅关联商户-->
                  <template v-if="Number(scope.row.agent_type) == 7">
                    <el-tag v-if="scope.row.mer" type="success" effect="dark" size="small">{{ scope.row.mer.mer_name }}</el-tag>
                  </template>
                  <!--配送商关联商户-->
                  <template v-if="Number(scope.row.agent_type) == 8">
                    <el-tag v-if="Object.values(scope.row.merList).length > 0" type="success" effect="dark" size="small">
                      {{ scope.row.merList[0].mer_name }}
                      {{ Object.values(scope.row.merList).length > 1 ? '+'+Object.values(scope.row.merList).length : '' }}
                    </el-tag>
                  </template>
                  <!--公司名称-->
                  <el-tag v-if="(scope.row.agent_type == 2 || scope.row.agent_type == 5) && scope.row.corporate_name" type="warning" effect="dark" size="small">
                    {{ scope.row.corporate_name || '' }}
                  </el-tag>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="归属" min-width="280" align="center">
          <template slot-scope="scope">
            <div class="user-content" v-if="scope.row.pid > 0">
              <div class="user-avatar">
                <img :src="scope.row.parent.avatar || moren" />
              </div>
              <div class="user-info">
                <div class="nickname">
                  {{ scope.row.parent.nickname }}
                  <div class="user-id">{{ scope.row.parent.uid }}</div>
                </div>
                <div class="agent-type-text">
                  <!--角色信息-->
                  <el-tag type="info" effect="dark" size="small">{{ scope.row.parent.agent_type_text || '' }}</el-tag>
                  <!--公司名称-->
                  <el-tag v-if="(scope.row.parent.agent_type == 2 || scope.row.parent.agent_type == 5) && scope.row.parent.corporate_name" type="warning" effect="dark" size="small">
                    {{ scope.row.parent.corporate_name || '' }}
                  </el-tag>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="联系人" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.contact_name }}<br />
            {{ scope.row.contact_phone }}
          </template>
        </el-table-column>
        <el-table-column label="地址" min-width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.province_name }}{{ scope.row.city_name }}{{ scope.row.area_name }}{{ scope.row.street_name }}<br />
            {{ scope.row.address }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="create_time" min-width="130" align="center"/>
        <el-table-column label="操作" min-width="120" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" effect="dark" @click="showEditInfoPopup(scope.row.agent_type,scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleUserSizeChange"
          @current-change="pageUserChange"
        />
      </div>
    </el-card>
    <!--编辑弹框-->
    <edit-info ref="editInfo" :agent_type="agent_type" :agent_id="agent_id" :drawer_show="drawer_show" @closeEdit="closeEditInfoPopup"></edit-info>
  </div>
</template>

<script>
import editInfo from './template/edit';
import {agentList} from "@/api/marketing";
export default {
  name: "preSaleProductList",
  components: {
    editInfo
  },
  data() {
    return {
      moren: require("@/assets/images/f.png"),
      // 编辑弹框
      drawer_show: false,
      agent_type: 0,
      agent_id: 0,
      // 列表
      userListLoading: false,
      tableFrom: {
        page: 1,
        limit: 20,
        uid: '',
        contact_name: '',
        contact_phone: '',
        agent_type: '',

      },
      tableData: {
        data: [],
        total: 0,
      },
    };
  },
  watch: {},
  mounted() {
    this.getAgentList();
  },
  methods: {
    // 代理编辑 - 弹出弹框
    showEditInfoPopup(agent_type,agent_id = 0){
      // 类型：1=总部发起人,2=省发起人,3=省公司外勤,4=省公司内勤,5=区县运营商,6=区县合伙人,7=餐厅,8=配送商
      this.agent_type = agent_type || 0;
      this.agent_id = agent_id || 0;
      this.drawer_show = true;
    },
    // 代理编辑 - 关闭弹框
    closeEditInfoPopup() {
      this.drawer_show = false;
      this.getAgentList('');
    },
    // 代理列表
    getAgentList(num){
      let _this = this;
      _this.userListLoading = true;
      _this.tableFrom.page = num ? num : _this.tableFrom.page;
      agentList(_this.tableFrom)
        .then((res) => {
          _this.tableData.data = res.data.list;
          _this.tableData.total = res.data.count;
          _this.userListLoading = false;
        })
        .catch((res) => {
          _this.userListLoading = false;
          _this.$message.error(res.message);
        });
    },
    pageUserChange(page) {
      this.tableFrom.page = page;
      this.getUserList('');
    },
    handleUserSizeChange(val) {
      this.tableFrom.limit = val;
      this.getUserList('');
    },
  },
};
</script>

<style scoped lang="scss">
/deep/ .el-table .cell{
  padding: 0!important;
}
.selWidth{
  margin-bottom: 10px!important;
}
.user-content{
  --user-content-height-: 80px;

  height: var(--user-content-height-);
  width: 100%;
  display: inline-flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-items: center;
  .user-avatar{
    height: var(--user-content-height-);
    width: var(--user-content-height-);
    display: inline-flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    img{
      height: 80%!important;
      width: 80%!important;
      border-radius: 50% !important;
    }
  }
  .user-info{
    max-width: calc(100% - var(--user-content-height-));
    height: var(--user-content-height-);
    display: inline-flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: flex-start;
    .nickname{
      width: 100%;
      height: 25px;
      line-height: 25px;
      text-align: left;
      font-size: 15px;
      font-weight: bold;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      display: inline-flex;
      flex-direction: row;
      flex-wrap: nowrap;
      justify-content: flex-start;
      align-items: center;
      .user-id{
        color: #fff;
        background-color: #409eff;
        border-color: #409eff;
        height: 18px;
        line-height: 20px;
        font-size: 13px;
        padding: 0 5px;
        border-radius: 5px;
        margin-left: 10px;
        width: max-content!important;
      }
    }
    .user-id{
      width: 100%;
      text-align: left;
      font-size: 13px;
      line-height: calc(var(--user-content-height- * 35%));
    }
  }
}
</style>
