<!-- @desc:商品属性  @auth:常皓宁  @time:2024-1-31 9:47 -->
<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="brandTableFlag" slot="right" :id="'table-' + $options.name" ref="table-select"
                   :data="brandTableData"
                   :pageFlag="true"
                   :page-total="pageInfo.total" :current-page="pageInfo.currentPage" :choose-flag="false"
                   :height="this.tableHeight-30" primaryKey="brandId" @pageChange="pageSizeChange">
            <DkTableColumn field="brandCode"></DkTableColumn>
            <DkTableColumn field="brandName"></DkTableColumn>
            <DkTableColumn field="shortName"></DkTableColumn>
            <DkTableColumn field="supplierNames" :title="$t('supplierName')"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"></DkTableColumn>
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>

          <!-- 系列表格   -->
          <DkTable v-if="seriesTableFlag" slot="right" :id="'table-' + $options.name" ref="table-select"
                   :data="seriesTableData"
                   :pageFlag="true"
                   :page-total="pageInfo.total" :current-page="pageInfo.currentPage" :choose-flag="false"
                   :height="this.tableHeight" primaryKey="seriesId" @pageChange="pageSizeChange">
            <DkTableColumn field="seriesCode"></DkTableColumn>
            <DkTableColumn field="seriesName"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"></DkTableColumn>
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>

          <!-- 计量单位表格   -->
          <DkTable v-if="unitTableFlag" slot="right" :id="'table-' + $options.name" ref="table-select"
                   :data="unitTableData"
                   :pageFlag="true"
                   :page-total="pageInfo.total" :current-page="pageInfo.currentPage" :choose-flag="false"
                   :height="this.tableHeight" primaryKey="unitId" @pageChange="pageSizeChange">
            <DkTableColumn field="unitCode"></DkTableColumn>
            <DkTableColumn field="unitName"></DkTableColumn>
            <DkTableColumn field="decimalPlaces"></DkTableColumn>
            <DkTableColumn field="flgValid" type="switch"></DkTableColumn>
            <DkTableColumn field="remarks"></DkTableColumn>
          </DkTable>

          <!-- 商品种类表格   -->
          <DkTable v-if="treeFlag"
                   :data="categoryTableData"
                   slot="right" :page-flag="false"
                   :page-total-flag="false"
                   :height="this.tableHeight"
                   :tree-flag="true"
                   :choose-flag="false"
                   :expand-flag="true"
                   tree-row-field="catId"
                   ref="table-select" name="table">
            <DkTableColumn field="catCode" min-width="150"></DkTableColumn>
            <DkTableColumn field="catName" min-width="150" tree-node></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="goodsAttrName" :required="true">
          <InputPop v-model="formData.goodsAttrName"/>
        </DkFormItem>
        <!--品牌简称（品牌特有）-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.goodsBrand" prop="shortName" :required="true">
          <InputPop v-model="formData.shortName"/>
        </DkFormItem>
        <!--供应商 (品牌特有)-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.goodsBrand" prop="supId" :label="$t('supplierName')">
          <SelectMagnifier v-model="formData.supplierIds" :display-text="formData.supplierName"
                           :type="this.$config.MagnifierType.supplier"/>
        </DkFormItem>
        <!--父级 (种类特有)-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.goodsCategory" prop="parentId">
          <SelectPop v-model="formData.parentId" ref="parentId"
                     :options="categoryList"
                     :treeStrict="false"
                     :treeFlag="true"
                     :multiple="false"
                     labelKey="catName" valueKey="catId">
          </SelectPop>
        </DkFormItem>
        <!--小数位数（计量单位特有）-->
        <DkFormItem v-if="kindCode === this.$config.kindCode.goodsUnit" prop="digits" :required="true" :data-type="$config.dataType.number">
          <InputNumberPop v-model="formData.digits" :digits="0" :min="0" :max="6"/>
        </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: 'goods-attr',
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      //左侧列表
      leftData: [],
      kindList: [],
      modalTitle: '',//新建编辑弹窗名称
      submitType: '',//新建编辑提交类型
      split: 0.15,
      minSplit: 0.15,
      treeFlag: false,// 种类
      brandTableFlag: false, //品牌
      seriesTableFlag: false, //系列
      unitTableFlag: false, //计量单位
      kindCode: null,
      // 种类数据
      categoryTableData: [],
      // 品牌数据
      brandTableData: [],
      // 系列数据
      seriesTableData: [],
      // 计量单位数数据
      unitTableData: [],
      // 种类父级
      categoryList:[],
      formData: {
        kindName: null,
        goodsAttrName: null,
        shortName: null,
        parentId:null,
        parentName:null,
        supplierIds: [],
        supplierName:null,
        digits: 0,
        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.goodsBrand || this.kindCode == null) {
          setTimeout(()=>{
            this.brandTableFlag = true
          },100)
          this.treeFlag = false
          this.seriesTableFlag = false
          this.unitTableFlag = false
          params.brandCode = row.code
          params.brandName = row.name
          this.excute(this.$service.goodsBrandService, this.$service.goodsBrandService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.brandTableData = res.data.list
            }
          })
        }
        // 商品种类
        if (this.kindCode == this.$config.kindCode.goodsCategory) {
          params.catCode = row.code
          params.catName = row.name
          setTimeout(()=>{
            this.treeFlag = true
          },100)
          this.brandTableFlag = false
          this.seriesTableFlag = false
          this.unitTableFlag = false
          this.excute(this.$service.goodsCategoryService, this.$service.goodsCategoryService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.categoryTableData = res.data.list
              this.categoryList = res.data.list
            }
          })
        }
        // 商品系列
        if (this.kindCode == this.$config.kindCode.goodsSeries) {
          params.seriesCode = row.code
          params.seriesName = row.name
          this.treeFlag = false
          this.brandTableFlag = false
          this.unitTableFlag = false
          setTimeout(()=>{
            this.seriesTableFlag = true
          },100)
          this.excute(this.$service.goodsSeriesService, this.$service.goodsSeriesService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.seriesTableData = res.data.list
            }
          })
        }
        // 计量单位
        if (this.kindCode == this.$config.kindCode.goodsUnit) {
          params.unitCode = row.code
          params.unitName = row.name
          setTimeout(()=>{
            this.unitTableFlag = true
          },100)
          this.treeFlag = false
          this.seriesTableFlag = false
          this.brandTableFlag = false
          this.excute(this.$service.unitService, this.$service.unitService.selectByCond, params).then(res => {
            if (res.code === this.$config.SUCCESS_CODE) {
              this.unitTableData = 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.goodsBrand) {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].brandId
          this.formData.goodsAttrName = rows[0].brandName
          this.formData.shortName = rows[0].shortName
          this.formData.supplierIds = rows[0].supplierIds
          this.formData.supplierName = rows[0].supplierNames
          this.formData.remarks = rows[0].remarks
        }
        // 商品种类
        if (row.kindCode == this.$config.kindCode.goodsCategory) {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].catId
          this.formData.goodsAttrName = rows[0].catName
          this.formData.parentId = rows[0].parentId
          this.formData.parentName = rows[0].parentName
          this.formData.remarks = rows[0].remarks
        }
        // 商品系列
        if (row.kindCode == this.$config.kindCode.goodsSeries) {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].seriesId
          this.formData.goodsAttrName = rows[0].seriesName
          this.formData.remarks = rows[0].remarks
        }
        // 计量单位
        if (row.kindCode == this.$config.kindCode.goodsUnit) {
          this.formData.kindName = row.kindName
          this.formData.id = rows[0].unitId
          this.formData.goodsAttrName = rows[0].unitName
          this.formData.digits = rows[0].decimalPlaces
          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.goodsBrand) {
        params = {
          brandId:this.formData.id,
          brandName: this.formData.goodsAttrName,
          shortName: this.formData.shortName,
          supplierIds: this.formData.supplierIds,
          remarks:this.formData.remarks,
        }
        serviceName = this.$service.goodsBrandService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.goodsBrandService.insert
        } else {
          serviceNameWay = this.$service.goodsBrandService.update
        }
      }
      // 商品种类
      if (this.kindCode === this.$config.kindCode.goodsCategory) {
        params = {
          catId:this.formData.id,
          catName: this.formData.goodsAttrName,
          parentId: this.formData.parentId,
          parentName:this.formData.parentName,
          remarks:this.formData.remarks,
        }
        serviceName = this.$service.goodsCategoryService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.goodsCategoryService.insert
        } else {
          serviceNameWay = this.$service.goodsCategoryService.update
        }
      }
      // 商品系列
      if (this.kindCode === this.$config.kindCode.goodsSeries) {
        params = {
          seriesId:this.formData.id,
          seriesName: this.formData.goodsAttrName,
          remarks:this.formData.remarks,
        }
        serviceName = this.$service.goodsSeriesService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.goodsSeriesService.insert
        } else {
          serviceNameWay = this.$service.goodsSeriesService.update
        }
      }
      // 计量单位
      if (this.kindCode === this.$config.kindCode.goodsUnit) {
        params = {
          unitId:this.formData.id,
          unitName: this.formData.goodsAttrName,
          decimalPlaces: this.formData.digits,
          remarks:this.formData.remarks,
        }
        serviceName = this.$service.unitService
        if (this.modalParams.button === this.$config.formMode.add) {
          serviceNameWay = this.$service.unitService.insert
        } else {
          serviceNameWay = this.$service.unitService.update
        }
      }
      this.excute(serviceName, serviceNameWay, params, true, 'save').then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.formData = []
          this.getData();
        }
      })
    },

    /**
     * @desc   : 获取数据
     * @author : 常皓宁
     * @date   : 2024/3/9 10:55
     */
    getData(params) {
      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
     */
    getGoodsAttrData() {
      // 读取系统字典
      let dataKindList =this.$store.state.user.dataKindList
      let data = dataKindList.filter(it=>it.kindType == this.$config.kindType.goodsAttr)
      this.leftData = data
      if (this.leftData.size() > 0) {
        this.kindCode = this.leftData[0].kindCode
      }
    },
  },
  created() {
    this.primaryKey = 'goodsAttrId'  // 设置主键Id
    this.routeObjName = 'goodsAttr'  // 设置路由名称
    this.getGoodsAttrData() //商品属性左侧列表数据
  }
}
</script>

<style scoped>

</style>
