<!-- @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 v-show="editTopFlgValid != null && editTopFlgValid" ref="edit" name="edit"></BaseIndexButton>
        <!--    启用    -->
        <BaseIndexButton v-show="flgValid != null && flgValid" ref="enable" name="enable"></BaseIndexButton>
        <!--    停用    -->
        <BaseIndexButton v-show="flgValid != null && flgValid" ref="disable" name="disable"></BaseIndexButton>
      </template>
      <template #right>
        <!--   新建     -->
        <BaseIndexButton ref="add" name="add"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>

    <!--  卡片区  -->
    <div class="index-card">
      <!--  查询条件区域  -->
      <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>
      <!--  表格部分  -->
      <DkTable :data="tableData" :page-flag="false"
               :choose-flag="false" :page-total-flag="false"
               :height="tableHeight-30" tree-flag
               :child-disable-flag="false"
               :expand-flag="true"
               tree-row-field="orgId"
               @current-change="currentChangeEvent($event)"
               ref="table-select" name="table">
        <DkTableColumn field="orgName" :tree-node="true" ></DkTableColumn>
        <DkTableColumn field="levelName" :title="$t('orgLevelName')"></DkTableColumn>
        <DkTableColumn field="orgCode" ></DkTableColumn>
        <DkTableColumn field="orgMac"></DkTableColumn>
        <DkTableColumn field="whName"></DkTableColumn>
        <DkTableColumn field="flgValid" type="switch" ></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" :label-max-words="5">
        <!--部门名称-->
        <DkFormItem prop="orgName" :required="true">
          <InputPop v-model="formData.orgName" ref="orgName"/>
        </DkFormItem>
        <!--父级名称-->
        <DkFormItem prop="parentId" :label="$t('parentName')" :required="true">
          <SelectPop ref="parentName"  v-model="formData.parentId" :options="parentOrgList"
                     label-key="orgName" value-key="orgId"
                     :showClearFlag="false"
                     :multiple="false" :tree-flag="true" :tree-strict="false"
                     :disabled="formData.levelNo == 1"/>
        </DkFormItem>
        <!--仓库-->
        <DkFormItem prop="whId" >
          <MultiSelect v-model="formData.warehouseList"
                       main-field="ownerFlag"
                       rate-field="allocationRatio"
                       :order-id="formData.orgId"
                       :default-value="formData.whName"
                       @ok="warehousOk($event)"
                       :type="this.$config.MagnifierType.warehouse"/>
        </DkFormItem>
        <!--资金账号-->
        <DkFormItem prop="macId" >
          <MultiSelect v-model="formData.macList"
                       main-field="ownerFlag"
                       rate-field="allocationRatio"
                       :order-id="formData.orgId"
                       :default-value="formData.macName"
                       @ok="moneyAccountOk($event)"
                       :type="this.$config.MagnifierType.moneyAccount"/>
        </DkFormItem>
        <!--备注-->
        <DkFormItem prop="remarks">
          <InputPop v-model="formData.remarks" textareaFlag/>
        </DkFormItem>
      </DkForm>
    </DkModal>
  </div>
</template>

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

export default {
  name: "org",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      formData: {
        orgCode: null,
        orgName: null,
        parentId: null,
        levelLeaf: null,
        displayNo: null,
        remarks: null,
        warehouseList: [],//存放组织仓库
        macList:[],//存放资金账号
        whId:null,
        whCode:null,
        whName:null,
        macId:null,
        macCode:null,
        macName:null,
      },
      // 获取topId值
      topId: null,
      // 是否显示启用或停用按钮
      flgValid: null,
      // 如果parent_id是空，则不能编辑
      editTopFlgValid:null,
      // 父级信息
      parentOrgList:[],
      // 查询条件
      searchContent: [
        {
          itemCode: 'orgName',
        },
        self.$config.flgValidSearch // 有效标识
      ],
    }
  },
  methods: {
    /**
     * @desc   : 根据ID查询数据
     * @author : 宋扬
     * @date   : 2024/3/8 15:43
     */
    detail(id) {
      return this.excuteNoParam(this.$service.orgService, this.$service.orgService.selectById, [id],false)
    },

    /**
     * @desc   : 查询数据
     * @author : 宋扬
     * @date   : 2024/3/8 15:43
     */
    getData(params) {
      let excute =  this.excute(this.$service.orgService, this.$service.orgService.selectByCond, params)
      excute.then(res=>{
        if (res.code === this.$config.SUCCESS_CODE) {
         this.parentOrgList=res.data.list;
         // 赋值TopId
          this.topId=res.data.list[0].topId;
        }
      })
      return excute
    },

    /**
     * @desc   : 保存数据
     * @author : 付斌
     * @date   : 2024-01-30 15:21
     */
    saveData() {
      if (this.modalParams.button === this.$config.formMode.add) {
        return this.excute(this.$service.orgService, this.$service.orgService.insert, this.params)
      } else {
        return this.excute(this.$service.orgService, this.$service.orgService.update, this.params)
      }
    },

    /**
     * @desc   : 仓库赋值
     * @author : 宋扬
     * @date   : 2024/3/22 10:19
     */
    warehousOk(rows) {
      if (rows && rows.length > 0) {
        this.formData.warehouseList = []
        for (let i of rows) {
          //找到主业务员
          if (i.ownerFlag) {
            this.formData.whId = i.whId
            this.formData.whCode = i.whCode
            this.formData.whName = i.whName
          }
          //存放从业务员id和分配比
          this.formData.warehouseList.push({
            whId: i.whId,
            whCode: i.whCode,
            whName: i.whName,
            ownerFlag: i.ownerFlag
          })
        }
      }
    },

    /**
     * @desc   : 资金账户
     * @author : 宋扬
     * @date   : 2024/3/22 10:21
     */
    moneyAccountOk(rows) {
      if (rows && rows.length > 0) {
        this.formData.macList = []
        for (let i of rows) {
          //找到主业务员
          if (i.ownerFlag) {
            this.formData.macId = i.macId
            this.formData.macCode = i.macCode
            this.formData.macName = i.macName
          }
          //存放从业务员id和分配比
          this.formData.macList.push({
            macId: i.macId,
            macCode: i.macCode,
            macName: i.macName,
            ownerFlag: i.ownerFlag
          })
        }
      }
    },

    /**
     * @desc   : 切换实际赋值
     * @author : 宋扬
     * @date   : 2024/3/11 17:17
     */
    currentChangeEvent({row}) {
      if (row != null) {
        this.formData.parentId = row.orgId;
        // 判断是否是底层数据
        this.flgValid = row.levelLeaf;
        if(row.parentId==null){
          this.editTopFlgValid=false;
        }
        else{
          this.editTopFlgValid=true;
        }
      }
    },

  },
  created() {
    this.primaryKey = 'orgId'  // 设置主键Id
    this.routeObjName = 'org'  // 设置路由名称
    this.comHandleEnableFlag = true;
  }
}
</script>
