<template>
  <div class="divBox">
    <el-card class="box-card">
      <!--顶部搜索-->
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px" :inline="true">
            <el-form-item label="申请人UID：" label-width="100px" style="display: inline-block;">
              <el-input style="width: 350px" v-model="tableFrom.uid" placeholder="请输入申请人uid" class="selWidth"></el-input>
            </el-form-item>
            <el-form-item label="联系人姓名：" label-width="100px" style="display: inline-block;">
              <el-input style="width: 350px" v-model="tableFrom.contact_name" placeholder="请输入联系人姓名" class="selWidth"></el-input>
            </el-form-item>
            <el-form-item label="联系电话：" label-width="100px" style="display: inline-block;">
              <el-input style="width: 350px" v-model="tableFrom.contact_phone" placeholder="请输入联系电话" class="selWidth"></el-input>
            </el-form-item>
            <el-form-item label="状态：" label-width="100px" style="display: inline-block;">
              <el-select v-model="tableFrom.status" clearable filterable placeholder="请选择" class="selWidth">
                <el-option label="审核中" :value="0" />
                <el-option label="已通过" :value="1" />
                <el-option label="已驳回" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item label="代理类型：" label-width="100px" style="display: inline-block;">
              <el-select v-model="tableFrom.agent_type" clearable filterable placeholder="请选择" class="selWidth">
                <!--<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>
        </div>
        <el-button size="small" type="primary" @click="getList(1)">搜索</el-button>
      </div>
      <!--列表-->
      <el-table v-loading="listLoading" :data="tableData.data">
        <el-table-column prop="id" label="ID" min-width="80" />
        <el-table-column label="邀请人" min-width="230" 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 v-if="Number(scope.row.parent.agent_type) == 1" type="info" effect="dark" size="small">总部发起人</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 2" type="info" effect="dark" size="small">省发起人</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 3" type="info" effect="dark" size="small">省公司外勤</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 4" type="info" effect="dark" size="small">省公司内勤</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 5" type="info" effect="dark" size="small">区县运营商</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 6" type="info" effect="dark" size="small">区县合伙人</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 7" type="info" effect="dark" size="small">餐厅</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 8" type="info" effect="dark" size="small">配送商</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 9" type="info" effect="dark" size="small">总部外勤</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 10" type="info" effect="dark" size="small">总部内勤</el-tag>
                  <el-tag v-if="Number(scope.row.parent.agent_type) == 11" type="info" effect="dark" size="small">烟酒店代销商</el-tag>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="申请人" min-width="230" 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 v-if="Number(scope.row.agent_type) == 1" type="info" effect="dark" size="small">总部发起人</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 2" type="info" effect="dark" size="small">省发起人</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 3" type="info" effect="dark" size="small">省公司外勤</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 4" type="info" effect="dark" size="small">省公司内勤</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 5" type="info" effect="dark" size="small">区县运营商</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 6" type="info" effect="dark" size="small">区县合伙人</el-tag>
                  <template v-if="Number(scope.row.agent_type) == 7">
                    <el-tag type="info" effect="dark" size="small">餐厅</el-tag>
                    <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 type="info" effect="dark" size="small">配送商</el-tag>
                    <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="Number(scope.row.agent_type) == 9" type="info" effect="dark" size="small">总部外勤</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 10" type="info" effect="dark" size="small">总部内勤</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 11" type="info" effect="dark" size="small">烟酒店代销商</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="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.orderInfo && scope.row.orderInfo.status != 3" type="info">待支付</el-tag>
            <template v-else>
              <el-tag v-if="scope.row.status == 0" type="primary">待审核</el-tag>
              <el-tag v-if="scope.row.status == 1" type="success">已通过</el-tag>
              <el-tag v-if="scope.row.status == 2" type="warning">已驳回</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" prop="create_time" min-width="150" align="center"/>
        <el-table-column label="操作" min-width="280" fixed="right" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.status == 0 && (!scope.row.orderInfo || scope.row.orderInfo.status == 3)">
              <el-button type="success" size="small" @click="toExamine(scope.row.id, 1)">通过</el-button>
              <el-button type="danger" size="small" @click="toExamine(scope.row.id, 2)">驳回</el-button>
            </template>
            <el-button type="info" size="small" @click="seeDetails(scope.row)">查看详情</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="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--查看详情-->
    <el-drawer :with-header="false" :size="1000" :visible.sync="drawer" direction="rtl" :before-close="closeSeeDetails">
      <div>
        <!--顶部内容-->
        <div class="head">
          <div class="text">
            <div class="title">
              <span class="bold">查看申请信息</span>
            </div>
          </div>
        </div>
        <!--具体内容-->
        <el-form ref="seeInfoField" size="small" :model="seeInfo" label-width="130px" @submit.native.prevent>
          <el-tabs type="border-card" v-model="activeName">
            <el-tab-pane label="基本信息" name="detail">
              <div class="section">
                <div class="title">邀请人</div>
                <el-row :gutter="24" class="mt20">
                  <el-col :span="12">
                    <el-form-item label="邀请人：">
                      <div class="user-content" v-if="seeInfo.parent">
                        <div class="user-avatar">
                          <img :src="seeInfo.parent.avatar || moren" />
                        </div>
                        <div class="user-info">
                          <div class="nickname">{{ seeInfo.parent.nickname }}</div>
                          <div class="agent-type-text">
                            <el-tag type="info" effect="dark" size="small">{{ seeInfo.parent.uid }}</el-tag>
                          </div>
                        </div>
                      </div>
                    </el-form-item>
                  </el-col>
                </el-row>
                <div class="title">基础信息</div>
                <el-row :gutter="24" class="mt20">
                  <el-col :span="8">
                    <el-form-item label="联系人姓名：">
                      <el-input :value="seeInfo.contact_name" readonly disabled />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="联系人电话：">
                      <el-input :value="seeInfo.contact_phone" readonly disabled />
                    </el-form-item>
                  </el-col>
                  <el-col v-if="seeInfo.agent_type != 11" :span="12">
                    <el-form-item label="地区：">
                      <el-input type="text" :value="seeInfo.province_name + (seeInfo.city_name || '') + (seeInfo.area_name || '') + (seeInfo.street_name || '')" readonly disabled />
                    </el-form-item>
                  </el-col>
                  <template v-if="seeInfo.agent_type == 7">
                    <el-col :span="12" >
                      <el-form-item label="详细地址：">
                        <el-input :value="seeInfo.address" readonly disabled/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="商户名称：">
                        <el-input :value="seeInfo.mer_name" readonly disabled/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="商户分类：">
                        <el-input :value="seeInfo.mer_category_name" readonly disabled/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="商户类型：">
                        <el-input :value="seeInfo.mer_type_name" readonly disabled/>
                      </el-form-item>
                    </el-col>
                  </template>
                </el-row>
              </div>
            </el-tab-pane>
            <el-tab-pane label="证件凭证" name="account">
              <div class="section">
                <el-row :gutter="24" class="mt20">
                  <el-col :span="24">
                    <el-form-item :label="seeInfo.agent_type == 11 ? '凭证：' : '商户资质相关图片：'">
                      <el-image
                        class="id-image"
                        v-for="(item,index) in seeInfo.mer_images"
                        :key="index"
                        :src="item"
                        :preview-src-list="[item]" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-form>
      </div>
      <div class="footer">
        <el-button size="small" @click="closeSeeDetails">取消</el-button>
      </div>
    </el-drawer>

  </div>
</template>

<script>
import {agentApplyList, agentApplyToExamine} from "@/api/marketing";

export default {
  name: "SupplierList",
  components: { },
  data() {
    return {
      moren: require("@/assets/images/f.png"),
      listLoading: true,
      tableFrom: {
        page: 1,
        limit: 20,
        uid: "",
        contact_name: "",
        contact_phone: "",
        status: "",
        agent_type: ""
      },
      tableData: {
        data: [],
        total: 0
      },
      // 弹框
      activeName: 'detail',
      drawer: false,
      seeInfo: {},
    };
  },
  mounted() {
    this.getList("");
  },
  methods: {
    // 获取申请列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      agentApplyList(this.tableFrom).then(res => {
        this.tableData.data = res.data.list;
        this.tableData.total = res.data.count;
        this.listLoading = false;
      }).catch(res => {
        this.listLoading = false;
        this.$message.error(res.message);
      });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList(1);
    },
    // 审核操作
    toExamine(id, status){
      let _this = this;
      let params = {
        id: id,
        status: status
      };
      if(Number(status) === 2){
        // 驳回
        _this.$prompt('驳回原因', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputErrorMessage: '请输入原因',
          inputType: 'textarea',
          inputPlaceholder: '请输入原因',
          inputValidator: (value) => {
            if (!value) {
              return '请输入原因'
            }
          }
        }).then(({value}) => {
          params.reason = value || '';
          _this.submitToExamine(params);
        }).catch(() => {})
      }else{
        // 通过
        this.$confirm('是否确认审核通过?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.submitToExamine(params);
        }).catch(() => {});
      }
    },
    // 审核操作 - 提交审核
    submitToExamine(params){
      let _this = this;
      agentApplyToExamine(params).then(res => {
        _this.$message.success(res.message);
        _this.getList();
      }).catch(res => {
        _this.$message.error(res.message);
      });
    },
    // 查看详情
    seeDetails(info){
      this.seeInfo = info || {};
      this.drawer = true;
    },
    // 关闭查看详情弹框
    closeSeeDetails(){
      this.drawer = false;
    }
  }
};
</script>

<style scoped lang="scss">
.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%));
    }
  }
}
.head {
  padding: 20px 20px 10px;
  .full {
    display: flex;
    align-items: center;
    .order_icon {
      width: 60px;
      height: 60px;
    }
    .text {
      align-self: center;
      flex: 1;
      min-width: 0;
      padding-left: 12px;
      font-size: 13px;
      color: #606266;
      .title {
        margin-bottom: 10px;
        font-weight: 500;
        font-size: 16px;
        line-height: 16px;
        color: #282828;
      }
    }
  }
  .bold{
    font-weight: bold;
  }
  .list {
    display: flex;
    margin-top: 20px;
    overflow: hidden;
    list-style: none;
    padding: 0;
    .item {
      flex: none;
      width: 200px;
      font-size: 14px;
      line-height: 14px;
      color: rgba(0, 0, 0, 0.85);
      .title {
        margin-bottom: 12px;
        font-size: 13px;
        line-height: 13px;
        color: #666666;
      }
    }
  }
}
.footer{
  width: 100%;
  text-align: center;
  position: absolute;
  bottom: 17px;
  padding-top: 17px;
  border-top: 1px dashed #eeeeee;
}
.selWidth{
  margin-bottom: 10px!important;
}
.id-image{
  width: 150px;
  height: 150px;
  margin-right: 10px;


}
</style>
