<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="请输入标题"
          :disabled="[3, 4].includes(dlgType)"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <SelectRemote
          :disabled="[3, 4].includes(dlgType)"
          url="/video/category/list"
          field="name"
          v-model="form.categoryId"
          :selectObj="selectObj"
          :searchParams="searchParams"
        />
      </el-form-item>
      <el-form-item label="封面图片" prop="picPath">
        <image-upload
          :limit="1"
          v-model="form.picPath"
          :disabled="[3, 4].includes(dlgType)"
        />
      </el-form-item>
      <el-form-item label="视频" prop="videoPath">
        <VideoUpload
          :limit="1"
          v-model="form.videoPath"
          :disabled="[3, 4].includes(dlgType)"
        />
      </el-form-item>
      <template v-if="dlgType === 3">
        <el-form-item label="状态" prop="checkStatus">
          <el-select
            v-model="form.checkStatus"
            placeholder="请选择状态"
            style="width: 100%"
            disabled
          >
            <el-option
              v-for="dict in dict.type.check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
      <template v-if="[3, 4].includes(dlgType)">
        <el-form-item label="审核结果" prop="checkResult">
          <el-select
            v-model="form.checkResult"
            placeholder="请选择审核结果"
            style="width: 100%"
            :disabled="dlgType === 3"
          >
            <el-option
              v-for="dict in dict.type.check_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="4"
            v-model="form.remark"
            placeholder="审核不通过时必填"
            :disabled="dlgType === 3"
          />
        </el-form-item>
      </template>
    </el-form>
    <div style="text-align: right" v-if="dlgType !== 3">
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
    <div style="text-align: right" v-else>
      <el-button @click="handleCancel">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import SelectRemote from "@/components/SelectRemote";
import VideoUpload from "@/components/VideoUpload";
export default {
  components: { SelectRemote, VideoUpload },
  dicts: ["check_result", "check_status"],
  props: {
    data: {
      type: Object,
      default: () => {},
    },
    loading: {
      type: Boolean,
      default: false,
    },
    dlgType: {
      // 弹窗类型  1 - 新增   2 - 修改   3 - 查看   4 - 审核
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      // 选中的资讯标题
      selectObj: {},
      // 远程搜索框的查询条件
      searchParams: {
        status: "0",
      },
      form: {
        title: "",
        categoryId: "",
        picPath: "",
        videoPath: "",
        content: "",
        checkStatus: "1", // 审核状态，默认草稿
        checkResult: "",
        remark: "",
      },
      rules: {
        title: [{ required: true, trigger: "blur", message: "请输入标题" }],
        categoryId: [
          { required: true, trigger: "change", message: "请选择分类" },
        ],
        picPath: [{ required: true, trigger: "blur", message: "请上传封面图" }],
        videoPath: [
          { required: true, trigger: "blur", message: "请上传封面图" },
        ],
        content: [
          { required: true, trigger: "blur", message: "请输入正文内容" },
        ],
      },
    };
  },
  watch: {
    data: {
      handler(newVal) {
        if (newVal) {
          this.form = {
            id: newVal.id || "",
            title: newVal.title || "",
            categoryId: newVal.categoryId || "",
            picPath: newVal.picPath || "",
            videoPath: newVal.videoPath || "",
            content: newVal.content || "",
            checkStatus: newVal.checkStatus || "1",
            checkResult: newVal.checkResult || "",
            remark: newVal.remark || "",
          };
          this.selectObj = {
            id: newVal.categoryId || "",
            selectLabel: newVal.categoryName || "",
          };
          this.$nextTick(() => {
            this.$refs.form && this.$refs.form.clearValidate();
          });
        }
      },
    },
  },
  methods: {
    // 确定
    handleConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.checkResult == "1" && !this.form.remark)
            return this.$modal.msgError("请填写审核不通过的原因");
          if (this.dlgType === 4) this.form.checkStatus = "3";
          this.$emit("confirm", this.form);
        }
      });
    },
    // 取消
    handleCancel() {
      this.form = {
        title: "",
        categoryId: "",
        picPath: "",
        videoPath: "",
        content: "",
        checkStatus: "1", // 审核状态，默认草稿
        checkResult: "",
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
      this.$emit("cancel");
    },
  },
};
</script>

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