<template>
  <el-dialog title="选择商户" :modal-append-to-body="true" :visible.sync="dialogVisible" width="1400px"
    :before-close="handleClose">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" :size="ELsize" label-width="auto">
      <el-form-item label="店铺ID" prop="businessId">
        <el-input v-model="queryParams.businessId" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <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>
        <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" :data="list" :border="TableBorder" :size="ELTableSize">
      <el-table-column type="selection" width="50" align="center" v-if="multiple" />
      <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="店铺ID" align="center" prop="businessId" width="100">
        <template slot-scope="{row}">
          {{ row.businessId | TableItemEmpty }}
        </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="100">
        <template slot-scope="{row}">
          <StatusTag :status-vo="row.closeStatus | ShopStatusOptionFilter(true)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80" v-if="!multiple">
        <template slot-scope="scope">
          <el-button :size="ELTableSize" type="text" @click="select(scope.row)">选择</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" />
    <div style="text-align: center; margin-top: 20px;" v-if="multiple">
      <el-button style="margin-right: 50px;" @click="handleClose()">取消</el-button>
      <el-button type="primary" :disabled="activeList.length <= 0" @click="submit">确认选择</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { businessPage, businessCategoryList, cnAreaTree } from "@/api/shop.js";
import { ShopTypeOption, ShopStatusEnum } from "@/utils/statusEnum.js";
export default {
  dicts: ['sys_notice_status'],
  data() {
    return {
      ShopTypeOption,
      dialogVisible: false,
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        closeStatus: ShopStatusEnum.SHOP_NORMAL,
        businessId: undefined,
        businessName: undefined,
        legalName: undefined,
        businessType: undefined,
      },
      areaTrea: [],
      shopTypeList: [],
      activeList: []
    }
  },
  props: {
    // 多选
    multiple: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    joint(row, fields) {
      let str = ''
      fields.forEach(field => {
        str += row[field] || ''
      })
      return str
    },
    open(obj = {}) {
      if (obj.hotStatus || obj.hotStatus === false) {
        this.queryParams.hotStatus = obj.hotStatus;
      }
      this.dialogVisible = true;
      this.getList();
      // this.getShopTypeList();
      // this.getCnAreaTree()
    },
    getShopTypeList() {
      businessCategoryList().then(res => {
        this.shopTypeList = res.data
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // this.ids = selection.map(item => item.userId);
      this.activeList = selection
    },
    getCnAreaTree() {
      cnAreaTree().then(res => {
        this.areaTrea = res.data?.children
      })
    },
    handleClose(done) {
      this.dialogVisible = false;
    },
    select(row) {
      this.$emit("result", row);
      this.handleClose()
    },
    submit() {
      this.$emit("result", this.activeList)
      this.handleClose()
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      businessPage(this.queryParams).then(response => {
        // response.rows.map(v => {
        //   let arr = []
        //   v.businessProductCategoryList.map(k => {
        //     arr.push(k.categoryName)
        //   })
        //   v.businessProductCategoryText = arr.join(",")
        // })
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 省市区选择
    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];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>

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