<!-- @desc:基础资料  @auth:常皓宁  @time:2024-1-30 15:28 -->
<template>
  <div class="main-div" ref="mainDiv">
    <loading :loading="loading" v-if="!modalVisible"></loading>
    <BaseIndexButtonGroup ref="BaseIndexButtonGroup" id="BaseIndexButtonGroup">
      <template #left>
        <!--    查询    -->
        <BaseIndexButton ref="search" name="search"></BaseIndexButton>
        <!--    清空条件    -->
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
        <!--    编辑    -->
        <BaseIndexButton ref="edit" name="edit"></BaseIndexButton>
        <!--    启用    -->
        <BaseIndexButton ref="enable" name="enable"></BaseIndexButton>
        <!--    停用    -->
        <BaseIndexButton 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"
                    :searchContent="searchContent"
        ></SearchCond>
      </div>

      <!--  表格部分  -->
      <div :style="'height: ' + tableHeight + 'px'" class="split-div-class">
        <DkSplit v-model="split">
          <!-- 左边部分   -->
          <DkTable slot="left" ref="leftTable" :data="leftData" :height="tableHeight" :page-flag="false"
                   :page-total-flag="false" :show-setting-flag="false" :freeze="false" :choose-flag="false"
                   @current-change="currentChangeEvent($event)">
            <DkTableColumn :title="$t('allType')" field="kindName" width="auto" :filter="false" :sortable="false"
                           :center-flag="true">
            </DkTableColumn>
          </DkTable>

          <!-- 仓库表格   -->
          <DkTable v-if="wareHouseTableFlag" slot="right" :id="'table-' + $options.name" ref="table-select"
                   :data="wareHouseData"
                   :pageFlag="true"
                   :page-total="pageInfo.total" :current-page="pageInfo.currentPage" :choose-flag="false"
                   :height="this.tableHeight-30" primaryKey="brandId" @pageChange="pageSizeChange">
            <DkTableColumn field="whCode"></DkTableColumn>
            <DkTableColumn field="whName"></DkTableColumn>
            <DkTableColumn field="supervisor"></DkTableColumn>
            <DkTableColumn field="contactPhone"></DkTableColumn>
            <DkTableColumn field="flgDefault" type="switch"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"></DkTableColumn>
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>

          <!-- 销售渠道表格   -->
          <DkTable v-if="saleChannelTableFlag" slot="right" :id="'table-' + $options.name" ref="table-select"
                   :data="saleChannelTableData"
                   :pageFlag="true"
                   :page-total="pageInfo.total" :current-page="pageInfo.currentPage" :choose-flag="false"
                   :height="this.tableHeight" primaryKey="channelId" @pageChange="pageSizeChange">
            <DkTableColumn field="channelCode"></DkTableColumn>
            <DkTableColumn field="channelName"></DkTableColumn>
            <DkTableColumn field="flgDefault" type="switch"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"></DkTableColumn>
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>

          <!-- 数据字典   -->
          <DkTable v-if="dictionaryTableFlag" slot="right" :id="'table-' + $options.name" ref="table-select"
                   :data="dictionaryTableData"
                   :pageFlag="true"
                   :page-total="pageInfo.total" :current-page="pageInfo.currentPage" :choose-flag="false"
                   :height="this.tableHeight" primaryKey="unitId" @pageChange="pageSizeChange">
            <DkTableColumn field="dataValue"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"></DkTableColumn>
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>
        </DkSplit>
      </div>

    </div>

    <!--新建编辑-->
    <DkModal
      :loading="loading"
      v-model="editAddModal"
      ref="modal_editAdd"
      @on-visible-change="handleVisibleModal"
      @modalOk="saveData"
      :saveFlag="true"
      :title="modalTitle"
    >
      <DkForm ref="formInline" v-model="formData" :col-count="1">
        <!--属性分类-->
        <DkFormItem prop="kindName" :label="$t('className')">
          <InputPop v-model="formData.kindName" :disabled="true"/>
        </DkFormItem>
        <!--仓库名称-->
        <DkFormItem prop="name" :required="true">
          <InputPop v-model="formData.name"/>
        </DkFormItem>
        <!--仓库主管 (仓库特有)-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.basicWarehouse" prop="supervisor">
          <InputPop v-model="formData.supervisor"/>
        </DkFormItem>
        <!--联系电话（仓库特有）-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.basicWarehouse" prop="contactPhone">
          <InputPop v-model="formData.contactPhone"/>
        </DkFormItem>
        <!-- 默认仓库（仓库特有）-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.basicWarehouse || kindCode === this.$config.kindCode.basicChannel" prop="flgDefault">
          <DkSwitch prop="flgDefault" v-model="formData.flgDefault"
                    ref="flgDefault"></DkSwitch>
        </DkFormItem>
        <!--备注-->
        <DkFormItem prop="remarks">
          <InputPop ref="remarks" v-model="formData.remarks"
                    maxlength=500 textareaFlag/>
        </DkFormItem>
      </DkForm>
    </DkModal>

  </div>
</template>

<script>
import {indexMixin} from '@/mixins'

export default {
  name: 'basic-data',
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      modalTitle: '',//新建编辑弹窗名称
      submitType: '',//新建编辑提交类型
      kindCode: null,
      split: 0.15,
      minSplit: 0.15,
      leftData: [],
      tableData: [],
      wareHouseTableFlag: false, //仓库
      saleChannelTableFlag: false, //销售渠道
      dictionaryTableFlag: false, //数据字典
      // 仓库数据
      wareHouseData: [],
      // 销售渠道
      saleChannelTableData: [],
      // 数据字典
      dictionaryTableData: [],
      formData: {
        id: null,
        kindName: null,
        name: null,
        supervisor: null,
        contactPhone: null,
        flgDefault: false,
        remarks: null,
      },
      searchContent:
        [
          {
            itemCode: 'code',
            itemName: 'goodsAttrCode'
          },
          {
            itemCode: 'name',
            itemName: 'goodsAttrName'
          },
        ],
    }
  },
  methods: {
    /**
     * @desc   : 左侧列表行变化事件
     * @author : 常皓宁
     * @date   : 2024/1/31 9:03
     */
    currentChangeEvent({row}) {
      if (row != null) {
        this.kindCode = row.kindCode
        let params = {
          cpId: this.$store.state.user.cpId,
        }

        // 仓库
        if (this.kindCode == this.$config.kindCode.basicWarehouse || this.kindCode == null) {
          setTimeout(() => {
            this.wareHouseTableFlag = true
          }, 100)
          this.saleChannelTableFlag = false
          this.dictionaryTableFlag = false
          params.whCode = row.code
          params.whName = row.name
          this.excute(this.$service.wareHouseService, this.$service.wareHouseService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.wareHouseData = res.data.list
            }
          })
        }
        // 渠道
        else if (this.kindCode == this.$config.kindCode.basicChannel) {
          params.channelCode = row.code
          params.channelName = row.name
          this.wareHouseTableFlag = false
          this.dictionaryTableFlag = false
          setTimeout(() => {
            this.saleChannelTableFlag = true
          }, 100)
          this.excute(this.$service.saleChannelService, this.$service.saleChannelService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.saleChannelTableData = res.data.list
            }
          })
        } else {
          // 数据字典
          params.dictCode = this.kindCode
          setTimeout(() => {
            this.dictionaryTableFlag = true
          }, 100)
          this.saleChannelTableFlag = false
          this.wareHouseTableFlag = false
          this.excute(this.$service.dictionaryDataService, this.$service.dictionaryDataService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.dictionaryTableData = res.data.list
            }
          })
        }
      }
    },

    /**
     * @desc   : 新建按钮点击事件
     * @author : 常皓宁
     * @date   : 2024/1/31 9:19
     */
    add() {
      this.modalTitle = this.setTitle(this.$config.formMode.add, '基础资料')
      let row = this.$refs['leftTable'].$refs.table.getCurrentRecord()
      if (row) {
        this.formData = {}
        this.formData.kindName = row.kindName
      }
      this.editAddModal = true
      this.modalParams.button = 'add'
    },

    /**
     * @desc   : 编辑按钮事件
     * @author : 常皓宁
     * @date   : 2024/1/31 10:35
     */
    edit(rows) {
      this.modalTitle = this.setTitle(this.$config.formMode.edit, '基础资料')
      let row = this.$refs['leftTable'].$refs.table.getCurrentRecord()
      if (rows.length > 0) {
        // 仓库
        if (row.kindCode == this.$config.kindCode.basicWarehouse) {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].whId
          this.formData.name = rows[0].whName
          this.formData.supervisor = rows[0].supervisor
          this.formData.contactPhone = rows[0].contactPhone
          this.formData.flgDefault = rows[0].flgDefault
          this.formData.remarks = rows[0].remarks
        }
        // 销售渠道
        else if (row.kindCode == this.$config.kindCode.basicChannel) {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].channelId
          this.formData.name = rows[0].channelName
          this.formData.flgDefault = rows[0].flgDefault
          this.formData.remarks = rows[0].remarks
        }
        //数据字典
        else {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].dataId
          this.formData.name = rows[0].dataValue
          this.formData.remarks = rows[0].remarks
        }
        this.editAddModal = true
        this.modalParams.button = 'edit'
      }
    },

    /**
     * @desc   : 新建
     * @author : 常皓宁
     * @date   : 2024/3/9 15:51
     */
    saveData() {
      let serviceName
      let serviceNameWay
      let params = {}
      //仓库
      if (this.kindCode === this.$config.kindCode.basicWarehouse) {
        params = {
          whId: this.formData.id,
          whName: this.formData.name,
          supervisor: this.formData.supervisor,
          contactPhone: this.formData.contactPhone,
          remarks: this.formData.remarks,
          flgDefault: this.formData.flgDefault,
        }
        serviceName = this.$service.wareHouseService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.wareHouseService.insert
        } else {
          serviceNameWay = this.$service.wareHouseService.update
        }
      }
      // 销售渠道
      else if (this.kindCode === this.$config.kindCode.basicChannel) {
        params = {
          channelId: this.formData.id,
          channelName: this.formData.name,
          flgDefault: this.formData.flgDefault,
        }
        serviceName = this.$service.saleChannelService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.saleChannelService.insert
        } else {
          serviceNameWay = this.$service.saleChannelService.update
        }
      }
      // 数据字典
      else {
        params = {
          dataId: this.formData.id,
          dictCode: this.kindCode,
          dataValue: this.formData.name,
          remarks: this.formData.remarks,
        }
        serviceName = this.$service.dictionaryDataService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.dictionaryDataService.insert
        } else {
          serviceNameWay = this.$service.dictionaryDataService.update
        }
      }
      this.excute(serviceName, serviceNameWay, params, true, 'save').then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.formData.id = null
          this.formData.name = null
          this.formData.supervisor = null
          this.formData.contactPhone = null
          this.formData.flgDefault = null
          this.formData.remarks = null
          this.searchData();
        }
      })
    },

    /**
     * @desc   : 获取数据
     * @author : 常皓宁
     * @date   : 2024/3/9 10:55
     */
    getData() {
      if (this.kindCode) {
        let data = {
          kindCode: this.kindCode,
          code: this.searchCond._value['code'],
          name: this.searchCond._value['name'],
        }
        this.currentChangeEvent({row: data})
      }
      this.loading = false
    },

    /**
     * @desc   : 基础资料左侧列表数据
     * @author : 常皓宁
     * @date   : 2024/3/9 13:03
     */
    getBasicData() {
      // 读取系统字典
      let dataKindList = this.$store.state.user.dataKindList
      let data = dataKindList.filter(it => it.kindType == this.$config.kindType.basicData)
      this.leftData = data
      if (this.leftData.size() > 0) {
        this.kindCode = this.leftData[0].kindCode
      }
    },
  },
  created() {
    this.primaryKey = 'basicDataId'  // 设置主键Id
    this.routeObjName = 'basicData'  // 设置路由名称
    this.getBasicData() //商品属性左侧列表数据
  }
}
</script>

<style scoped>

</style>
