<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="供应商名称：" label-width="100px" style="display: inline-block;">
              <el-input style="width: 350px" v-model="tableFrom.supplier_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.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.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.is_show" clearable filterable placeholder="请选择" class="selWidth">
                <el-option label="开启" :value="1" />
                <el-option label="关闭" :value="0" />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <el-button size="small" type="success" @click="addSupplier">添加供应商</el-button>
        <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="50" />
        <el-table-column label="供应商名称" prop="supplier_name" min-width="150" align="center"/>
        <el-table-column label="联系人" min-width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.name }}<br />
            {{ scope.row.phone }}
          </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.inviteUser">
              <div class="user-avatar">
                <img :src="scope.row.inviteUser.avatar || moren" />
              </div>
              <div class="user-info">
                <div class="nickname">
                  {{ scope.row.inviteUser.nickname }}
                  <div class="user-id">{{ scope.row.inviteUser.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) == 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) == 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>
                  <el-tag v-if="Number(scope.row.agent_type) == 7" type="info" effect="dark" size="small">餐厅</el-tag>
                  <el-tag v-if="Number(scope.row.agent_type) == 8" type="info" effect="dark" size="small">配送商</el-tag>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="60" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_show == 1" type="success" effect="dark" size="small">开启</el-tag>
            <el-tag v-else type="danger" effect="dark" size="small">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time" min-width="150" align="center"/>
        <el-table-column label="排序" prop="sort" min-width="50" align="center"/>
        <el-table-column label="操作" min-width="200" fixed="right" align="center">
          <template slot-scope="scope">
            <!--<el-button v-if="tableFrom.status === '1'" type="text" size="small" @click="onLogo(scope.row.id)">登录</el-button>-->
            <el-button type="text" size="small" @click="onEdit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="onDetails(scope.row.id)">详情</el-button>
            <!--<el-button v-if="tableFrom.status === '0'" type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)">删除</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>
    <!-- 添加、编辑、详情 -->
    <supplier-detail
      ref="supplierDetail"
      :supplierId="supplierId"
      @closeDrawer="closeDrawer"
      @changeDrawer="changeDrawer"
      @onPassword="onPassword"
      @getList="getList"
      :drawer="drawer"
    ></supplier-detail>
  </div>
</template>

<script>
import supplierDetail from './handle/supplierDetails.vue';
import {supplierList} from "@/api/supplier";





import {
  merchantListApi,
  merchantCreateApi,
  merchantUpdateApi,
  merchantDeleteForm,
  merchantStatuseApi,
  merchantPasswordApi,
  merchantLoginApi,
  changeCopyApi,
  merchantCountApi,
  merchantIsCloseApi,
  getstoreTypeApi,
  getMerCateApi, marginDeductionForm
} from "@/api/merchant";
import { fromList } from "@/libs/constants.js";
import { roterPre } from "@/settings";
import SettingMer from "@/libs/settingMer";
import Cookies from "js-cookie";

export default {
  name: "SupplierList",
  components: { supplierDetail },
  data() {
    return {
      listLoading: true,
      tableFrom: {
        page: 1,
        limit: 20,
        supplier_name: "",
        name: "",
        phone: "",
        is_show: "",
      },
      tableData: {
        data: [],
        total: 0
      },






      fromList: fromList,
      roterPre: roterPre,
      isChecked: false,
      merCateList: [],
      storeType: [],
      headeNum: [
        {
          count: '',
          type: "1",
          title: "正常开启的商户"
        },
        {
          count: '',
          type: "0",
          title: "已关闭商户"
        }
      ],
      autoUpdate: true,
      supplierId: "",
      drawer: false,
      timeVal: []
    };
  },
  mounted() {
    this.getList("");



    this.getHeadNum();
    this.getMerCategory();
    this.getStoreType();
  },
  methods: {
    // 添加供应商
    addSupplier() {
      this.drawer = true;
      this.$refs.supplierDetail.initData();
    },
    // 获取供应商列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      supplierList(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);
    },









    onLogo(id) {
      merchantLoginApi(id)
        .then(res => {
          Cookies.set("merchantToken", res.data.token);
          window.open(SettingMer.httpUrl + res.data.url);
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 选择时间
    selectChange(tab) {
      this.tableFrom.date = tab;
      this.timeVal = [];
      this.tableFrom.page = 1;
      this.getList("");
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = this.timeVal ? this.timeVal.join("-") : "";
      this.tableFrom.page = 1;
      this.getList("");
    },
    // 获取开启商户数
    getHeadNum() {
      merchantCountApi()
        .then(res => {
          this.headeNum[0]["count"] = res.data.valid;
          this.headeNum[1]["count"] = res.data.invalid;
        })
        .catch(res => {});
    },
    // 商户分类；
    getMerCategory() {
        getMerCateApi().then(res => {
            this.merCateList = res.data
        }).catch(res => {
            this.$message.error(res.message)
        })
    },
    getStoreType(){
        getstoreTypeApi().then(res => {
            this.storeType = res.data
        }).catch(res => {
            this.$message.error(res.message)
        })
    },
    // 修改状态
    onchangeIsShow(row) {
      const title = row.is_best === 0 ? "是否开启推荐商户" : "是否关闭推荐商户";
      this.$modalSure(title).then(() => {
        merchantStatuseApi(row.id, row.is_best === 1 ? 0 : 1)
          .then(({ message }) => {
            this.$message.success(message);
            this.getList("");
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    // 开启关闭
    onchangeIsClose(row) {
      merchantIsCloseApi(row.id, row.status === 1 ? 0 : 1)
        .then(({ message }) => {
          this.$message.success(message);
          this.getList("");
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },

    // 编辑
    onEdit(id) {
      this.supplierId = id;
      this.$refs.supplierDetail.isEdit = true;
      this.$refs.supplierDetail.getInfo(id);
      this.drawer = true;
    },
    // 详情
    onDetails(id) {
      this.supplierId = id;
      this.$refs.supplierDetail.isEdit = false;
      this.$refs.supplierDetail.getInfo(id);
      this.drawer = true;
    },
    changeDrawer(v) {
      this.drawer = v;
    },
    closeDrawer() {
      this.drawer = false;
    },
    // 删除
    handleDelete(id) {
      this.$modalForm(merchantDeleteForm(id)).then(() => this.getList(""));
    },
    // 扣除保证金
    onDeduct(id) {
      this.$modalForm(marginDeductionForm(id)).then(() => this.getList(""));
    },

    // 修改密码表单
    onPassword(id) {
      this.$modalForm(merchantPasswordApi(id));
    }
  }
};
</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%));
    }
  }
}


</style>
