<template>
  <div>
    <!-- 搜索区域 -->
    <div style="margin: 10px 0">
#foreach ($column in $columns)
#if($column.isQuery == "1")
#set($javaField=$column.javaField)
#set($columnComment=$column.columnComment)
#if($column.htmlType == "input")
      <el-input style="width: 200px" placeholder="请输入${columnComment}" suffix-icon="el-icon-search" v-model="queryParams.${javaField}"></el-input>
#elseif($column.htmlType == "select" || $column.htmlType == "radio")
      <el-select v-model="queryParams.${javaField}" placeholder="请选择${columnComment}" style="width: 200px" class="ml-5">
        <el-option
          v-for="item in _dict.getDictDataByType('${column.dictType}')"
          :key="item.value"
          :label="item.name"
          :value="item.value">
        </el-option>
      </el-select>
#elseif($column.htmlType == "datetime")
      <el-date-picker style="width: 200px" v-model="queryParams.${javaField}" type="date" value-format="yyyy-MM-dd" placeholder="选择${columnComment}"></el-date-picker>
#end
#end
#end
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <!-- 操作按钮区域 -->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>

    <!-- 表格区域 -->
    <el-table :data="${businessName}List" border stripe :header-cell-class-name="'headerBg'" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="${pkColumn.javaField}" label="ID" width="80"></el-table-column>
#foreach ($column in $columns)
#if($column.isList == "1")
#set($javaField=$column.javaField)
#set($columnComment=$column.columnComment)
#if($column.htmlType == "datetime")
      <el-table-column prop="${javaField}" label="${columnComment}" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.${javaField} }}</span>
        </template>
      </el-table-column>
#elseif($column.htmlType == "imageUpload")
      <el-table-column prop="${javaField}" label="${columnComment}" align="center" width="100">
        <template slot-scope="scope">
          <el-image 
            style="width: 50px; height: 50px"
            :src="scope.row.${javaField}"
            :preview-src-list="[scope.row.${javaField}]">
          </el-image>
        </template>
      </el-table-column>
#elseif($column.htmlType == "select" || $column.htmlType == "radio")
      <el-table-column prop="${javaField}" label="${columnComment}" align="center">
        <template slot-scope="scope">
          {{ _dict.getDictLabel('${column.dictType}', scope.row.${javaField}) }}
        </template>
      </el-table-column>
#else
      <el-table-column prop="${javaField}" label="${columnComment}" align="center"></el-table-column>
#end
#end
#end
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.${pkColumn.javaField})"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="form.${pkColumn.javaField} ? '编辑' : '新增'" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px" size="small">
#foreach ($column in $columns)
#if($column.isEdit == "1")
#set($javaField=$column.javaField)
#set($columnComment=$column.columnComment)
        <el-form-item label="${columnComment}" prop="${javaField}"#if($column.isRequired == "1") :rules="[{ required: true, message: '${columnComment}不能为空', trigger: #if($column.htmlType == "select")'change'#else'blur'#end }]"#end>
#if($column.htmlType == "input")
          <el-input v-model="form.${javaField}" placeholder="请输入${columnComment}"></el-input>
#elseif($column.htmlType == "textarea")
          <el-input v-model="form.${javaField}" type="textarea" placeholder="请输入${columnComment}"></el-input>
#elseif($column.htmlType == "select" || $column.htmlType == "radio")
          <el-select v-model="form.${javaField}" placeholder="请选择${columnComment}">
            <el-option
              v-for="item in _dict.getDictDataByType('${column.dictType}')"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
#elseif($column.htmlType == "datetime")
          <el-date-picker v-model="form.${javaField}" type="date" value-format="yyyy-MM-dd" placeholder="选择${columnComment}"></el-date-picker>
#elseif($column.htmlType == "imageUpload")
          <el-upload
            class="avatar-uploader"
            action="/file/uploadImg"
            :show-file-list="false"
            :key="uploadKey"
            :on-success="imgSuccess">
            <img v-if="form.${javaField}" :src="form.${javaField}" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
#elseif($column.htmlType == "fileUpload")
          <el-upload
            class="upload-demo"
            action="/file/upload"
            :key="uploadKey"
            :on-success="fileSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
#end
        </el-form-item>
#end
#end
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "${BusinessName}",
  data() {
    return {
      total: 0,
      newsList:[],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
#foreach ($column in $columns)
#if($column.isQuery == "1")
        $column.javaField: undefined#if($foreach.count != $columns.size()),#end

#end
#end
      },
      form: {},
      dialogFormVisible: false,
      multipleSelection: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/${moduleName}/${businessName}/page", {
        params: this.queryParams
      }).then(res => {
        this.${businessName}List = res.data.records
        this.total = res.data.total        
      })
    },
    save() {
      this.request.post("/${moduleName}/${businessName}", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },
    del(${pkColumn.javaField}) {
      this.request.delete("/${moduleName}/${businessName}/" + ${pkColumn.javaField}).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.${pkColumn.javaField})
      this.request.delete("/${moduleName}/${businessName}/" + ids.join(",")).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.queryParams.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.queryParams.pageNum = pageNum
      this.load()
    },
    imgSuccess(res) {
      if (res && res.data && res.data[0] && res.data[0].url) {
        this.form.newsPhoto = res.data[0].url;
        this.uploadKey += 1;
      } else {
        this.$message.error('上传失败，请重试');
      }
    },
    fileSuccess(res) {
      if (res) {
        this.form.newsPhoto = res;
        this.uploadKey += 1;
      } else {
        this.$message.error('上传失败，请重试');
      }
    },
  }
}
</script>

<style>
.headerBg {
  background: #eee!important;
}
</style>