<template>
  <div class="app-container">
    <el-card>
      <el-table 
      border 
      stripe  
      v-loading="loading" 
      ref="multipleTable" 
      :data="disposalList" 
      default-expand-all
      row-key="id"
      :tree-props="{ children: 'child' }"
      >
        <el-table-column label="类别编号" align="center" width="150" prop="num" />
        <el-table-column label="类别名称" align="center">
          <template slot-scope="{ row }">
            <span :style="{ paddingLeft: ((row.level || 0) * 16) + 'px', display: 'inline-block' }">
              {{ row.categoryName || row.label || row.name || "—" }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-add"
              @click="handleAddSuperior(scope.row)"
            >添加上级</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加或修改死淘处理方式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="isView" label-width="100px">
        <el-form-item label="上级" prop="parentId">
          <el-select v-model="form.parentId" placeholder="请选择上级">
            <el-option
              v-for="item in goodsList"
              :key="item.id"
              :label="item.categoryName"
              :value="item.id"
              :disabled="form && item.id === form.id"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="处理方式" prop="dealWay">-->
        <!--          <el-input type="textarea" v-model="form.dealWay" maxlength="50" show-word-limit placeholder="请输入处理方式" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getGoodsTree, getGoodsById, editGoods, getGoodsList } from "@/api/baseInfo/goods";
export default {
  name: "GoodsChart",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      disposalList: [],
      // 原始树数据（用于递归扁平化）
      goodsTreeData: [],
      // 弹出层标题
      title: "",
      isView:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      },
      pickerOptions: {
        disabledDate(time) {
          // 获取当前日期
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 将时间设置为当天的 00:00:00

          // 禁用今天之后的日期
          return time.getTime() > today.getTime();
        }
      },
      // 表单参数
      form: {},
      rules: {},
      goodsList: [],
    }
  },
  watch: {
    "queryParams.timeRange": {
      handler(newVal) {
        if (newVal) {
          this.queryParams.startTime = newVal[0]
          this.queryParams.endTime = newVal[1]
        } else {
          this.queryParams.startTime = null
          this.queryParams.endTime = null
        }
      },
      deep: true
    },
  },
  created() {
    this.getList();
    this.getGoodsList();
  },
  methods: {
    goNext(event, nextRef) {
      event.preventDefault();
      this.$refs[nextRef].focus();
    },
    getGoodsList() {
      getGoodsList({}).then(res => {
        const list = res.data || [];
        // 顶级节点固定置顶
        this.goodsList = [{ id: 0, categoryName: "顶级" }, ...list];
      })
    },
    /** 查询检验项目列表 */
    getList() {
      getGoodsTree({}).then(res => {
          this.disposalList = res.data
          this.loading = false;
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  
    handleAddSuperior(row) {
      let params = {
        id: row.id
      }
      getGoodsById(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = false;
        this.title = "添加上级";
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        // dealReason: null,
        // dealWay: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          editGoods(this.form).then(res => {
            this.$modal.msgSuccess("绑定成功");
            this.open = false;
            this.getList();
          })
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">

</style>

