<!-- @desc:客户管理新建编辑  @auth:付斌  @time:2024/1/30 15:15 -->
<template>
  <div class="main-div">
    <DkCollapse @on-change="changeCollapse" ref="collapse">
      <DkPanel prop="essentialInformation">
        <!--  下拉区域  -->
        <DkForm slot="content" ref="formInline" v-model="formData" style="width: 95%">
          <!--客户编码-->
          <DkFormItem prop="cusCode">
            <InputPop v-model="formData.cusCode" :readonly="true"/>
          </DkFormItem>
          <!--客户名称-->
          <DkFormItem prop="cusName" :required="true">
            <InputPop v-model="formData.cusName"/>
          </DkFormItem>

          <DkFormItem prop="cusPhone" :required="true">
            <InputPop :telephone="true" v-model="formData.cusPhone" maxlength="11"/>
          </DkFormItem>
          <!--联系人-->
          <DkFormItem prop="contactName">
            <InputPop v-model="formData.contactName"/>
          </DkFormItem>
          <!--联系电话-->
          <DkFormItem prop="contactPhone">
            <InputPop :telephone="true" v-model="formData.contactPhone" maxlength="11"/>
          </DkFormItem>
          <!--行政区划-->
          <DkFormItem prop="addressName" required>
            <RegionsChoose v-model="regionData" :options="cityData" :default-value="formData.addressName"
                           ref="customerDistrict"
                           @on-choose="chooseAddress"></RegionsChoose>
          </DkFormItem>
          <!--门牌号-->
          <DkFormItem :label="$t('addressNo')" prop="addressNo" required>
            <InputPop v-model="formData.addressNo" @input="setAddressFull"/>
          </DkFormItem>
          <!--详细地址-->
          <DkFormItem prop="addressFull" :required="true">
            <InputPop ref="addressCustomerFull" :readonly="true" v-model="formData.addressFull"/>
          </DkFormItem>
          <!--业务员-->
          <DkFormItem prop="staffId" :required="true">
            <SelectMagnifier v-model="formData.staffId"
                             :multiple="false"
                             :type="this.$config.MagnifierType.staff"
                             :display-text="formData.staffName"/>
          </DkFormItem>
          <!--部门-->
          <DkFormItem prop="orgId" :required="true">
            <SelectMagnifier v-model="formData.orgId"
                             :multiple="false"
                             :type="this.$config.MagnifierType.org"
                             :display-text="formData.orgName"/>
          </DkFormItem>
          <!-- 渠道 -->
          <DkFormItem prop="channelId" :required="true">
            <SelectPop v-model="formData.channelId" :options="channelList" :multiple="false"
                       :clearable="false"
                       label-key="channelName" value-key="channelId"/>
          </DkFormItem>
          <!--客成状态-->
          <DkFormItem prop="saleStatus" v-if="type != 'add'">
<!--            <SelectPop v-model="formData.saleStatusName" :options="saleStatusList" :multiple="false"-->
<!--                       :clearable="false"-->
<!--                       label-key="dataName" value-key="dataCode" :disabled="type == 'edit'"/>-->
            <InputPop v-model="formData.saleStatusName" :readonly="true"/>
          </DkFormItem>
          <!--客户来源-->
          <DkFormItem prop="cusFrom">
            <SelectPop v-model="formData.cusFrom" :options="customerFromList" :multiple="false"
                       :clearable="false"
                       label-key="dataValue" value-key="dataId"/>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks" :span="24">
            <InputPop v-model="formData.remarks"/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
    </DkCollapse>

    <!--  下部分按钮区域  -->
    <DkSaveButton ref="saveButton" :loading="loading" @save="save" @close="btnGroupClick"></DkSaveButton>
  </div>
</template>

<script>
import {formMixin} from '@/mixins/form'

export default {
  components: {},
  mixins: [formMixin],
  data() {
    let self = this
    return {
      // 客户来源list
      customerFromList: [],
      // 渠道list
      channelList:[],
      // 客成状态list
      saleStatusList: [],
      loading: false,
      formData: {
        cusId: null,
        cusName: null,
        cusCode: null,
        cusPhone: null,
        contactName: null,
        contactPhone: null,
        saleStatusName: null,
        staffId:null,
        staffName:null,
        reportStaff: self.$store.state.user.id,
        reportStaffName: self.$store.state.user.userName,
        addressArea: null,
        addressName: null,
        addressNo: null,
        addressFull: null,
        customerDistrict: null,
        cusFrom:null,
        cusFromName:null,
        bankAccount: null,
        remarks: '',
        cpId: self.$store.state.user.cpId,
        orgId: null,
        orgName: null,
        channelName: null
      },
      //cpIds
      cpIds: [],

      regionData: [],
      cityData: [],
    }
  },
  methods: {
    /**
     * @desc   : 选择地址
     * @author : 付斌
     * @date   : 2024-02-01 14:30
     */
    chooseAddress(value) {
      this.formData.customerDistrict = value;
      // 说明是清空
      if (!value) {
        this.formData.addressName = null;
        this.formData.addressArea = {};
        this.formData.addressFull = null;
        this.formData.addressGcj02 = {};
      } else {
        //按照title进行
        this.formData.addressName = value.addressName;
        this.formData.addressArea = value.addressArea;
        this.formData.addressGcj02 = value.addressGcj02;
        this.formData.addressFull = value.addressFull + (this.formData.addressNo ? this.formData.addressNo : '');
      }
    },
    /**
     * @desc   : 拼写详细地址
     * @author : 付斌
     * @date   : 2024-02-01 14:30
     */
    setAddressFull(e) {
      // 客户区域有值才会拼接详细地址
      if (this.formData.customerDistrict && this.formData.customerDistrict.addressFull) {
        this.formData.addressFull = this.formData.customerDistrict.addressFull + (this.formData.addressNo ? this.formData.addressNo : '')
      }
    },
    /**
     * @desc   : 保存的提交事件
     * @author : 付斌
     * @date   : 2024-02-01 14:30
     */
    submitForm() {

    },
    /**
     * @desc   : 新建完毕后清空窗体
     * @author : 付斌
     * @date   : 2024-02-01 14:30
     */
    clear() {
      this.formData.cusId = null
      this.formData.cusName = null
      this.formData.cusCode = null
      this.formData.cusPhone = null
      this.formData.contactName = null
      this.formData.contactPhone = null
      this.formData.addressFull = null
      this.formData.addressNo = null
      this.formData.addressName = null
      this.formData.cusFrom = null
      this.formData.cusFromName = null
      this.formData.channelId = null
      this.formData.channelName = null
      this.formData.saleStatusName = null
      this.formData.remarks = null
    },

    /**
    * @desc   : 给参数赋值
    * @author : 常皓宁
    * @date   : 2024/4/9 13:44
    */
    setParams(){
      this.params = {...this.formData}
    },

    /**
     * @desc   : 保存
     * @author : 付斌
     * @date   : 2024-02-01 14:30
     */
    saveData() {
      console.log('this.params', this.params)
      return this.type === this.$config.formMode.add ?
        this.excute(this.$service.customerService, this.$service.customerService.insert, this.params)
        : this.excute(this.$service.customerService, this.$service.customerService.update, this.params)
    },
    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2024-02-01 14:30
     */
    detail(id) {
      return this.excuteNoParam(this.$service.customerService, this.$service.customerService.selectById, [id])
    },

    /**
     * @desc   : 编辑页面赋值
     * @author : 常皓宁
     * @date   : 2024/2/29 9:49
     */
    setValuesByEdit(data) {
      console.log('data',data)
      this.formData.cusId = data.cusId
      this.formData.cusName = data.cusName
      this.formData.cusCode = data.cusCode
      this.formData.cusPhone = data.cusPhone
      this.formData.contactName = data.contactName
      this.formData.contactPhone = data.contactPhone
      this.formData.addressFull = data.addressFull
      this.formData.addressNo = data.addressNo
      this.formData.addressName = data.addressName
      this.formData.cusFrom = data.cusFrom
      this.formData.cusFromName = data.cusFromName
      this.formData.channelId = data.channelId
      this.formData.channelName = data.channelName
      this.formData.saleStatusName = data.saleStatusName
      this.formData.staffId = data.staffId
      this.formData.staffName = data.staffName
      this.formData.orgId = data.orgId
      this.formData.orgName = data.orgName
      this.formData.remarks = data.remarks
      // 设置地址（不包括门牌号）
      this.formData.customerDistrict = {}
      if (data.addressFull) {
        this.formData.customerDistrict.addressFull = data.addressFull.replace(data.addressNo, '')
      }
      console.log('this,',this.formData)
      this.formData.annexPaths = data.annexPaths
    },


    /**
     * @desc   : 返回一览页面
     * @author : 付斌
     * @date   : 2024-02-01 14:31
     */
    btnGroupClick() {
      this.formData = {}
      this.closeTag(this.$route)
    },
    /**
     * @desc   : 查询集团字典
     * @author : 付斌
     * @date   : 2024-02-01 14:31
     */
    getDictionaryData() {

    },
    /**
     * @desc   : 返回预览页面
     * @author : 付斌
     * @date   : 2024-02-01 14:31
     */
    returnUpperLevel() {
      this.$router.push('/sale/customer/index')
    },
    /**
     * @desc   : 选择标签
     * @author : 付斌
     * @date   : 2024-02-01 14:31
     */
    selectTag(checked, item) {

    },
    /**
    * @desc   : 获取客户来源
    * @author : 常皓宁
    * @date   : 2024/4/9 10:06
    */
    getCusFrom(){
      let params = {
        dataSourceCode: 'customerFrom'
      }
      this.excute(this.$service.commonService, this.$service.commonService.getInitData, params, false).then(res => {//调用api查询方法
        if (res.code === this.$config.SUCCESS_CODE) {
          this.customerFromList = res.data
        }
      })
    },

    /**
     * @desc   : 获取客户渠道
     * @author : 常皓宁
     * @date   : 2024/4/9 10:06
     */
    getChannel(){
      let params = {
        dataSourceCode: 'saleChannel'
      }
      this.excute(this.$service.commonService, this.$service.commonService.getInitData, params, false).then(res => {//调用api查询方法
        if (res.code === this.$config.SUCCESS_CODE) {
          this.channelList = res.data
        }
      })
    },

    /**
     * @desc   : 查询事件
     * @author : 付斌
     * @date   : 2024-02-01 14:24
     */
    initData() {
      this.getCusFrom()
      this.getChannel()
    },
  },
  created() {
    this.focusItem = 'cusName';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'cusId'  // 设置主键Id
    // 查询集团字典
    this.getDictionaryData()
  },
}
</script>
