<!-- @desc:部门管理  @auth:付斌  @time:2024/1/30 15:15 -->
<template>
  <div class="main-div" ref="mainDiv">
    <loading :loading="loading" v-if="!modalVisible"></loading>
    <!--按钮区-->
    <BaseIndexButtonGroup id="BaseIndexButtonGroup">
      <template #left>
        <!--    查询    -->
        <BaseIndexButton ref="search" name="search"></BaseIndexButton>
        <!--    清空条件    -->
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
        <!--    编辑    -->
        <BaseIndexButton ref="edit" name="edit"></BaseIndexButton>
      </template>
      <template #right>
        <!--   新建     -->
        <BaseIndexButton ref="add" name="add"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>

    <!--  查询条件区域  -->
    <div id="search-cond-div" ref="search-cond-div" class="search-cond-class">
      <SearchCond ref="searchCond" v-model="searchCond"
                  @collapse-change="collapseChange" :setFlag="false"
                  :search-content="searchContent"></SearchCond>
    </div>

    <!--  表格部分  -->
    <div>
      <DkTable :data="tableData" :page-flag="false"
               :choose-flag="false" :page-total-flag="false"
               :height="tableHeight" tree-flag
               :child-disable-flag="false"
               :expand-flag="true"
               tree-row-field="orgId"
               ref="table-select" name="table">
        <DkTableColumn field="orgName" width="300" :tree-node="true" type="link"
                       @on-link="handleLink(Object.assign($event,{button:'edit'}))"></DkTableColumn>
        <DkTableColumn field="levelName" width="350" :title="$t('orgLevelName')"></DkTableColumn>
        <DkTableColumn field="orgCode" width="260" :title="$t('orgCode')"></DkTableColumn>
        <DkTableColumn field="displayNo"></DkTableColumn>
        <DkTableColumn field="flgValid" type="switch"
                       @on-switch-change="onSwitchChange" :switch-disabled="false"></DkTableColumn>
        <DkTableColumn field="remarks" width="auto"></DkTableColumn>
      </DkTable>
    </div>

    <!--新建编辑-->
    <DkModal
      :loading="loading"
      v-model="editAddModal"
      ref="modal_editAdd"
      @on-visible-change="handleVisibleModal"
      @modalOk="save"
      :saveFlag="true"
      :title="modalTitle"
    >
      <DkForm ref="formInline" v-model="formData" :col-count="1">
        <!--部门名称-->
        <DkFormItem prop="orgName" :required="true">
          <InputPop v-model="formData.orgName" ref="orgName"/>
        </DkFormItem>
        <!--父级名称-->
        <DkFormItem prop="parentId" :data-type="$config.dataType.number" :label="$t('parentName')" required>
          <SelectPop ref="parentName" v-model="formData.parentId" label-key="orgName" value-key="orgId"
                     :showClearFlag="false"
                     :multiple="false" :options="orgList" :tree-flag="true" :tree-strict="false"
                     @on-select-label="selectOrg" @input="changeOrg" :disabled="formData.levelNo == 1"/>
        </DkFormItem>
        <!--显示顺序-->
        <DkFormItem prop="displayNo" :data-type="$config.dataType.number" :required="true">
          <InputNumberPop v-model="formData.displayNo" ref="displayNo"/>
        </DkFormItem>
        <!--部门编码-->
        <DkFormItem prop="orgCode">
          <InputPop v-model="formData.orgCode" ref="orgCode"/>
        </DkFormItem>
        <!--备注-->
        <DkFormItem prop="remarks">
          <InputPop v-model="formData.remarks" textareaFlag/>
        </DkFormItem>
      </DkForm>
    </DkModal>
  </div>
</template>

<script>
import {indexMixin} from "@/mixins";

export default {
  name: "organization",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      tableData: [],
      editAddModal: false,
      modalTitle: '',//新建编辑弹窗名称
      submitType: '',//新建编辑提交类型
      orgList: [],// 部门
      formData: {
        orgCode: null,
        orgName: '',
        parentId: 0,
        topId: 0,
        parentName: '',
        displayNo: 0,
        remarks: ''
      },
      searchContent: [
        {itemCode: 'orgName'},
        self.$config.flgValidSearch // 有效标识
      ],
      oldOrg: {},
      orgId: null,
    }
  },
  methods: {
    // region 一览界面操作
    /**
     * @desc   : switch停用启用
     * @author : 付斌
     * @date   : 2024-01-30 15:19
     */
    onSwitchChange(param) {
      if (param.flgValid) {
        this.excuteNoParam(this.$service.organizationService, this.$service.organizationService.enable, [param.orgId]).then(res => {
          this.searchData();
        })
      } else {
        this.excuteNoParam(this.$service.organizationService, this.$service.organizationService.disable, [param.orgId]).then(res => {
          this.searchData();
        })
      }
    },
    /**
     * @desc   : 获取数据
     * @author : 付斌
     * @date   : 2024-01-30 15:19
     */
    getData(params) {
      // 查询数据
      let excute = this.excute(this.$service.organizationService, this.$service.organizationService.selectByCond, params);
      excute.then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          for (let it of res.data.list) {
            //顶级部门、区域不能停用
            it.disabled = it.levelNo == 1
          }
        }
      })
      return excute
    },
    // endregion

    // region 新建/编辑

    /**
     * @desc   : 清空
     * @author : 付斌
     * @date   : 2024-01-30 15:20
     */
    clear() {
      let parentName = this.formData.parentName
      let parentId = this.formData.parentId
      let topId = this.formData.topId
      this.formData = {
        parentName: parentName,
        parentId: parentId,
        topId: topId
      }
      this.getMaxDisplayNo(this.$config.displayNoTable.organization);
    },

    /**
     * @desc   : 加载数据
     * @author : 姜宁
     * @date   : 2023/1/9 13:09
     */
    initData(type) {
      if (type === this.$config.formMode.add) {
        // 加载最大序号
        this.getMaxDisplayNo(this.$config.displayNoTable.organization);
        this.getOrgInfo(null, null);  // 获取部门
      }
    },
    /**
     * @desc   : 获取部门
     * @author : 付斌
     * @date   : 2024-01-30 15:20
     */
    getOrgInfo(topId, orgId) {
      let params = {
        ftyId: this.$store.state.user.ftyId,
        topId: topId,
        orgId: orgId
      }
      this.excute(this.$service.commonService, this.$service.commonService.getOrg, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.orgList = res.data;
        }
      })
    },
    /**
     * @desc   : 选择部门数据
     * @author : 周兴
     * @date   : 2023/4/11 8:31
     */
    selectOrg(row) {
      if (row) {
        this.oldOrg = row
        this.formData.topId = row.topId
        this.formData.parentId = row.orgId
        this.formData.parentName = row.orgName
      } else {
        return
      }
    },
    /**
     * @desc   :
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    changeOrg(row) {
      if (row) {
        this.orgId = row
      } else {
        let row = this.orgList.filter(it => it.orgId == this.orgId)[0]
        this.formData.topId = row.topId
        this.formData.parentId = row.orgId
        this.formData.parentName = row.orgName
        this.$refs.formInline.resetFields()
        // console.log("this.formData",this.formData,this.$refs.formInline)
      }
    },
    /**
     * @desc   : 给参数赋值
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    setParams() {
      this.params = {...this.formData}
    },
    /**
     * @desc   : 保存数据
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    saveData() {
      if (this.modalParams.button === this.$config.formMode.add) {
        delete this.params.modelId // 解决提示主键重复的问题，删除主键代码
        return this.excute(this.$service.organizationService, this.$service.organizationService.insert, this.params)
      } else {
        return this.excute(this.$service.organizationService, this.$service.organizationService.update, this.params)
      }
    },
    /**
     * @desc   : 新建按钮点击事件
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    add() {
      this.modalTitle = this.setTitle(this.$config.formMode.add, 'organization')
      this.modalParams = {
        button: this.$config.formMode.add,
      }
      this.editAddModal = true
      let chooseKeys = this.$refs['table-select'].batchRows;
      this.oldOrg = chooseKeys
      if (chooseKeys && chooseKeys.length > 0) {
        this.formData.topId = chooseKeys[0].topId
        this.formData.parentId = chooseKeys[0].orgId
        this.formData.parentName = chooseKeys[0].orgName
      } else {
        this.formData.topId = 0
        this.formData.parentName = ''
        this.formData.parentId = 0
      }
    },

    /**
     * @desc   : 编辑按钮事件
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    edit(rows) {
      this.modalParams = {
        button: this.$config.formMode.edit,
        closeFlag: true
      }
      this.modalTitle = this.setTitle(this.$config.formMode.edit, 'organization')
      if (rows.length > 0) {
        this.detail(rows[0].orgId)
      }
      this.editAddModal = true
    },

    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    detail(id) {
      this.excuteNoParam(this.$service.organizationService, this.$service.organizationService.selectById,
        [id], false).then(res => {//调用api查询方法
        if (res.code === this.$config.SUCCESS_CODE) {
          this.formData = res.data
          // 获取部门
          this.getOrgInfo(this.formData.topId, this.formData.orgId)
        }
      })
    },
    // endregion

  },
  created() {
    this.pageFlag = false; // 不进行分页查询
    this.primaryKey = 'orgId'  // 设置主键Id
    this.routeObjName = 'organization'  // 设置路由名称
  }
}
</script>
