<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-select v-model="tableFrom.status" class="selWidth" clearable>
                <el-option label="待付款" :value="0"></el-option>
                <el-option label="待分配" :value="1"></el-option>
                <el-option label="已分配" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-button size="small" type="primary" @click="getList(1)">搜索</el-button>
        </div>
      </div>
      <!--表格信息-->
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini">
        <el-table-column prop="id" label="ID" min-width="50" align="center"/>
        <el-table-column label="配送商" min-width="200" align="center">
          <template slot-scope="scope">
            <div class="user-content">
              <div class="user-avatar">
                <img :src="scope.row.agent.avatar || moren" />
              </div>
              <div class="user-info">
                <div class="nickname">{{ scope.row.agent.nickname }}</div>
                <div class="user-id">ID：{{ scope.row.agent.uid }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="绑定商户" min-width="200" align="center">
          <template slot-scope="scope">
            <div class="user-content" v-if="scope.row.mer">
              <div class="user-avatar">
                <img :src="scope.row.mer.mer_avatar || moren" />
              </div>
              <div class="user-info">
                <div class="nickname">{{ scope.row.mer.mer_nickname }}</div>
                <div class="user-id">ID：{{ scope.row.mer.mer_id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付金额" prop="price" min-width="100" align="center"/>
        <el-table-column label="冠名品牌额度" prop="title_quota" min-width="100" align="center"/>
        <el-table-column label="其他品牌额度" prop="other_quota" min-width="100" align="center"/>
        <el-table-column label="状态" min-width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 0" type="info">待付款</el-tag>
            <el-tag v-else-if="scope.row.status == 1" type="warning">待分配</el-tag>
            <el-tag v-else-if="scope.row.status == 2" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time" min-width="150" align="center"/>
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 1" type="text" size="small" class="mr10" @click="allocationOrder(scope.row)">分配</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="block">
        <el-pagination :page-size="tableFrom.limit" :current-page="tableFrom.page" :total="tableData.total" @current-change="pageChange" />
      </div>
    </el-card>
    <!--分配商户弹框-->
    <el-dialog title="请选择商户" :visible.sync="dialogStatus" width="1200px" :before-close="allocationClose">
      <!--列表-->
      <el-table v-loading="merLoading" :data="merTableData.data" style="width: 100%" size="small" highlight-current-row class="switchTable">
        <el-table-column prop="mer_id" label="ID" min-width="60" align="center"/>
        <el-table-column label="商户" min-width="200" align="center">
          <template slot-scope="scope">
            <div class="user-content">
              <div class="user-avatar">
                <img :src="scope.row.mer_avatar || moren" />
              </div>
              <div class="user-info">
                <div class="nickname">{{ scope.row.mer_name }}</div>
                <div class="user-id">Tel：{{ scope.row.mer_phone }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" class="mr10" @click="allocationSubmit(scope.row)">选中</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        :page-size="merTableFrom.limit"
        :current-page="merTableFrom.page"
        :total="merTableData.total"
        @current-change="allocationChangePage" />
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="allocationClose">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { deliveryMerList, deliveryOrderAllocation, deliveryOrderList} from '@/api/marketing'
export default {
  name: "preSaleProductList",
  components: { },
  data() {
    return {
      moren: require("@/assets/images/f.png"),
      listLoading: true,
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        status: '',
      },
      // 弹框
      currentInfo: {},
      dialogStatus: false,
      merLoading: false,
      merTableFrom: {
        page: 1,
        limit: 10,
        agent_id: '',
      },
      merTableData: {
        data: [],
        total: 0
      },



    };
  },
  watch: {},
  mounted() {
    this.getList('');
  },
  methods: {
    // 获取列表
    getList(num) {
      let _this = this;
      _this.listLoading = true;
      _this.tableFrom.page = num ? num : this.tableFrom.page;
      deliveryOrderList(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('');
    },
    // 分配 - 显示商户列表
    allocationOrder(item){
      this.currentInfo = item;
      this.dialogStatus = true;
      this.getMerList(1);
    },
    // 分配 - 商户列表
    getMerList(num){
      let _this = this;
      _this.merLoading = true;
      _this.merTableFrom.page = num ? num : this.merTableFrom.page;
      _this.merTableFrom.agent_id = this.currentInfo.agent_id;
      deliveryMerList(this.merTableFrom).then((res) => {
        _this.merTableData.data = res.data.list;
        _this.merTableData.total = res.data.count;
        _this.merLoading = false;
      }).catch((res) => {
        _this.merLoading = false;
        _this.$message.error(res.message);
      });
    },
    // 分配 - 关闭弹框
    allocationClose(){
      let _this = this;
      _this.currentInfo = {};
      _this.dialogStatus = false;
    },
    // 分配 - 表单分页
    allocationChangePage(page) {
      this.getMerList(page);
    },
    // 分配 - 选中
    allocationSubmit(item){
      let _this = this;
      let params = {
        mer_id: item.mer_id,
        agent_delivery_id: _this.currentInfo.id
      };
      this.$confirm('是否确认分配给当前商户，确认后订单赠送的相关额度会赠送给当前商户！', '提示', {
        confirmButtonText: '确认分配',
        cancelButtonText: '取消分配',
        type: 'warning'
      }).then(() => {
        deliveryOrderAllocation(params).then((res) => {
          _this.allocationClose();
          _this.getList('');
        }).catch((res) => {
          _this.$message.error(res.message);
        });
      }).catch(() => {});
    }

  },
};
</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: center;
  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{
    height: var(--user-content-height-);
    display: inline-flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: flex-start;
    .nickname{
      font-size: 15px;
      font-weight: bold;
      line-height: calc(var(--user-content-height- * 65%));
    }
    .user-id{
      font-size: 13px;
      line-height: calc(var(--user-content-height- * 35%));
    }
  }
}
</style>
