<template>
  <div class="data-info">
    <el-card>
      <el-form
        :model="dataForm"
        :rules="rules"
        ref="dataFormRef"
        label-width="120px"
      >
        <el-form-item label="项目名称:" prop="name" class="form-item-half">
          <el-input v-model="dataForm.name" placeholder="请输入项目名称"></el-input>
        </el-form-item>

        <el-form-item
          label="所属分类:"
          prop="category_id"
          class="form-item-half"
        >
          <el-select
            v-model="dataForm.category_id"
            placeholder="请选择所属分类"
            size="large"
          >
            <el-option
              v-for="item in allCategories"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="项目状态:" prop="status" class="form-item-half">
          <el-select
            v-model="dataForm.status"
            placeholder="请选择状态"
            size="large"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.status"
              :label="item.name"
              :value="item.status"
            />
          </el-select>
        </el-form-item>

        <!-- 项目介绍保持全宽 -->
        <el-form-item label="项目介绍:" prop="intro" class="form-item-half">
          <el-input
            rows="3"
            placeholder="请输入项目介绍"
            type="textarea"
            v-model="dataForm.intro"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <div style="text-align: right; margin-top: 20px">
      <el-button type="warning" @click="btnSave">确认保存</el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import {
  searchCategory,
  createProject,
  updateProject,
  getProjectInfo,
} from "@/api/project";
import { searchCustomer } from "@/api/customer";
import { searchDocument } from "@/api/document";
export default {
  components: {
    Tinymce,
  },
  emits: ["onClose"],
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    id: {
      handler(v) {
        let _this = this;
        if (v == null) return;
        if (v == 0) {
          this.dataForm = {
            id: 0,
            name: "",
            intro: "",
            customer_id: "",
            category_id: "",
            status: 5,
            /*  linkDoc:[], */
          };
          return;
        }
        _this.getInfo(v);
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      activeName: "base",
      currentCategory: [],
      allCategories: [],
      imageUrl: "",
      customerList: [],
      articleList: [],
      dataForm: {
        id: 0,
        name: "",
        intro: "",
        customer_id: "",
        category_id: "",
        status: 5,
        /*  linkDoc:[], */
      },
      statusOptions: [
        {
          status: 5,
          name: "启用",
        },
        {
          status: 6,
          name: "停用",
        },
      ],
      // 添加校验规则
      rules: {
        name: [
          { required: true, message: "请输入项目名称", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符之间",
            trigger: "blur",
          },
        ],
        /* intro: [
          { required: true, message: "请输入项目介绍", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "长度在 1 到 500 个字符之间",
            trigger: "blur",
          },
        ], */
        category_id: [
          { required: true, message: "请选择所属分类", trigger: "change" },
        ],
        status: [
          { required: true, message: "请选择项目状态", trigger: "change" },
        ],
      },
    };
  },

  mounted() {
    let _this = this;
    _this.uploadUrl = process.env.VUE_APP_BASE_API + "/upload";
    _this.initAllCategory();
    this.initCustomer();
    /*  this.initDocList(); */
  },
  methods: {
    initDocList() {
      let _this = this;
      searchDocument({
        page: 1,
        pageSize: 10,
      }).then((res) => {
        if (res.status != 200) return;
        _this.articleList = res.data.dataList;
      });
    },
    //图片上传成功
    onUploadSuccess(e) {
      console.log("onUploadSuccess", e);
    },
    //保存更新
    btnSave(e) {
      let _this = this;

      _this.$refs.dataFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }

        const savePromise =
          _this.dataForm.id > 0
            ? updateProject(_this.dataForm)
            : createProject(_this.dataForm);

        savePromise
          .then((res) => {
            if (res.status !== 200) return;
            _this.$message.success(
              _this.dataForm.id > 0 ? "项目信息更新成功" : "项目信息创建成功"
            );
            setTimeout(() => {
              _this.$emit("onClose", true);
            }, 0);
          })
          .catch((error) => {
            console.error("Save failed:", error);
          });
      });
    },

    //产品详情
    getInfo(id) {
      let _this = this;
      let par = {
        id: id,
      };
      getProjectInfo(par).then((res) => {
        if (!res) return;
        if (res.status != 200) return;
        _this.dataForm = res.data;
        _this.dataForm.category_id = res.data.categoryId;
        _this.dataForm.customer_id = res.data.customerId;
      });
    },

    onChangeStatus(e) {
      this.dataForm.status = e;
    },
    //修改分类信息
    onChangeCategory(e) {
      let _this = this;
      _this.dataForm.categoryId = e;
    },
    //初始化所有分类
    initAllCategory() {
      let _this = this;
      let par = {
        page: 1,
        pageSize: 9999,
        status: 5,
      };
      searchCategory(par).then((res) => {
        _this.allCategories = res.data.dataList;
      });
    },
    initCustomer() {
      let _this = this;
      let par = {
        page: 1,
        pageSize: 99,
      };
      searchCustomer(par).then((res) => {
        _this.customerList = res.data.dataList;
      });
    },
  },
};
</script>

<style lang="scss">
@import "./dataInfo.scss";
.data-info {
  // 添加半宽表单项的样式
  .form-item-half {
    width: 50%;

    // 确保 select 和 input 占满容器宽度
    .el-input,
    .el-select {
      width: 100%;
    }
  }

  // 可选：添加一些响应式布局
  @media screen and (max-width: 768px) {
    .form-item-half {
      width: 100%;
    }
  }
}
</style>
