<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" :size="ELsize" label-width="auto">
      <el-form-item label="店铺名称" prop="businessName">
        <el-input v-model="queryParams.businessName" placeholder="请输入商家名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalName">
        <el-input v-model="queryParams.legalName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺分类" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择" clearable>
          <el-option v-for="(v, i) in ShopTypeOption" :key="i" :label="v.label" :value="v.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择" clearable>
          <el-option v-for="(v, i) in AuditStatusOption" :key="i" :label="v.label" :value="v.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :border="TableBorder" :size="ELTableSize" :data="list">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="{$index}">
          {{ $getTableIndex(queryParams, $index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="店铺信息" align="center" prop="businessName">
        <template slot-scope="{row}">
          <div>
            <p class="table-p">店铺名称：{{ row.businessName | TableItemEmpty }}</p>
            <p class="table-p">店铺所在区域：{{ joint(row, ['provinceName', 'cityName', 'areaName']) | TableItemEmpty }}</p>
            <p class="table-p">店铺地址：{{ row.address }}</p>
            <p class="table-p">客服电话：{{ row.customerServicePhone }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="主体信息" align="center" prop="enterpriseName">
        <template slot-scope="{row}">
          <div>
            <p class="table-p">企业名称：{{ row.enterpriseName }}</p>
            <p class="table-p">统一信用代码：{{ row.uscCode }}</p>
            <p class="table-p">法人姓名：{{ row.legalName }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="店铺类型" align="center" width="150">
        <template slot-scope="{row}">
          {{ row.businessType | ShopTypeOptionFilter | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" width="150">
        <template slot-scope="{row}">
          {{ row.createTime | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="businessStatus" width="120">
        <template slot-scope="{row}">
          <StatusTag :status-vo="row.businessStatus | ShopStatusOptionFilter(true)" />
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="{row}">
          <el-button :size="ELTableSize" v-if="row.auditStatus === AuditStatusEnum.AUDIT_PENDING" type="text"
            icon="el-icon-edit" @click="handleUpdate(row, 'check')">审核</el-button>
          <el-button :size="ELTableSize" v-else type="text" icon="el-icon-edit"
            @click="handleUpdate(row, 'show')">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <systemUser ref="systemUserRef" @success="getList" />
  </div>
</template>

<script>
import { businessApplyWaitPage, businessStatus, businessDel, businessCategoryList, cnAreaTree, systemUserResetPwd } from "@/api/shop.js";
// import { partnerRelationList } from "@/api/partner"
import systemUser from "./systemUser.vue"
import { ShopTypeOption, AuditStatusOption, AuditStatusEnum } from "@/utils/statusEnum.js";
export default {
  components: {
    systemUser
  },
  data() {
    return {
      ShopTypeOption,
      AuditStatusOption,
      AuditStatusEnum,
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: undefined,
        businessType: undefined,
        legalName: undefined,
        auditStatus: undefined
      },
      daterangeCreateTime: [],
      areaTrea: [],
      shopTypeList: [],
      partnerList: []
    }
  },
  created() {
    if (this.$route.query.parentId) {
      this.queryParams.cityPartnerId = this.$route.query.parentId * 1
    }
    this.getShopTypeList()
    this.getCnAreaTree();
    // this.getPartnerList()
    this.getList()
  },
  methods: {
    joint(row, fields) {
      let str = ''
      fields.forEach(field => {
        str += row[field] || ''
      })
      return str
    },
    openSystemUser(row) {
      this.$refs.systemUserRef.open(row)
    },
    getPartnerList() {
      partnerRelationList().then(res => {
        this.partnerList = res.data
      })
    },
    sortChange(e) {
      this.queryParams.orderByColumn = 'order_index';
      this.queryParams.isAsc = e.order;
      this.handleQuery()
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      businessApplyWaitPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCnAreaTree() {
      cnAreaTree().then(res => {
        this.areaTrea = res.data?.children
      })
    },
    // 省市区选择
    handleChange(value) {
      this.queryParams.provinces = value;
      this.queryParams.provinceCode = value[0];
      this.queryParams.provinceName = this.$refs['refSubCat'].getCheckedNodes()[0].pathLabels[0];
      this.queryParams.cityCode = value[1];
      this.queryParams.cityName = this.$refs['refSubCat'].getCheckedNodes()[0].pathLabels[1];
      this.queryParams.areaCode = value[2];
      this.queryParams.areaName = this.$refs['refSubCat'].getCheckedNodes()[0].pathLabels[2];
    },
    getShopTypeList() {
      businessCategoryList().then(res => {
        this.shopTypeList = res.data
      })
    },
    handleAdd() {
      this.$router.push({
        path: '/shop/details'
      })
    },
    handleUpdate(row, type) {
      this.$router.push({
        path: '/shop/details',
        query: {
          businessAuditId: row.businessAuditId,
          type: type
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        shelvedStatus: undefined,
        orderIndex: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.queryParams.provinceCode = undefined;
      this.queryParams.provinceName = undefined;
      this.queryParams.cityCode = undefined;
      this.queryParams.cityName = undefined;
      this.queryParams.areaCode = undefined;
      this.queryParams.areaName = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.businessAuditId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.businessAuditId || this.ids;
      this.$modal.confirm('是否确认删除商家名称为"' + row.businessName + '"的数据项？').then(() => {
        this.loading = true;
        return businessDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 上下架按钮操作 */
    setStatus(row) {
      const ids = row.businessAuditId || this.ids;
      let statusText = row.shelvedStatus ? '下架' : '上架';
      this.$modal.confirm('是否确认' + statusText + '商家名称为"' + row.businessName + '"的数据项？').then(() => {
        this.loading = true;
        return businessStatus(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess(statusText + "成功");
      }).finally(() => {
        this.loading = false;
      });
    },
  }
}
</script>

<style lang='scss' scoped></style>