<template>
  <div class="p-2">
    <el-card shadow="hover" class="mb-[10px]">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="default" class="ry_form">
        <el-form-item label="用户手机号" prop="phone">
          <el-input v-model="queryParams.phone" placeholder="用户手机号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="queryParams.productName" placeholder="商品名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item class="flex_one tr">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="hover">
      <el-table v-loading="loading" :data="umsMemberCartList" @selection-change="handleSelectionChange" border cell-class-name="my-cell">
        <!--      <el-table-column type="selection" width="55"  />-->
        <el-table-column label="用户信息" prop="phoneHidden" align="center" >
          <template v-slot="scope">
            <p>{{ scope.row.member.phoneHidden }}</p>
            <p>{{ scope.row.member.nickname }}</p>
          </template>
        </el-table-column>
        <el-table-column label="用户备注" prop="mark" align="center" >
          <template v-slot="scope">
            <p>{{ scope.row.member.mark }}</p>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="productName" align="center"  />
        <el-table-column label="商品规格" prop="spData" align="center" >
          <template v-slot="scope">
            <div class="product-container">
              <el-popover placement="right" trigger="hover">
                <el-image :src="scope.row.pic" style="width: 350px; height: 350px" />
                <template v-slot:reference>
                  <el-image class="small-img product-item" :src="scope.row.pic" style="width: 40px; height: 40px" />
                </template>
              </el-popover>
              <div class="product-item" style="margin-left: 5px">
                <div class="sp-data">
                  <span v-for="(value, key) in JSON.parse(scope.row.spData)">{{ key }}：{{ value }}&nbsp;</span>
                </div>
                <div class="product-item quantity">
                  <span style="margin-right: 10px">￥{{ scope.row.price }}</span>
                  <span>x{{ scope.row.quantity }}</span>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <!--      <el-table-column label="价格"  prop="price">-->
        <!--        <template v-slot="scope">-->
        <!--          <div>{{ scope.row.price.toFixed(2) }}</div>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column label="状态" prop="skuIfExist" align="center" >
          <template v-slot="scope">
            <el-tag effect="plain" size="default" :type="scope.row.skuIfExist === 1 ? 'success' : 'error'">
              {{ scope.row.skuIfExist === 1 ? '有效' : '失效' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="加入时间" prop="createTime" align="center" >
          <template v-slot="scope">
            <div>{{ parseTime(scope.row.createTime, '') }}</div>
          </template>
        </el-table-column>
        <!--      <el-table-column label="操作"  class-name="small-padding fixed-width">-->
        <!--        <template slot-scope="scope">-->
        <!--          <el-button-->
        <!--            -->
        <!--            text type="primary"-->
        <!--            icon="Edit"-->
        <!--            @click="handleUpdate(scope.row)"-->
        <!--            v-hasPermi="['ums:memberCart:edit']"-->
        <!--          >修改</el-button>-->
        <!--          <el-button-->
        <!--            -->
        <!--            text type="primary"-->
        <!--            icon="Delete"-->
        <!--            @click="handleDelete(scope.row)"-->
        <!--            v-hasPermi="['ums:memberCart:remove']"-->
        <!--          >删除</el-button>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
      </el-table>

      <pagination v-if="total" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script>
import {
  addUmsMemberCart,
  delUmsMemberCart,
  exportUmsMemberCart,
  getUmsMemberCart,
  listUmsMemberCart,
  updateUmsMemberCart
} from '@/api/ums/memberCart';

export default {
  name: 'UmsMemberCart',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物车表格数据
      umsMemberCartList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        productName: null,
        phone: null
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    const { phone } = this.$route.query;
    if (phone) {
      this.queryParams.phone = phone;
    }
    this.getList();
  },
  methods: {
    /** 查询购物车列表 */
    getList() {
      this.loading = true;
      const { current: pageNum, size: pageSize } = this.queryParams;
      const query = { ...this.queryParams, pageNum: undefined, pageSize: undefined };
      const pageReq = { current: pageNum - 1, size: pageSize };
      listUmsMemberCart(query, pageReq).then((response) => {
        const { records = [], total = 0 } = response.data || {};
        this.umsMemberCartList = records;
        this.total = total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: 0,
        memberId: null,
        productId: null,
        pic: null,
        skuId: null,
        productName: null,
        spData: null,
        quantity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加购物车';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUmsMemberCart(id).then((response) => {
        this.form = response;
        this.open = true;
        this.title = '修改购物车';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUmsMemberCart(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addUmsMemberCart(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除购物车编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUmsMemberCart(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm('是否确认导出所有购物车数据项？')
        .then(() => {
          this.exportLoading = true;
          return exportUmsMemberCart(queryParams);
        })
        .then((response) => {
          this.$download.download(response);
          this.exportLoading = false;
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="scss">
.product-container {
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 180px;
  .product-item {
    margin: auto;
    width: 180px;
    .sp-data {
      font-size: 13px;
    }
    .quantity {
      font-weight: bold;
      font-size: 13px;
    }
  }
}
.el-table .my-cell {
  vertical-align: top;
}
</style>
