<template>
  <div class="app-container">
    <!-- 详情视图 -->
    <div v-if="!editing" v-loading="loading" class="detail-view">
      <el-card>
        <div slot="header" class="clearfix">
          <span>介绍详情</span>
          <el-button
            type="primary"
            size="mini"
            @click="enterEdit"
            v-hasPermi="['homepage:introduction:edit']"
            >编辑</el-button
          >
        </div>
        <el-form ref="detailForm" :model="form" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="标题">
                <span class="detail-value">{{ form.title || "未设置" }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="简介">
                <span class="detail-value">{{ form.summary || "未设置" }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片">
                <div v-if="form.images && form.images.length">
                  <img
                    v-for="(img, index) in form.images.split(',')"
                    :key="index"
                    :src="img"
                    style="
                      max-width: 200px;
                      max-height: 150px;
                      margin-right: 10px;
                    "
                  />
                </div>
                <span v-else class="detail-value">未设置</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="状态">
                <el-tag :type="form.status === '0' ? 'success' : 'danger'">
                  {{
                    form.status
                      ? dict.type.sys_normal_disable.find(
                          (item) => item.value === form.status
                        ).label
                      : "未知"
                  }}
                </el-tag>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>

    <!-- 编辑视图 -->
    <el-form
      v-else
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      v-loading="loading"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="简介" prop="summary">
            <el-input
              v-model="form.summary"
              type="textarea"
              placeholder="请输入简介"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片" prop="images">
            <image-upload v-model="form.images" :multiple="true" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm"
          v-hasPermi="['homepage:introduction:edit']"
          >保 存</el-button
        >
        <el-button @click="cancelEdit">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getIntroduction,
  addIntroduction,
  updateIntroduction,
} from "@/api/homepage/introduction";

export default {
  name: "Introduction",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      loading: true,
      editing: false,
      form: {
        introId: undefined,
        title: undefined,
        summary: undefined,
        images: undefined,
        status: "0",
      },
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        summary: [{ required: true, message: "简介不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getIntroductionData();
  },
  methods: {
    getIntroductionData() {
      this.loading = true;
      getIntroduction()
        .then((response) => {
          if (response.data) {
            this.form = response.data;
          } else {
            this.form = {
              introId: undefined,
              title: undefined,
              summary: undefined,
              images: undefined,
              status: "0",
            };
          }
          this.loading = false;
        })
        .catch(() => {
          this.form = {
            introId: undefined,
            title: undefined,
            summary: undefined,
            images: undefined,
            status: "0",
          };
          this.loading = false;
        });
    },
    enterEdit() {
      this.editing = true;
    },
    cancelEdit() {
      this.editing = false;
    },
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.introId != undefined) {
            updateIntroduction(this.form).then(() => {
              this.$modal.msgSuccess("保存成功");
              this.getIntroductionData();
              this.editing = false;
            });
          } else {
            addIntroduction(this.form).then(() => {
              this.$modal.msgSuccess("保存成功");
              this.getIntroductionData();
              this.editing = false;
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.detail-value {
  color: #606266;
  line-height: 32px;
}
.clearfix {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  &::after {
    display: none;
  }
}
</style>
