<template>
  <div>
    <ai-list class="list">
      <ai-title
        slot="title"
        title="商品列表"
        isShowBottomBorder isShowBack @onBackClick="cancel(false)">
      </ai-title>
      <template slot="content">
        <div style="margin-right: 0px; margin-bottom: 10px;">
          <el-select v-model="search.orderBy" :clearable="true" @change="search.current =1, getList()" placeholder="请选择排序方式" size="small">
            <el-option
              v-for="item in orderBys"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr; gap: 16px;">
          <el-card v-for="item in tableData" :key="item.id" :body-style="{ padding: '0px', margin: '5px' }">
            <img :src="item.imgUrl" class="image">
            <div style="padding: 14px;">
              <div class="bottom clearfix">
                <div style="margin-bottom: 5px;">
                  <div style="display: inline; margin-left: 5px;">${{ item.price }}<sub style="margin-left: 2px;" v-html="getPricePercent(item.priceChange)"></sub></div>
                  <div style="display: inline; margin-right: 5px; float: right;">{{ item.saleTotal }}<sub style="margin-left: 2px;" v-html="getSalePercent(item.saleChange)"></sub></div>
                </div>
                <ai-product-drop-down :params="item" :isShowDetail="true" @onGoDetail="goDetail(item.goodsId, item.monitorId)" style="float: right;"></ai-product-drop-down>
              </div>
            </div>
          </el-card>
          <el-pagination
            layout="prev, pager, next"
            style="position: absolute; bottom: 0px; right: 40px;"
            :total="total"
            :page-size.sync="search.size"
            :current-page.sync="search.current"
            @current-change="getList">
          </el-pagination>
        </div>
      </template>
    </ai-list>

    <div class="productDetail">
      <el-dialog
        title="商品详情"
        :visible="isShowDetailDlg"
        :close-on-click-modal="false"
        width="80%"
        :before-close="handleClose">
        <ai-product-detail v-if="isShowDetailDlg" :params="detailParams"/>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AiProductDetail from "@/components/AiProductDetail.vue";
import AiProductDropDown from '@/components/AiProductDropDown.vue';

  export default {
    name: 'DetailPage',
    props: ['params'],
    components: {AiProductDetail, AiProductDropDown},
    data () {
      return {
        monitorId: '',
        search: {
          current: 1,
          type: '1',
          size: 120,
          orderBy: ''
        },
        orderBys: [{
          value: '0',
          label: '按销量涨辐排序'
        },{
          value: '1',
          label: '按价格涨辐排序'
        },{
          value: '2',
          label: '按销量排序'
        },{
          value: '3',
          label: '按价格排序'
        }],
        tableData: [],
        total: 0,
        isShowDetailDlg: false,
        detailParams: {}
      }
    },

    created () {
      this.monitorId = this.params.id
      this.getList()
    },

    methods: {
      getList () {
        this.$http.post('/api/monitorDetail/myPageNew',null,{
          params: {
            monitorId: this.monitorId,
            ...this.search
          }
        }).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        })
      },
      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      },
      getPricePercent(data) {
        if (data < 0) {
          return '<div style="display: inline; color: green">↓' + data + '%</div>'
        } else if (data > 0) {
          return '<div style="display: inline; color: red">↑' + data + '%</div>'
        }
        return ''
      },
      getSalePercent(data) {
        if (data < 0) {
          return '<div style="display: inline; color: green">↓' + data + '%</div>'
        } else if (data > 0) {
          return '<div style="display: inline; color: red">↑' + data + '%</div>'
        }
        return ''
      },
      handleClose() {
        this.isShowDetailDlg = false
      },
      goDetail (goodsId, monitorId) {
        this.detailParams = {goodsId: goodsId, monitorId: monitorId}
        this.isShowDetailDlg = true
      }
    }
  }
</script>

<style scoped lang="scss">
.productDetail {
  :deep(.el-dialog) {
      height: 78vh;
      overflow: auto;
  }

}
.time {
    font-size: 13px;
    color: #999;
  }
  
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
      display: table;
      content: "";
  }
  
  .clearfix:after {
      clear: both
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
