<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="选择时间：" style="width: 100%">
              <el-radio-group
                v-model="tableFrom.date"
                size="small"
                @change="selectChange(tableFrom.date)"
              >
                <el-radio-button
                  v-for="(itemn, indexn) in fromList.fromTxt"
                  :key="indexn"
                  :label="itemn.val"
                >{{ itemn.text }}
                </el-radio-button
                >
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                type="daterange"
                placeholder="选择日期"
                format="yyyy/MM/dd"
                value-format="yyyy/MM/dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item label="状态：">
              <el-radio-group
                v-model="tableFrom.status"
                size="small"
                @change="statusChange(tableFrom.status)"
              >
                <el-radio-button
                  v-for="(itemn, indexn) in statusList.fromTxt"
                  :key="indexn"
                  :label="itemn.val"
                >{{ itemn.text }}
                </el-radio-button
                >
              </el-radio-group>
            </el-form-item>
            <template v-if="tableFrom.merchant_type == 0">
              <el-form-item label="商户分类：">
                <el-select
                  v-model="tableFrom.category_id"
                  clearable
                  placeholder="请选择"
                  class="selWidth"
                  @change="getList(1)"
                >
                  <el-option
                    v-for="item in merCateList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="店铺类型：">
                <el-select
                  v-model="tableFrom.type_id"
                  clearable
                  placeholder="请选择"
                  class="selWidth"
                  @change="getList(1)"
                >
                  <el-option
                    v-for="item in storeType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </template>
            <el-form-item label="关键字：">
              <el-input
                v-model="tableFrom.keyword"
                @keyup.enter.native="getList(1)"
                placeholder="请输入商户名称关键字/联系电话"
                class="selWidth"
              >
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!--表单-->
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" highlight-current-row class="switchTable">
        <el-table-column prop="mer_intention_id" label="ID" min-width="50" align="center" />
        <el-table-column label="申请人" min-width="230" align="center">
          <template slot-scope="scope">
            <div class="user-content" v-if="scope.row.applicant">
              <div class="user-avatar" v-if="scope.row.applicant.avatar && scope.row.applicant.avatar != 1">
                <img :src="scope.row.applicant.avatar" />
              </div>
              <div class="user-info">
                <div class="nickname">{{ scope.row.applicant.nickname }}</div>
                <div class="user-id">ID：{{ scope.row.applicant.uid }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="管理员" min-width="230" align="center">
          <template slot-scope="scope">
            <div class="user-content" v-if="scope.row.manage">
              <div class="user-avatar" v-if="scope.row.manage.avatar && scope.row.manage.avatar != 1">
                <img :src="scope.row.manage.avatar" />
              </div>
              <div class="user-info">
                <div class="nickname">{{ scope.row.manage.nickname }}</div>
                <div class="user-id">ID：{{ scope.row.manage.uid }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商户信息" min-width="180" align="left">
          <template slot-scope="scope">
            商户名称：{{ scope.row.mer_name || '' }}<br />
            <template v-if="[0,2].includes(Number(tableFrom.merchant_type))">
              商户分类：{{ scope.row.category_name || '' }}<br />
              经营类型：{{ scope.row.type_name || '' }}<br />
            </template>
            <template v-if="[0,1].includes(Number(tableFrom.merchant_type))">
              <!--'商户子类型：0=默认，1=酒道馆/大型餐厅，2=小酒馆/中小型餐厅'-->
              <span v-if="tableFrom.merchant_type == 0 && scope.row.merchant_sub_type == 1">商户类型：大型餐厅</span>
              <span v-else-if="tableFrom.merchant_type == 0 && scope.row.merchant_sub_type == 2">商户类型：中小型餐厅</span>
              <span v-else-if="tableFrom.merchant_type == 1 && scope.row.merchant_sub_type == 1">商户类型：酒道馆</span>
              <span v-else-if="tableFrom.merchant_type == 1 && scope.row.merchant_sub_type == 2">商户类型：小酒馆</span>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="联系人信息" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.name || '' }}<br />
            {{ scope.row.phone || '' }}<br />
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="申请时间" min-width="150" align="center" />
        <el-table-column label="资质图片" min-width="80" align="center" >
          <template slot-scope="scope">
            <div class="qualification-image">
              <el-image v-for="(item, index) in scope.row.images" :key="index" :src="item" class="mr5" :preview-src-list="[item]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="80" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1" type="success">通过</el-tag>
            <el-tag v-if="scope.row.status == 0" type="info">未处理</el-tag>
            <el-tag v-if="scope.row.status == 2" type="warning">未通过</el-tag>
            <div v-if="scope.row.status == 2">{{ scope.row.fail_msg }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="mark" label="备注" min-width="150" />
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 0" type="text" size="small" @click="onchangeIsShow(scope.row.mer_intention_id)">
              审核
            </el-button>
            <el-button type="text" size="small" @click="onEdit(scope.row.mer_intention_id)">备注</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row.mer_intention_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="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>

import {
  intentionLstApi,
  auditApi,
  intentionDelte,
  intentionStatusApi,
  getstoreTypeApi,
  getMerCateApi
} from "@/api/merchant";
import {fromList, statusList} from "@/libs/constants.js";
import {roterPre} from "@/settings";
import {merchantTypeText} from "@/filters";

export default {
  name: "MerchantApplication",
  data() {
    return {
      props: {
        emitPath: false
      },
      fromList: fromList,
      statusList: statusList, //筛选状态列表
      roterPre: roterPre,
      isChecked: false,
      listLoading: true,
      merCateList: [],
      storeType: [],
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        date: "",
        status: this.$route.query.status ? this.$route.query.status : "",
        keyword: "",
        mer_intention_id: this.$route.query.id ? this.$route.query.id : "",
        category_id: "",
        type_id: "",
        merchant_type: '',
      },
      mer_id: this.$route.query.id ? this.$route.query.id : "",
      autoUpdate: true,
      timeVal: [],
      merchantTitle: '商户',
    };
  },
  watch: {
    mer_id(newName, oldName) {
      this.getList("");
    },
    '$route' () {
      this.tableFrom.merchant_type = this.$route.meta.merchant_type || 0;
    },
    'tableFrom.merchant_type' () {
      this.getMerCategory();
      this.getStoreType();
      this.getList("");
      this.merchantTitle = merchantTypeText(this.tableFrom.merchant_type);
    },
  },
  mounted() {
    this.tableFrom.merchant_type = this.$route.meta.merchant_type || 0;

  },
  methods: {
    // 选择时间
    selectChange(tab) {
      this.tableFrom.date = tab;
      this.timeVal = [];
      this.tableFrom.page = 1;
      this.getList("");
    },
    statusChange(tab) {
      this.tableFrom.status = tab;
      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("");
    },
    // 商户分类；
    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)
      })
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      intentionLstApi(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);
    },
    // 修改状态
    onchangeIsShow(id) {
      this.$modalForm(intentionStatusApi(id)).then(() => this.getList(""));
    },
    // 添加

    // 编辑
    onEdit(id) {
      this.$modalForm(auditApi(id)).then(() => this.getList(""));
    },
    // 删除
    handleDelete(id) {
      this.$deleteSure().then(() => {
        intentionDelte(id)
          .then(({message}) => {
            this.$message.success(message);
            this.getList("");
          })
          .catch(({message}) => {
            this.$message.error(message);
          });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.qualification-image {
  width: 100%;
  display: inline-flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
}
/deep/ table .el-image {
  display: inline-block !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 {
    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%;
      text-align: left;
      font-size: 15px;
      font-weight: bold;
      line-height: calc(var(--user-content-height- * 65%));
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .user-id {
      width: 100%;
      text-align: left;
      font-size: 13px;
      line-height: calc(var(--user-content-height- * 35%));
    }
  }
}
/deep/ .cell{
  padding: 0!important;
}
</style>
