<template>
  <div class="app-container">
    <h4>基础服务</h4>
    <div class="table">
      <el-table ref="noticeTable" :data="basicsList" element-loading-text="Loading" border fit highlight-current-row stripe>
        <el-table-column align="center" label="名称" prop="name"></el-table-column>
        <el-table-column align="center" label="图片" prop="imageUrl">
          <template slot-scope="scope">
            <el-image style="width: 40px; height: 40px; display: block; margin: 0 auto;" :src="scope.row.imageUrl" :preview-src-list="[scope.row.imageUrl]" fit="cover">
              <div slot="error" style="height: 100%;font-size: 40px;">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" class-name="status-col">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '开启' : '禁用'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createTime"></el-table-column>
        <el-table-column align="center" label="操作" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="addOrEdit(scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增编辑 -->
    <el-dialog title="编辑" :visible.sync="addFormVisible" :show-close="false" width="50%" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="left" label-width="80px">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="addForm.name" autocomplete="off" placeholder="请输入品牌名称"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            :action="baseURL + 'common/upload'"
            :headers="myHeaders"
            :show-file-list="false"
            :on-success="uploadSuccess"
            :before-upload="beforeUpload">
            <img v-if="addForm.imgUrl" :src="addForm.imgUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="addForm.status">
            <el-radio :label="true">开启</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddForm">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { getHomeList, getHomeDetail, editHome } from "@/api/miniapp";

export default {
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      myHeaders: {'x-token': getToken()},
      basicsList: null, // 基础服务列表

      editId:  null,
      addFormVisible: false,
      addForm: {
        name: '', // 名称
        imgUrl: '', // 图片
        status: true,
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        imgUrl: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ],
      },
    }
  },

  created() {
    this.getList();
  },

  methods: {
    getList() {
      getHomeList().then(res => {
        this.basicsList = res.data;
      })
    },

    // 新增编辑
    addOrEdit(id) {
      this.editId = id;
      getHomeDetail({id}).then(res => {
        this.addForm = {
          name: res.data.name,
          imgUrl: res.data.imageUrl,
          status: res.data.status,
        }
        this.addFormVisible = true;
      })
    },

    // 取消 新增编辑
    cancelAddForm(){
      this.addFormVisible = false;
      this.$refs.addForm.resetFields();
    },

    // 提交 新增编辑
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let params = {
            id: this.editId,
            name: this.addForm.name,
            imageUrl: this.addForm.imgUrl,
            status: this.addForm.status,
          }
          editHome(params).then(res => {
            this.cancelAddForm();
            this.getList();
            this.$successMsg('编辑成功');
          })
        }
      })
    },

    uploadSuccess(res, file) {
      this.addForm.imgUrl = res.data.url;
    },

    beforeUpload(file) {
      const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
      const whiteList = ['jpg', 'jpeg', 'png'];
      if (whiteList.indexOf(fileSuffix) === -1) {
        this.$errorMsg('只支持上传jpg/png文件!');
        return false;
      }
    },
  }
}
</script>

<style scoped lang="scss">

</style>