<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog v-drag-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align:left;"
    @close="closeDialog"
    width="500px"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="类型" prop="type">
            <el-select v-model="formModel.type" placeholder="请选择" style="width:300px">
                <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="url" prop="url">
          <el-input v-model="formModel.url" placeholder="请输入url" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input  v-model="formModel.title"  placeholder="请输入标题"  style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="value">
          <el-input  v-model="formModel.value" type="textarea" :rows="3"  placeholder="请输入内容"  style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
           <el-date-picker
           style="width:300px"
            v-model="formModel.startTime"
            type="datetime"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
            style="width:300px"
            v-model="formModel.endTime"
            type="datetime"
            placeholder="选择结束时间"
            value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="formModel.status" placeholder="请选择" style="width:300px">
                <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input  v-model="formModel.remark" type="textarea" :rows="3"  placeholder="请输入备注"  style="width:300px"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import tipsApi from "@/api/sys/tips";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      showPwd: true,
      ruleValidate: {
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "url不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        value: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      },
      typeList: [{
        "label":"头部标题",
        "value":1
      },{
        "label":"弹窗",
        "value":2
      }],
      statusList: [{
        "label":"上架",
        "value":"1"
      },{
        "label":"下架",
        "value":"2"
      }],
      formModel: {},
      showDialog: true,
      loading: false,
      submitting: false,
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate(valid => {
        if (valid) {
          (function() {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return tipsApi.add(self.formModel);
            } else {
              return tipsApi.update(self.formModel);
            }
          })().then(function(response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success"
              });

              self.$emit("close",true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning"
              });

              self.$emit("close", false);
            }
          });
        }
      });
    }
  },
  async mounted() {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return tipsApi.create();
      } else {
        return tipsApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;

        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    // "el-select-tree": SelectTree
  }
};
</script>