<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>供应商名称:</span>
        <el-input
          v-model="listQuery.shopName"
          placeholder="供应商名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="shopDialogVisible = true"> 添加供应商 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="660">
      <el-table-column prop="shopName" fixed label="供应商名称" align="center" />
      <el-table-column prop="linkMan" label="联系人" align="center" />
      <el-table-column prop="mobile" label="手机号" align="center">
        <template slot-scope="{ row }">
          {{ row.mobile ? row.mobile : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="shopStatus" label="供应商状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.shopStatus === 90" type="success" size="small">已上线</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="logo" align="center" width="180">
        <template slot-scope="{ row }">
          <el-popover placement="top-start" title="" width="180" trigger="hover">
            <img :src="row.productImage" alt="" style="width: 150px; height: 150px" />
            <img slot="reference" :src="row.productImage" alt="" style="width: 80px; height: 80px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="最近报表时间" align="center">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="添加时间" align="center">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="{ row }">
          <el-button type="success" size="mini" style="margin: 4px" @click="handleRecordDetail(1, row)">
            上传LOGO
          </el-button>
          <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(2, row)">
            数据报表
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[100]"
      :page-size="100"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getCustomerShopList"
    />
    <!--  选择供应商弹窗 -->
    <shop-dialog v-if="shopDialogVisible" ref="shopDialog" @confirm="handleAddShopConfirm" @cancel="handleCancel" />
    <!-- 设置统计状态 -->
    <el-dialog title="设置统计状态" :visible.sync="dialogFormVisible" width="500px">
      <el-form ref="dataForm" :model="renewCustome" :rules="rules" label-position="right">
        <el-form-item prop="image" label="logo">
          <div class="form-el-upload" style="width: 148px; height: 148px">
            <el-upload
              class="avatar-uploader"
              :action="actionUrl"
              :headers="getToken"
              :show-file-list="false"
              :on-success="handleSuccess"
              :before-upload="beforeUpload"
            >
              <div v-loading="loadImgLoading" class="avatar" style="width: 148px; height: 148px; display: block">
                <img
                  v-if="renewCustome.image"
                  :src="renewCustome.image"
                  style="width: 148px; height: 148px; display: block"
                  @error="reloadImage"
                  @load="loadSucess"
                />
                <i
                  v-else
                  class="el-icon-plus avatar-uploader-icon"
                  style="width: 148px; height: 148px; line-height: 148px"
                ></i>
              </div>
            </el-upload>
            <p class="uploader-tips">注：请尽量上传126*50(px)尺寸的图片。</p>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCustomerShopList, saveCustomerShop, renewCustomerShop } from '@/api/user/customer/customer'
import ShopDialog from './components/shop-dialog'
export default {
  name: 'RecordList',
  components: { ShopDialog },
  filters: {},
  data() {
    return {
      isLoading: true,
      listQuery: {
        shopName: '',
        status: '',
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      shopDialogVisible: false,
      dialogFormVisible: false,
      renewCustome: {
        id: '',
        image: ''
      },
      rules: {
        image: [{ required: true, message: '请上传弹窗图片', trigger: 'blur' }]
      }
    }
  },
  computed: {
    etToken() {
      return {
        token: this.$store.getters.token
      }
    },
    actionUrl() {
      return process.env.VUE_APP_BASE_API + '/formData/MultiPictareaddData'
    }
  },
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取行为记录列表
    getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getCustomerShopList()
    },
    // 确认选择供应商
    handleAddShopConfirm(data) {
      console.log('data', data)
      const shopIds = []
      data.forEach((ele) => {
        shopIds.push(ele.shopId)
      })
      this.saveCustomerShop({ shopIds: shopIds.join(',') })
    },
    // 取消选择供应商
    handleCancel() {
      this.shopDialogVisible = false
      this.$refs.shopDialog.visible = false
    },
    // 确认设置统计状态
    handleConfirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.renewCustomerShop(this.renewCustome)
        } else {
          return false
        }
      })
    },
    // 获取潜客搜集供应商列表
    async getCustomerShopList() {
      try {
        this.isLoading = true
        const res = await getCustomerShopList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 保存添加统计供应商
    async saveCustomerShop(params) {
      try {
        await saveCustomerShop(params)
        this.shopDialogVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 统计状态
    async renewCustomerShop(params) {
      try {
        await renewCustomerShop(params)
        this.dialogFormVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 查看详情
    handleRecordDetail(type, row) {
      switch (type) {
        case 1: // 潜客统计列表
          this.renewCustome.id = row.id
          this.renewCustome.status = row.status
          console.log('renewCustome', this.renewCustome)
          this.dialogFormVisible = true
          break
        case 2: // 相关文章
          this.$router.push({
            path: '/user/customer/market-report-list',
            query: { shopId: row.shopId, shopName: row.shopName }
          })
          break
      }
    },
    // 上传图标事件
    handleSuccess(res, file) {
      this.loadImgLoading = true
      this.$nextTick(() => {
        setTimeout(() => {
          this.form.image = res.data
        }, 1000 * 2)
      })
    },
    // 对上传图片的大小、格式进行限制
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isJPG2 = file.type === 'image/jpg'
      const isPNG = file.type === 'image/png'
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isJPG && !isJPG2 && !isPNG) {
        this.$message.error('只支持jpg或png格式图片')
      }
      if (!isLt5M) {
        this.$message.error('请上传5MB以内的图片!')
      }
      return (isJPG || isJPG2 || isPNG) && isLt5M
    },
    reloadImage() {
      this.loadImgLoading = true
      setTimeout(() => {
        this.temp.classifyImage = this.temp.classifyImage.split('?')[0] + '?r=' + Math.floor(Math.random() * 1000)
      }, 1000 * 2)
    },
    loadSucess() {
      this.loadImgLoading = false
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  float: left;
}
.uploader-tips {
  position: absolute;
  bottom: 0;
  left: 160px;
  line-height: 28px;
  color: red;
  margin: 0;
}
</style>
