<!-- 模板区域 -->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="findpage"></right-toolbar>
    </el-row>
    <!-- 模板内容 -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column align="center" prop="typeName" label="奖品类型名称"></el-table-column>
      <el-table-column align="center" prop="typeAttr" label="奖品字段属性">
        <template slot-scope="scope">
          <span>{{ scope.row.typeAttr }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="text" size="small" style="color: red;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <pagination :total="pagetotal" :page.sync="pageRequest.pageNum" :limit.sync="pageRequest.pageSize"
      @pagination="MarketList" /> -->
      <pagination
      v-show="pagetotal>0"
      :total="pagetotal"
      :page.sync="pageRequest.pageNum"
      :limit.sync="pageRequest.pageSize"
      @pagination="findpage"
    />
    <el-dialog :title="dilogtitle" :visible.sync="dialogVisible" width="1000px">
      <el-form class="demo-ruleForm wrap-scrollbar scroll-style" :model="attributionform" ref="attributionform"
        label-width="130px" size="small">
        <el-row>
          <el-col :span="10">
            <el-form-item label="奖品分类标题：">
              <el-input v-model="attributionform.typeName" placeholder="分类标题"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div>
              <h4>配置参数</h4>
            </div>
          </el-col>
          <el-col v-for="(configureitem, configureindex) in attributionform.typeAttr" :key="configureindex">
            <el-col :span="10">
              <el-form-item label="字段名：">
                <el-input v-model="configureitem.attrKey" class="widths1" placeholder="请输入字段名"></el-input>
              </el-form-item>
              <el-form-item label="标签：">
                <el-input v-model="configureitem.attrName" class="widths1" placeholder="标签名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="表单类型：">
                <el-radio-group v-model="configureitem.type">
                  <el-radio label="input"></el-radio>
                  <el-radio label="select"></el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button @click.prevent="removeDomain(configureitem)" size="small">删除</el-button>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="configuration">新增配置</el-button>
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<!-- 行为区域 -->
<script>
import {
  addAwardType,
  updateAward,
  getAwardTypeList,
  delAwardType
} from "@/api/award/awardList"

export default {
  // 数据存放区域
  data() {
    return {
      dilogtitle:"新增",
      dialogVisible: false,
      pagetotal: 0,
      pageRequest: {
        pageNum: 1,
        pageSize: 20,
      },
      showSearch: true,
      loading: false,
      tableData: [],
      attributionform: {
        typeAttr: [
          {
            attrKey: "",
            atrName: "",
            type: "",
            value: "",
          },
        ],
        typeName: "", //分类标题
      },
    }
  },
  // 生命周期函数
  created() {
    this.findpage()
  },
  // 方法函数
  methods: {
    // 删除
    handleDelete(row) {
      const awardName = row.typeName
      this.$confirm('是否确认名称为"' + awardName + '"的奖品类型？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delAwardType({id: row.id}).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg)
              this.findpage()
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(() => {
          this.$message.info('已取消删除')
        });
    },
    // 编辑
    handleEdit(row) {
      this.attributionform = row
      this.attributionform.typeAttr = row.typeAttr
      this.dilogtitle = '编辑'
      this.dialogVisible = true
    },
    handleAdd() {
      this.dilogtitle = '新增'
      this.dialogVisible = true
    },
    submitForm() {
      if (this.attributionform.id) {
        updateAward(this.attributionform).then((res) => {
          if (res.code == "200") {
            this.$message({ message: "修改成功", type: "success" });
            this.findpage();
            this.handleClose()
          } else {
            this.$message({ message: res.msg, type: "success" });
          }
        });
      } else {  
        addAwardType(this.attributionform).then((res) => {
          if (res.code == "200") {
            this.$message({ message: "新增成功", type: "success" });
            this.findpage();
            this.handleClose()
          } else {
            this.$message({ message: res.msg, type: "success" });
          }
        });
      }
    },
    findpage(){
      getAwardTypeList(this.pageRequest).then((res) => {
        this.tableData = res.data.records
        this.pagetotal = res.data.total
        this.pageRequest.pageNum = res.data.current
        this.pageRequest.pageSize = res.data.size
      })
    },
    handleClose() {
      this.attributionform = {
        typeAttr: [
          {
            attrKey: "",
            atrName: "",
            type: "",
            value: "",
          },
        ],
        typeName: "",
      }
      this.dialogVisible = false
    },
    removeDomain(item) {
      var index = this.attributionform.typeAttr.indexOf(item);
      if (index !== -1) {
        this.attributionform.typeAttr.splice(index, 1);
      }
    },
    //新增配置按钮
    configuration() {
      this.attributionform.typeAttr.push({
        attrKey: "",
        atrName: "",
        type: "",
        value: "",
      });
    },
  },
  // 计算属性
  computed: {
  }
}
</script>
<!-- 样式区域 -->
<style lang='scss' scoped></style>
