<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" inline label-width="100px">
            <el-form-item label="商品分类：">
              <el-cascader
                ref="cascader"
                v-model="searchInfo.categoryId"
                @change="searchList"
                clearable
                :options="adminProductClassify"
                :props="categoryProps"
              />
            </el-form-item>
            <el-form-item label="商户类别：">
              <el-select
                v-model="searchInfo.isSelf"
                clearable
                placeholder="请选择"
                @change="searchList"
              >
                <el-option label="自营" :value="1" />
                <el-option label="非自营" :value="0" />
              </el-select>
            </el-form-item>
            <el-form-item label="商户名称：" v-hasPermi="['platform:merchant:page:list']">
              <merchant-name @getMerId="getMerId" ref="merchantName"></merchant-name>
            </el-form-item>
            <el-form-item label="商品搜索：" class="width100">
              <el-input
                v-model.trim="searchInfo.keywords"
                @keyup.enter.native="searchList"
                placeholder="请输入商品名称、关键字"
                class="selWidth"
                size="small"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1)"
                  v-hasPermi="['platform:finance:merchant:closing:page:list']"
                />
              </el-input>
              <el-button size="small" type="primary" style="margin-left: 30px;" @click="searchList">查询</el-button>
              <el-button size="small" plain @click="onReset">重置</el-button>
            </el-form-item>
            <br />
          </el-form>
        </div>
      </div>
      <el-table v-loading="listLoading" tooltip-effect="dark" :data="goodsList" style="width: 100%" class="table">
        <el-table-column prop="id" label="ID" min-width="60" />
        <el-table-column label="商品图" align="center">
          <template slot-scope="scope">
            <div>
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.image"
                :preview-src-list="[scope.row.image]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" min-width="120" align="center" />
        <el-table-column prop="price" label="商品售价" min-width="120" align="center" />
        <el-table-column prop="merchantName" label="商户名称" min-width="150" align="center" />
        <el-table-column prop="merName" label="分账设置" min-width="100" align="center" v-if="false">
          <template slot-scope="scope">
            <div>{{ scope.row.feeConfig && scope.row.feeConfig.level === 1 ? '默认' : '自定义' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="分账规则" min-width="180" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.feeConfig">
              <div>
                <span>售卖分账优先级：{{ scope.row.feeConfig.sellFeePriority === 0 ? '平台' : '商户' }}，</span>
                <span>售卖平台服务费：{{ scope.row.feeConfig.sellFee }}{{ scope.row.feeConfig.sellFeeType === 0 ? '%' : '元' }}</span>
              </div>
            </div>
            <div v-else>--</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="openInstallmentSettingDialog(scope.row)"
              >分账设置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="limit"
          :current-page="page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <installmentSettingDialog :showInstallmentSettingDialog="showInstallmentSettingDialog" :goodsId="goodsId" :merId="merId" :feeConfig="feeConfig" @update="getList" @closeInstallmentSettingDialog="closeInstallmentSettingDialog" />
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  getServiceChargeGoodsApi
} from '@/api/finance';
import merchantName from '@/components/merchantName';
import { checkPermi } from '@/utils/permission'; // 权限判断函数
import { mapGetters } from 'vuex';
import installmentSettingDialog from '../components/installmentSettingDialog';

export default {
  name: 'installmentSetting',
  data() {
    return {
      listLoading: true,
      showInstallmentSettingDialog: false,
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'childList',
        expandTrigger: 'hover',
        checkStrictly: false,
        emitPath: false,
        multiple: false,
      },
      searchInfo: {
        categoryId: null,
        isSelf: null,
        merId: null,
        keywords: ''
      },
      page: 1,
      limit: 20,
      total: 0,
      totalPage: null,
      goodsList: [],
      goodsId: null,
      merId: null,
      feeConfig: {}
    };
  },
  components: { merchantName, installmentSettingDialog },
  mounted() {
    if (!localStorage.getItem('adminProductClassify')) this.$store.dispatch('product/getAdminProductClassify');
    if (checkPermi(['platform:product:page:list'])) this.getList();
  },

  computed: {
    ...mapGetters(['adminProductClassify', 'frontDomain'])
  },

  methods: {
    checkPermi,

    getList() {
      let params = {
        page: this.page,
        limit: this.limit,
        type: 1
      };

      this.searchInfo.categoryId ? params.categoryId = this.searchInfo.categoryId : null;
      this.searchInfo.isSelf >= 0 ? params.isSelf = this.searchInfo.isSelf : null;
      this.searchInfo.merId ? params.merId = this.searchInfo.merId : null;
      this.searchInfo.keywords ? params.keywords = this.searchInfo.keywords : null;
      this.listLoading = true;

      getServiceChargeGoodsApi(params)
        .then((res) => {
          let { limit, list, page, total, totalPage } = res;
          list.forEach(el => {
            if (el.feeConfig) {
              el.feeConfig.sellFeeType === 0 ? el.feeConfig.sellFee = parseFloat(el.feeConfig.sellFee) * 100 : null;
            }
          });
          this.limit = limit;
          this.page = page;
          this.total = total;
          this.totalPage = totalPage;
          this.goodsList = list;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },

    searchList() {
      this.page = 1;
      this.getList();
    },

    getMerId(id) {
      this.searchInfo.merId = id;
      this.getList();
    },

    onReset() {
      this.searchInfo.categoryId = null;
      this.searchInfo.merId = null;
      this.searchInfo.isSelf = null;
      this.searchInfo.keywords = '';
      this.$refs.merchantName.merId = null;
    },

    openInstallmentSettingDialog(row) {
      this.goodsId = row.id;
      this.merId = row.merId;
      this.feeConfig = row.feeConfig;
      this.showInstallmentSettingDialog = true;
    },

    closeInstallmentSettingDialog() {
      this.showInstallmentSettingDialog = false;
    },

    pageChange(page) {
      this.page = page;
      this.getList('');
    },

    handleSizeChange(val) {
      console.log(val);
      this.limit = val;
      this.getList('');
    }
  },
};
</script>

<style lang="scss" scoped>
.demo-image__preview {
  width: 60px;
  height: 60px;
  .el-image {
    width: 100%;
    height: 100%;
  }
}
.title {
  font-size: 20px;
}
.wid100 {
  width: 100px;
}
.ml100 {
  margin-left: 100px;
}
.transferMinAmount {
  ::v-deep.el-form-item__content {
    line-height: normal;
  }
}
::v-deep .el-dialog__title {
  font-weight: bold;
}
.selWidth {
  width: 300px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.font-red {
  color: #ff4949;
}
.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
.pictrue {
  width: 60px;
  height: 60px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 10px;
  position: relative;
  cursor: pointer;
  img {
    width: 100%;
    height: 100%;
  }
}
.box-container {
  overflow: hidden;
  padding: 0 30px;
  font-size: 14px;
}

.box-container .list.image {
  margin: 20px 0;
  position: relative;
}
.box-container .list.image img {
  position: absolute;
  top: -20px;
}
.box-container .list {
  float: left;
  line-height: 40px;
}
.box-container .sp100 {
  width: 100%;
}

.acea-row {
  margin-bottom: 25px;
}
.pictures {
  width: 100%;
  height: 100%;
}
</style>
