<template>
  <div class="divBox">
    <div class="selCard">
      <el-form inline size="small" :model="tableFrom" ref="searchForm" label-width="85px">
        <el-form-item label="商户名称：" prop="mer_id">
          <el-select v-model="tableFrom.mer_id" clearable filterable placeholder="请选择" class="selWidth" @change="getList(1)">
            <el-option v-for="item in merSelect" :key="item.mer_id" :label="item.mer_name" :value="item.mer_id" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称：" prop="template_name">
          <el-input v-model="tableFrom.template_name" @keyup.enter.native="getList(1)" placeholder="请输入参数模板名称" class="selWidth" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)">搜索</el-button>
          <el-button size="small" @click="searchReset()">重置</el-button> 
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="small"
      >
        <el-table-column prop="template_id" label="ID" min-width="80" />
        <el-table-column prop="merchant.mer_name" label="商户名称" min-width="150" />
        <el-table-column prop="template_name" label="参数模板名称" min-width="150" />
        <el-table-column prop="sort" label="排序" min-width="100" />
        <el-table-column prop="create_time" label="创建时间" min-width="150" />
        <el-table-column label="操作" min-width="80" fixed="right">
          <template slot-scope="scope">  
            <el-button type="text" size="small" @click="onDetail(scope.row.template_id)">查看</el-button>
            <el-button type="text" size="small" @click="onCopy(scope.row.template_id)">复制</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--详情-->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="400px"
    >
      <div style="min-height: 500px;">
          <div class="description">
            <div class="acea-row">
              <div class="description-term title">
                <span class="name">参数名称</span>
                <span class="value">参数值</span>
              </div>
              <div v-for="(item, index) in specsInfo.parameter" :key="index" class="description-term">
                <span class="name">{{item.name}}</span>
                <span class="value">{{item.value}}</span>
              </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { roterPre } from '@/settings'
import {
  merSpecsList, merSelectApi, productSpecsInfo
} from "@/api/product";
export default {
  name: "MerSpecsList",
  data() {
    return {
      listLoading: true,
      merSelect: [],
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        mer_id: "",
        template_name: ""
      },
      specsInfo: {},
      dialogVisible: false,
      title: ""
    };
  },
  mounted() {
    this.getMerSelect();
    this.getList('');
  },
  methods: {  
    /**重置 */
    searchReset(){
      this.$refs.searchForm.resetFields()
      this.getList(1)
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      merSpecsList(this.tableFrom)
        .then((res) => {
          res.data.list.forEach((item,index)=>{
            item.cate_name= [];
            item.cateId.forEach((val,idx)=>{
              item.cate_name.push(val.category.cate_name)
            })
          })
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;  
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList('');
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList('');
    },
    // 商户列表；
    getMerSelect() {
      merSelectApi().then(res => {
        this.merSelect = res.data
      }).catch(res => {
        this.$message.error(res.message)
      })
    },
    // 编辑
    onCopy(id) {
      this.$router.push(`${roterPre}/product/specs/create/${id}?type=copy`);
    },
    // 查看
    onDetail(id) {
      productSpecsInfo(id)
        .then((res) => {
         this.specsInfo = res.data;
         this.title = res.data.template_name.length > 10 ? res.data.template_name.slice(0,10)+'...' : res.data.template_name;
         this.dialogVisible = true;
        })
        .catch((res) => {  
          this.$message.error(res.message);
        });
    },
  },
};
</script>

<style scoped lang="scss">

.description{
  padding-bottom: 15px;
  margin-top: 15px;
  &-term {
    display: table-cell;
    width: 100%;
    line-height: 40px;
    font-size: 13px;
    color: #333;
    border-bottom: 1px solid #DCDFE6;
    &.title{
      line-height: 40px;
      background-color: var(--prev-color-primary-light-8);
      border-radius: 4px;
      font-weight: bold;
    }
  }
  .name,.value{
    display: inline-block;
    padding: 0 20px;  
  }
  .name{
    width: 170px;
  }
  .value{
    width: 170px;
  }
}
</style>
