<template>
     <el-dialog title="技术资料详情" :visible.sync="open" width="50%" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" inline>
        <el-form-item label="类型" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入类型id" disabled/>
        </el-form-item>
        <el-form-item label="标题" prop="dataTitle">
          <el-input v-model="form.dataTitle" placeholder="请输入标题" disabled/>
        </el-form-item>
        <el-form-item label="作者" prop="dataAuthor">
          <el-input v-model="form.dataAuthor" placeholder="请输入作者" disabled/>
        </el-form-item>
        <el-form-item label="关键字" prop="dataKeyword">
          <el-input v-model="form.dataKeyword" placeholder="请输入关键字" disabled/>
        </el-form-item>
        <el-form-item label="机型" prop="dataModel">
          <el-input v-model="form.dataModel" placeholder="请输入机型" disabled/>
        </el-form-item>
        <el-form-item label="系统" prop="dataSys">
          <el-input v-model="form.dataSys" placeholder="请输入系统" disabled/>
        </el-form-item>
        <el-form-item label="专业" prop="dataSpeciality">
          <el-input v-model="form.dataSpeciality" placeholder="请输入专业" disabled/>
        </el-form-item>
        <el-form-item label="文件" prop="filePath">
          <file-upload v-model="form.filePath" :belongType="belongType" :belongId="belongId" disabled :isShowTip="false" />
        </el-form-item>
        <el-form-item label="文章数量" prop="dataNum">
          <el-input v-model="form.dataNum" placeholder="请输入文章数量" disabled/>
        </el-form-item>
        <el-form-item label="描述" prop="dataDescription">
          <el-input v-model="form.dataDescription" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <!---
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { listTechData, getTechData, delTechData, addTechData, updateTechData } from "@/api/tech/techData";
import FileUpload from "@/components/FileUpload/upload.vue";
import { listTechType, getTechType, delTechType, addTechType, updateTechType } from "@/api/tech/techType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "TechDialog",
  components: { FileUpload,Treeselect },
  props: ["showFlag",'belongId'],
  watch: {
    // 监听父组件传递过来的showFlag的变化,控制dialog的显示
    showFlag(newVal) {
      this.open = newVal; // Set the dialog's visibility based on showFlag
      this.handleUpdate(this.belongId)
      // this.getTypeList();
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技术资料表格数据
      techDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataTitle: null,
        dataAuthor: null,
        dataKeyword: null,
        dataModel: null,
        dataSys: null,
        dataSpeciality: null,
        dataNum: null,
        typeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [
          { required: true, message: "类型id不能为空", trigger: "blur" }
        ],
        dataTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
      },
      belongType: 'tech',
      belongId: null,
      techTypeList:[],
      defaultProps: {
          children: 'children',
          label: 'name'
        }
    };
  },
  mounted() {
    // this.getList();
    // this.getTypeList();
  },
  methods: {
    getTypeList() {
      listTechType(this.queryParams).then(response => {
        this.techTypeList = response.rows;

        this.techDataList.forEach(element => {
          if(element.id == this.form.typeId) {
            this.form.typeId = element.name
          }
        });
      });
    },
    handleNodeClick(data) {
      this.queryParams.typeId = data.id
      this.queryParams.pageSize = 10
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 查询技术资料列表 */
    getList() {
      this.loading = true;
      listTechData(this.queryParams).then(response => {
        this.techDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      // this.open = false;
      this.belongId = null;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeId: null,
        dataTitle: null,
        dataAuthor: null,
        dataKeyword: null,
        dataModel: null,
        dataSys: null,
        dataSpeciality: null,
        filePath: null,
        dataNum: null,
        dataDescription: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技术资料";
    },
    /** 修改按钮操作 */
    async handleUpdate(id) {
  this.reset(); // 重置表单
  try {
    // 获取技术数据
    const responseData = await getTechData(id);
    const data = responseData.data;
    console.log(data);

    // 获取技术类型列表
    const typeResponse = await listTechType();
    this.techTypeList = typeResponse.rows;

    // 通过 typeId 查找对应的名称
    const matchedType = this.techTypeList.find(element => element.id === data.typeId);
    if (matchedType) {
      // 将 typeId 设置为对应的名称
      data.typeId = matchedType.name;
    }

    // 更新表单数据
    this.form = { ...data };  // 使用浅拷贝来避免引用问题
    console.log(this.form); // 观察更新后的表单数据

  } catch (error) {
    console.error("Error fetching data:", error);
    // 处理错误情况
  }
},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTechData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除技术资料编号为"' + ids + '"的数据项？').then(function() {
        return delTechData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tech/techData/export', {
        ...this.queryParams
      }, `techData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
