<!-- @desc:供应商一览  @auth:宋扬  @time:2024年1月31日11:01:32 -->
<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>
        <!--    查看    -->
        <BaseIndexButton ref="check" name="check"></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"
               :page-total-flag="false"
               :height="tableHeight-30"
               primaryKey="supId"
               :pageFlag="true"
               @pageChange="pageSizeChange"
               :page-total="pageInfo.total"
               :current-page="pageInfo.currentPage"
               ref="table-select" name="table">
        <DkTableColumn field="supCode"></DkTableColumn>
        <DkTableColumn field="supName"></DkTableColumn>
        <DkTableColumn field="contactPhone"></DkTableColumn>
        <DkTableColumn field="contactName"></DkTableColumn>
        <DkTableColumn field="supTypeName"></DkTableColumn>
        <DkTableColumn field="serviceCategoriesName"></DkTableColumn>
        <DkTableColumn field="brandName"></DkTableColumn>
        <DkTableColumn field="orgName" :title="$t('orgPurName')"></DkTableColumn>
        <DkTableColumn field="staffName" :title="$t('staffPurName')"></DkTableColumn>
        <DkTableColumn field="sumAmtPayable" :title="$t('startAmount')"></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="supName" :required="true">
          <InputPop v-model="formData.supName"/>
        </DkFormItem>
        <!--  供应商类别  -->
        <DkFormItem prop="supType" :required="true">
          <SelectPop v-model="formData.supType" ref="supType" :options="supTypeList" :multiple="false"
                     @on-select="onChange"
                     labelKey="kindName" valueKey="kindCode">
          </SelectPop>
        </DkFormItem>
        <!--  业务员  -->
        <DkFormItem prop="staffId">
          <SelectMagnifier v-model="formData.staffId" :display-text="formData.staffName"
                           :type="this.$config.MagnifierType.staff"
                           :multiple="false"  @ok="staffOk"></SelectMagnifier>

        </DkFormItem>
        <!--  业务部门  -->
        <DkFormItem prop="orgId" :required="true">
          <SelectMagnifier v-model="formData.orgId" :display-text="formData.orgName"
                           :type="this.$config.MagnifierType.org" @ok="orgOk"
                           :multiple="false"></SelectMagnifier>
        </DkFormItem>
        <!--  联系人  -->
        <DkFormItem prop="contactName">
          <InputPop v-model="formData.contactName"/>
        </DkFormItem>
        <!--  联系电话  -->
        <DkFormItem prop="contactPhone">
          <InputPop v-model="formData.contactPhone"  :telephone="true"/>
        </DkFormItem>
        <!--  品牌  -->
        <DkFormItem v-show="flgValidBrand" prop="brandIds" :data-type="$config.dataType.array" :required="flgValidBrand">
          <SelectMagnifier v-model="formData.brandIds" :display-text="formData.brandNames"
                           :type="this.$config.MagnifierType.brand"
                           :multiple="true"></SelectMagnifier>
        </DkFormItem>
        <!--  服务类别  -->
        <DkFormItem v-show="flgValidServiceCategories" prop="serviceCategories" :required="flgValidServiceCategories">
          <SelectPop v-model="formData.serviceCategories" ref="serviceCategories" :options="serviceCategoriesList"
                     :multiple="true"
                     labelKey="kindName" valueKey="kindCode">
          </SelectPop>
        </DkFormItem>
        <!--   期初欠款     -->
        <DkFormItem v-show="this.modalParams.button === this.$config.formMode.edit?false:true" prop="startAmount">
          <InputPop v-model="formData.startAmount" textareaFlag/>
        </DkFormItem>
        <!--   备注     -->
        <DkFormItem prop="remarks">
          <InputPop v-model="formData.remarks" textareaFlag/>
        </DkFormItem>

      </DkForm>
    </DkModal>
  </div>
</template>

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

export default {
  name: "supplier",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      orgList: [],
      staffList: [],
      supTypeList: [],
      serviceCategoriesList: [],
      flgValidBrand:false,
      flgValidServiceCategories:false,
      formData: {
        supCode: null,
        supName: null,
        contactPhone: null,
        contactName: null,
        staffId: self.$store.state.user.staffId,
        staffName: self.$store.state.user.staffName,
        supType: null,
        serviceCategories: null,
        brandIds: [],
        brandNames:null,
        remarks: null,
        orgId : self.$store.state.user.orgId,
        orgName  : self.$store.state.user.orgName,
        makeStaff: self.$store.state.user.id,
        startAmount:0,
      },
      // 查询条件
      searchContent: [
        {
          itemCode: 'supName',
        },
        {
          itemCode: 'supTypeList',
          itemName: self.$t('supType'),
          valueFormat: {
            code: 'kindType',
            data: [],
            valueKey: 'kindCode',
            labelKey: 'kindName',
          },
          valueKind: 'M-CHOICE'
        },
        self.$config.flgValidSearch // 有效标识
      ],
    }
  },
  methods: {

    /**
     * @desc   : 加载数据
     * @author : 宋扬
     * @date   : 2024/3/4 11:16
     */
    initData() {
      // 获取供应商类别
      this.getSupType();
      // 获取服务类别
      this.getserviceCategories();
    },

    /**
     * @desc   : 获取供应商类别
     * @author : 宋扬
     * @date   : 2024/3/4 16:33
     */
    getSupType() {
      // 读取系统字典
      let dataKindList =this.$store.state.user.dataKindList
      let data = dataKindList.filter(it=>it.kindType == this.$config.kindType.supType)
      let list = this.searchContent.filter(it =>  it.valueFormat && it.valueFormat.code == 'kindType')
      if (list.length > 0) {
        list[0].valueFormat.data = data
      }
      this.supTypeList = data;
    },

    /**
     * @desc   : 获取服务类别
     * @author : 宋扬
     * @date   : 2024/3/4 16:33
     */
    getserviceCategories() {
      // 读取系统字典
      let dataKindList =this.$store.state.user.dataKindList
      let data = dataKindList.filter(it=>it.kindType == this.$config.kindType.serviceCategories)
      this.serviceCategoriesList = data;
    },

    /**
     * @desc   : 根据ID查询数据
     * @author : 宋扬
     * @date   : 2024/3/4 15:43
     */
    detail(id) {
      return this.excuteNoParam(this.$service.suppliersService, this.$service.suppliersService.selectById, [id],false)
    },

    /**
     * @desc   : 查询数据
     * @author : 宋扬
     * @date   : 2024/3/4 15:43
     */
    getData(params) {
      return this.excute(this.$service.suppliersService, this.$service.suppliersService.selectByCond, params)
    },
    /**
     * @desc   : 保存数据
     * @author : 宋扬
     * @date   : 2024/3/4 14:47
     */
    saveData() {
      if (this.modalParams.button === this.$config.formMode.edit) {
        return this.excute(this.$service.suppliersService, this.$service.suppliersService.update, this.params);
      } else {
        return this.excute(this.$service.suppliersService, this.$service.suppliersService.insert, this.params);
      }
    },

    /**
     *   @desc   : 获取业务员相关
     *   @author : 宋扬
     *   @date   : 2024/3/25 15:03
     */
    staffOk(val) {
      if (val && val.length > 0) {
        this.formData.orgId = val[0].orgId
        this.formData.orgCode = val[0].orgCode
        this.formData.orgName = val[0].orgName
      }
    },

    /**
     * @desc   : 获取部门相关
     * @author : 宋扬
     * @date   : 2024/3/25 15:03
     */
    orgOk(val) {
      if (val && val.length > 0) {
        this.formData.orgId = val[0].orgId
        this.formData.orgCode = val[0].orgCode
        this.formData.orgName = val[0].orgName
      }
    },

    /**
     * @desc   : 供应商类别选择事件
     * @author : 宋扬
     * @date   : 2024/3/25 10:26
     */
    onChange(val){
      if("供应商类别-采购"===val){
        this.flgValidBrand=true;
        this.flgValidServiceCategories=false;
      }
      else{
        this.flgValidBrand=false;
        this.flgValidServiceCategories=true;
      }
    },
  },
  created() {
    this.primaryKey = 'supId'  // 设置主键Id
    this.routeObjName = 'suppliers'  // 设置路由名称
    this.comHandleEnableFlag = true; // 开启启用停用功能
  }
}
</script>


