<template>
  <basic-container>
    <avue-crud
      v-model="form"
      :option="option"
      :data="data"
      :table-loading="loading"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @row-del="rowDel"
    >
      <template slot="icon" slot-scope="scope">
        <i :class="scope.row.icon" style="font-size: 24px"></i>
      </template>
      <template slot="menu" slot-scope="{ row, size, type }">
        <el-button v-if="row.level !== 4" :size="size" :type="type" @click="handleAdd(row)">
          新增子级
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import { getList, add, update, remove } from "@/api/basicResource/subjectSetting";

export default {
  data() {
    return {
      loading: false,
      parentId: undefined,
      form: {},
      data: [],
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        index: true,
        border: true,
        searchLabelWidth: 140,
        labelWidth: 160,
        menuWidth: 220,
        dialogClickModal: false,
        dialogWidth: 500,
        dialogType: "drawer",
        rowKey: "id",
        rowParentKey: "parentId",
        defaultExpandAll: true,
        tree: true,
        column: [
          {
            label: "上级科目",
            prop: "parentId",
            type: "tree",
            dicData: [],
            span: 24,
            hide: true,
            addDisabled: false,
            props: {
              value: "id",
              label: "name"
            },
            rules: [
              {
                required: false,
                message: "请选择上级菜单",
                trigger: "click"
              }
            ]
          },
          {
            label: "科目名称",
            prop: "name",
            type: "input",
            span: 24,
            minWidth: 160,
            search: true,
            showOverflowTooltip: true,
            rules: [
              {
                required: true,
                message: "请输入科目名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "科目代码",
            prop: "code",
            type: "input",
            span: 24,
            search: true,
            width: 120,
            align: 'center',
            showOverflowTooltip: true,
            rules: [
              {
                required: true,
                message: "请输入科目代码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "会计科目",
            prop: "accSubject",
            type: "textarea",
            span: 24,
            minWidth: 160,
            display: false,
            showOverflowTooltip: true,
          },
          {
            label: "高新研发费用科目",
            prop: "rdSubject",
            type: "textarea",
            span: 24,
            minWidth: 160,
            display: false,
            showOverflowTooltip: true,
          },
          {
            label: "高新研发费用科目",
            prop: "rdSubjectEnable",
            type: "radio",
            dicData:[{
              label: '是',
              value: 1
            },{
              label: '否',
              value: 0
            }],
            hide: true,
            span: 24,
            showOverflowTooltip: true,
          },
          {
            label: "加计扣除研发费用科目",
            prop: "adSubjectEnable",
            type: "radio",
            dicData:[{
              label: '是',
              value: 1
            },{
              label: '否',
              value: 0
            }],
            hide: true,
            span: 24,
            showOverflowTooltip: true,
          },
          {
            label: "加计扣除研发费用科目",
            prop: "adSubject",
            type: "textarea",
            span: 24,
            minWidth: 160,
            display: false,
            showOverflowTooltip: true,
          },
          {
            label: "辅助核算",
            prop: "fzhs",
            span: 24,
            width: 100,
            display: false,
            showOverflowTooltip: true,
          },
        ],
      },
    };
  },
  mounted() {
    this.loadData();
    this.getMenuData();
  },
  methods: {
    loopData(data, level, maxLevel) {
      level++;
      return data.map(item => {
        item.level = level;
        item.disabled = level === 4 ? true : false;
        item.hasChildren = item.children && item.children.length ? false : true;

        if (item.level === 4) {
          item.rdSubject = item.rdSubjectEnable === 1 ? item.accSubject : '';
          item.adSubject = item.adSubjectEnable === 1 ? item.accSubject : '';
        }

        if (maxLevel) {
          if (level >= maxLevel) {
            delete item.children;
          } else {
            this.loopData(item.children || [], level, maxLevel);
          }
        } else {
          this.loopData(item.children || [], level);
        }
        return item;
      });
    },
    getMenuData() {
      getList().then(res => {
        this.loading = false;
        if (res.data.code == 200) {

          const column = this.findObject(this.option.column, "parentId");
          column.dicData = this.loopData(JSON.parse(JSON.stringify(res.data.data)), 0, 3);
        }
      })
    },
    loadData(params) {
      this.loading = true;
      getList(params).then(res => {
        this.loading = false;
        if (res.data.code == 200) {

          this.data = this.loopData(res.data.data, 0);
        }
      })
    },
    searchReset() {
      this.query = {};
      this.parentId = 0;
      this.loadData();
    },
    searchChange(params, done) {
      this.query = params;
      this.parentId = '';
      this.loadData(params);
      done();
    },
    rowDel(row, index, done) {
      this.$confirm('确定将选择数据删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(row.id).then(({ data }) => {
          done();
          if (data.code == 200) {
            this.$message.success("删除成功!");
            this.loadData();
          }
        }).catch(() => {
          done();
        })
      })
    },
    rowSave(row, done) {
      add(row).then(({ data }) => {
        done();
        if (data.code == 200) {
          this.$message.success("新增成功!");
          this.loadData();
        }
      }).catch(() => {
        done();
      })
    },
    rowUpdate(row, index, done) {
      update(row).then(({ data }) => {
        done();
        if (data.code == 200) {
          this.$message.success("修改成功!");
          this.loadData();
        }
      }).catch(() => {
        done();
      })
    },
    handleAdd(row) {
      this.parentId = row.id;
      const column = this.findObject(this.option.column, "parentId");
      column.value = row.id;
      this.form.parentId = row.id;
      column.addDisabled = true;
      this.$refs.crud.rowAdd();
    },
  },
};
</script>