<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
    :title="!dataForm.id ? '添加' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
      <el-form-item label="分类" prop="cid">
        <el-select v-model="dataForm.cid" placeholder="请选择" class="selWidthd">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="跳转地址" prop="jumpUrl">
        <el-input v-model="dataForm.jumpUrl" placeholder="跳转地址"></el-input>
      </el-form-item>
      <el-form-item label="是否无效" prop="status">
        <el-select v-model="dataForm.status">
          <el-option label="有效" :value="true"></el-option>
          <el-option label="无效" :value="false"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/systemHelp.js'
import { listCategroy } from "@/api/categoryApi";

export default {
  data() {
    return {
      visible: false,
      categoryList: [],
      dataForm: {
        id: 0,
        cid: '',
        title: '',
        content: '',
        jumpUrl: '',
        shareTitle: '',
        shareSynopsis: '',
        shareImage: '',
        status: '',
        sort: '',
      },
      dataRule: {
        cid: [
          {required: true, message: '分类  为必填项', trigger: 'blur'}
        ],
        title: [
          {required: true, message: '标题  为必填项', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '内容  为必填项', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态  为必填项', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '排序  为必填项', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(id) { // 初始化表单验证规则
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          api.detailApi(id).then(res => {
            this.dataForm = res;
          })
        }
        this.getCategoryList()
      })
    },
    // 表单数据提交
    dataSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.id ?
            api.updateApi(this.dataForm).then(res => {
              this.$message.success('修改成功');
            }) :
            api.createApi(this.dataForm).then(res => {
              this.$message.success('新增成功');
            });
          this.visible = false;
          this.$emit('refreshDataList');
        }
      })
    },
    getCategoryList() {
      const categroy = {
        limit: 999,
        page: '1',
        status: 1,
        type : 9
      };
      listCategroy(categroy).then(res => {
        this.categoryList = res.list;
      })
    },
  }
}
</script>
