<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" label-width="90px">
      <el-form-item label="商家名称" prop="businessName">
        <el-input v-model="queryParams.businessName" placeholder="请输入商家名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="负责人姓名" prop="contactPerson">
        <el-input v-model="queryParams.contactPerson" placeholder="请输入负责人姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营业状态" prop="businessStatus">
        <el-select v-model="queryParams.businessStatus" placeholder="请选择营业状态" clearable size="small"
          style="width: 240px">
          <el-option label="正常" :value="true" />
          <el-option label="停业" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="商家分类" prop="businessCategoryId">
        <el-select v-model="queryParams.businessCategoryId" placeholder="请选择商家分类" clearable size="small"
          style="width: 240px">
          <el-option v-for="(v, i) in shopTypeList" :key="i" :label="v.name" :value="v.categoryId" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="shelvedStatus">
        <el-select v-model="queryParams.shelvedStatus" placeholder="请输入状态" clearable size="small" style="width: 240px">
          <el-option label="上架" :value="true" />
          <el-option label="下架" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="所在城市" prop="provinces">
        <el-cascader ref="refSubCat" size="small" v-model="queryParams.provinces" placeholder="请选择所在城市"
          :options="areaTrea" :props="{ expandTrigger: 'hover' }" @change="handleChange"
          style="width: 100%"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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" />
      <el-table-column label="商家logo" align="center" prop="logo">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px" :src="scope.row.logo"
            :preview-src-list="scope.row.logo ? [scope.row.logo] : []"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商家名称" align="center" prop="businessName" />
      <el-table-column label="负责人" align="center" prop="contactPerson" />
      <el-table-column label="联系方式" align="center" prop="contactPhone" />
      <el-table-column label="商家地址" align="center" prop="location" />
      <el-table-column label="营业状态" align="center" prop="businessStatus">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.businessStatus">正常</el-tag>
          <el-tag type="danger" v-if="!row.businessStatus">停业</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="shelvedStatus">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.shelvedStatus">上架</el-tag>
          <el-tag type="danger" v-if="!row.shelvedStatus">下架</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="分类" align="center" prop="businessProductCategoryText" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="!multiple">
        <template slot-scope="scope">
          <el-button size="mini" 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;">
      <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";
export default {
  dicts: ['sys_notice_status'],
  data() {
    return {
      dialogVisible: false,
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: undefined,
        contactPerson: undefined,
        provinces: undefined,
        businessStatus: undefined,
        shelvedStatus: true
      },
      areaTrea: [],
      shopTypeList: [],
      activeList: []
    }
  },
  props: {
    // 多选
    multiple: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    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>