<template>
  <div class="app-container">
    <el-tabs v-model="activeNameL" type="card" @change="getType(activeNameL)">
      <el-tab-pane
        :key="all"
        :label="all"
        :name="all"
      >
      </el-tab-pane>
      <el-tab-pane
        :key="item.sysLeixingpick"
        v-for="(item,index) in editableTabs"
        :label="item.sysLeixingpick"
        :name="item.sysLeixingpick"
      >
      </el-tab-pane>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="产品编号" prop="sysSerial">
            <el-input
              v-model="queryParams.sysSerial"
              placeholder="请输入产品编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="产品名称" prop="sysIntro">
            <el-input
              v-model="queryParams.sysIntro"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="备注" prop="sysBeihzu">
            <el-input
              v-model="queryParams.sysBeihzu"
              placeholder="请输入备注"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="元/次" prop="sysNumber">
            <el-input
              v-model="queryParams.sysNumber"
              placeholder="请输入元/次"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>


        <el-row :gutter="10" >
          <el-empty description="暂无商品" v-if="apitypeList.length == 0"></el-empty>
          <el-col :span="8" v-for="customer in apitypeList" v-if="apitypeList.length != 0&&activeNameL=='全部类型'||customer.sysLeixingpick==activeNameL">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <el-image :src="customer.sysTypes" @click="gotoMaintained">
                  <div slot="placeholder" class="image-slot">
                    加载中<span class="dot">...</span>
                  </div>
                </el-image>
                <br>
                <span style="font-size: 18px;font-weight: 900;">{{customer.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           sysIntro}}</span>
                <span style="float: right;font-size: 18px;font-weight: 900;" >{{customer.sysNumber}}元/次</span>
              </div>
              <div >
                <el-descriptions class="margin-top"  :column="1" :size="size" border>
                  <el-descriptions-item>
                    <template slot="label">
                      <i class="el-icon-user"> </i>产品编号</template>
                    {{customer.sysSerial}}
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template slot="label">
                      <i class="el-icon-user"> </i>备注</template>
                    {{customer.sysBeihzu}}
                  </el-descriptions-item>
                </el-descriptions>
              </div>
            </el-card>


          </el-col>
        </el-row>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改接口对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="产品编号" prop="sysSerial">
              <el-input v-model="form.sysSerial" placeholder="请输入产品编号" />
            </el-form-item>
            <el-form-item label="简介" prop="sysIntro">
              <el-input v-model="form.sysIntro" placeholder="请输入简介" />
            </el-form-item>
            <el-form-item label="产品状态" prop="sysTypes">
              <el-input v-model="form.sysTypes" placeholder="请输入产品状态" />
            </el-form-item>
            <el-form-item label="备注" prop="sysBeihzu">
              <el-input v-model="form.sysBeihzu" placeholder="请输入备注" />
            </el-form-item>
            <el-form-item label="类型选择" prop="sysLeixingpick">
              <el-input v-model="form.sysLeixingpick" placeholder="请输入类型选择" />
            </el-form-item>
            <el-form-item label="次/元 时间" prop="sysNumber">
              <el-input v-model="form.sysNumber" placeholder="请输入次/元 时间" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
    </el-tabs>


  </div>
</template>

<script>
import { listApitype, getApitype, delApitype, addApitype, updateApitype } from "@/api/market/apitype";

export default {
  name: "Apitype",
  data() {
    return {
      all:"全部类型",
      activeNameL:"全部类型",
      editableTabs: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接口表格数据
      apitypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysSerial: null,
        sysIntro: null,
        sysTypes: null,
        sysBeihzu: null,
        sysLeixingpick: null,
        sysNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    gotoMaintained(){
      location="http://10.0.2.1/prod-api/market/tobemaintained/index";
    },
    getType(activeNameL){
      this.activeNameL=activeNameL
    },
    /** 查询接口列表 */
    getList() {
      this.loading = true;
      listApitype(this.queryParams).then(response => {
        console.log(response);
        if(200==response.code){
          this.apitypeList = response.data.rows;

          const uniqueSysLeixingpick = new Map();
          response.data.rows.forEach(item => {
            uniqueSysLeixingpick.set(item.sysLeixingpick, item);
          });

          // 将 Map 的值（即去重后的对象）转换回数组
          this.editableTabs = Array.from(uniqueSysLeixingpick.values()) ;

          this.total = response.data.total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sysId: null,
        sysSerial: null,
        sysIntro: null,
        sysTypes: null,
        sysBeihzu: null,
        sysLeixingpick: null,
        sysNumber: 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.sysId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接口";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sysId = row.sysId || this.ids
      getApitype(sysId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改接口";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sysId != null) {
            updateApitype(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApitype(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sysIds = row.sysId || this.ids;
      this.$modal.confirm('是否确认删除接口编号为"' + sysIds + '"的数据项？').then(function() {
        return delApitype(sysIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('market/apitype/export', {
        ...this.queryParams
      }, `apitype_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
