<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="资讯标题" prop="infoId">
        <SelectRemote
          url="/info/infoContent/list"
          field="title"
          v-model="form.infoId"
          :selectObj="selectObj"
          :searchParams="searchParams"
        />
      </el-form-item>
      <el-form-item label="图片" prop="img">
        <image-upload :limit="1" v-model="form.img" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          style="width: 100%"
          v-model="form.sort"
          :min="1"
          :max="9999"
          label="请输入排序"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          style="width: 100%"
        >
          <el-option
            v-for="dict in statusOption"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align: right">
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import SelectRemote from "@/components/SelectRemote";
export default {
  components: { SelectRemote },
  props: {
    data: {
      type: Object,
      default: () => {},
    },
    loading: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 选中的资讯标题
      selectObj: {},
      // 远程搜索框的查询条件
      searchParams: {
        checkStatus: "5",
      },
      // 状态组
      statusOption: [
        { value: "0", label: "启用" },
        { value: "1", label: "停用" },
      ],
      form: {
        title: "",
        infoId: "",
        img: "",
        sort: "",
        status: "",
      },
      rules: {
        title: [{ required: true, trigger: "blur", message: "请输入标题" }],
        infoId: [
          { required: true, trigger: "change", message: "请选择资讯标题" },
        ],
        img: [{ required: true, trigger: "blur", message: "请上传图片" }],
        sort: [{ required: true, trigger: "blur", message: "请输入排序" }],
        status: [{ required: true, trigger: "change", message: "请选择状态" }],
      },
    };
  },
  watch: {
    data: {
      handler(newVal) {
        if (newVal) {
          this.form = {
            id: newVal.id || "",
            title: newVal.title || "",
            infoId: newVal.infoId || "",
            img: newVal.img || "",
            sort: newVal.sort || "",
            status: newVal.status || "",
          };
          this.selectObj = {
            id: newVal.infoId || "",
            selectLabel: newVal.infoTitle || "",
          };
          this.$nextTick(() => {
            this.$refs.form && this.$refs.form.clearValidate();
          });
        }
      },
    },
  },
  methods: {
    // 确定
    handleConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit("confirm", this.form);
        }
      });
    },
    // 取消
    handleCancel() {
      this.form = {
        title: "",
        infoId: "",
        img: "",
        sort: "",
        status: "",
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped></style>
