<template>
  <div>
    <!--    跟进-->
    <DkModal v-model="visible" :title="$t('follow2')" width="1400px" ref="modal_follow"
             :loading="loading"
             @on-cancel="cancelFollow"
             @modalOk="submitFollow"
             @modalCancel="cancelFollow"
             :saveFlag="true"
    >
      <div style="height: 600px;overflow-y:auto;overflow-x:hidden;width: 100%;">
        <DkCollapse @on-change="changeCollapse" ref="collapse">
          <DkPanel prop="customerInfo">
            <DkForm slot="content" style="width:100%;" v-model="customerInfo" name="dk-form" ref="customerInfo">
              <!--客户名称-->
              <DkFormItem prop="cusName" :required="true">
                <InputPop v-model="customerInfo.cusName"/>
              </DkFormItem>
              <!--客户电话-->
              <DkFormItem prop="cusPhone"
                          :required="!customerInfo.qvExternalUserid">
                <InputPop :telephone="true" v-model="customerInfo.cusPhone"/>
              </DkFormItem>
              <!--行政区划-->
              <DkFormItem prop="addressName" required>
                <RegionsChoose v-model="regionData" :options="cityData" :default-value="customerInfo.addressName"
                               ref="customerDistrict"
                               @on-choose="chooseAddress"></RegionsChoose>
              </DkFormItem>
              <!--门牌号-->
              <DkFormItem :label="$t('addressNo')" prop="addressNo">
                <InputPop v-model="customerInfo.addressNo" @input="setAddressFull"/>
              </DkFormItem>
              <!--详细地址-->
              <DkFormItem prop="addressFull" :required="true">
                <InputPop ref="addressCustomerFull" :readonly="true" v-model="customerInfo.addressFull" maxlength="50"/>
              </DkFormItem>
            </DkForm>
          </DkPanel>

          <Card class="follow-card">
            <p slot="title">
              {{ $t('followInfo') }}
            </p>
            <DkTabs v-model="followTab" :options="tabsOptions">
              <template slot="1">
                <DkForm ref="formFollow1" style="width:100%;margin-top: 10px;" name="dk-form" v-model="customerInfo1" :rules="rule1"
                        @onValidate="onValidate" :label-max-words="6">
                  <!--客户意向-->
                  <DkFormItem prop="intention" :required="true">
                    <SelectPop v-model="customerInfo1.intention" :options="intentionList" :multiple="false"
                               :clearable="false"
                               label-key="kindName" value-key="kindCode" transfer
                               @on-change="(val)=>cleanCompetitiveBrands(val, '1')"/>
                  </DkFormItem>
                  <!--下次邀约时间（提醒时间）-->
                  <DkFormItem prop="nextFollowTime" :required="true" :data-type="$config.dataType.date">
                    <DatePickerPop v-model="customerInfo1.nextFollowTime"
                                   :placeholder="$t('inputWords',{'search-name':$t('DeliveryDate')})"
                                   type="datetime" :short-cut-flag="true"/>
                  </DkFormItem>
                  <!--留店时长-->
                  <DkFormItem prop="stayTimeLen" :required="true">
                    <SelectPop v-model="customerInfo1.stayTimeLen" :options="stayTimeLen" :multiple="false"
                               :clearable="false"
                               label-key="kindName" value-key="kindCode" transfer/>
                  </DkFormItem>
                  <!--跟进内容-->
                  <DkFormItem prop="followData" :required="true">
                    <InputPop textarea-flag v-model="customerInfo1.followData" maxlength="100"/>
                  </DkFormItem>
                </DkForm>
                <Card class="follow-card">
                  <p slot="title">
                    {{ $t('appendix') }}
                  </p>
                  <DkPicWall v-model="customerInfo1.annexPaths"
                             folder="t_csm_follow/annex_paths"></DkPicWall>
                </Card>
              </template>
              <template slot="2">
                <DkForm ref="formFollow2" style="width:100%; margin-top: 10px;" name="dk-form" v-model="customerInfo2" :rules="rule2"
                        @onValidate="onValidate" :label-max-words="6">
                  <!-- 客户意向-->
                  <DkFormItem prop="intention" :required="true">
                    <SelectPop v-model="customerInfo2.intention" :options="intentionList" :multiple="false"
                               :clearable="false"
                               label-key="kindName" value-key="kindCode" transfer
                               @on-change="(val)=>cleanCompetitiveBrands(val, '2')"/>
                  </DkFormItem>
                  <!--邀约结果-->
                  <DkFormItem prop="inviteResult" :required="true">
                    <SelectPop v-model="customerInfo2.inviteResult" :options="inviteResultList" :multiple="false"
                               :clearable="false"
                               label-key="kindName" value-key="kindCode" transfer @on-change="changeInviteResult"/>
                  </DkFormItem>
                  <!--邀约时间-->
                  <DkFormItem :label="$t('inviteTime2')" prop="inviteTime"  :data-type="$config.dataType.date">
                    <DatePickerPop type="datetime" v-model="customerInfo2.inviteTime" transfer
                                   :placeholder="$t('inputWords',{'search-name':$t('inviteTime')})"
                                   :short-cut-flag="true"/>
                  </DkFormItem>
                  <!--下次邀约时间（提醒时间）-->
                  <DkFormItem :label="$t('nextFollowTime2')" prop="nextFollowTime">
                    <DatePickerPop type="datetime" v-model="customerInfo2.nextFollowTime" transfer
                                   format="yyyy-MM-dd HH:mm:ss"/>
                  </DkFormItem>
                  <!--跟进内容-->
                  <DkFormItem ref="formFollow2_followData" prop="followData">
                    <InputPop textarea-flag v-model="customerInfo2.followData" maxlength="100"/>
                  </DkFormItem>
                </DkForm>
                <Card class="follow-card">
                  <p slot="title">
                    {{ $t('appendix') }}
                  </p>
                  <DkPicWall v-model="customerInfo2.annexPaths"
                             folder="t_csm_follow/annex_paths"></DkPicWall>
                </Card>
              </template>
              <template slot="3">
                <DkForm ref="formFollow3" style="width:100%; margin-top: 10px;" name="dk-form" v-model="customerInfo3" :rules="rule3"
                        @onValidate="onValidate" :label-max-words="6">
                  <!--设计师-->
                  <DkFormItem prop="designUser" :required="true">
                    <SelectMagnifier v-model="customerInfo3.designUser"
                                     :multiple="false"
                                     :type="this.$config.MagnifierType.staff"
                                     :display-text="customerInfo3.staffName"/>
                  </DkFormItem>
                  <!--邀约结果-->
                  <DkFormItem prop="inviteResult" :required="true">
                    <SelectPop v-model="customerInfo3.inviteResult" :options="inviteResultList" :multiple="false"
                               :clearable="false"
                               label-key="kindName" value-key="kindCode" transfer/>
                  </DkFormItem>
                  <!--邀约时间-->
                  <DkFormItem :label="$t('inviteTime3')" prop="inviteTime">
                    <DatePickerPop type="datetime" v-model="customerInfo3.inviteTime" transfer
                                   format="yyyy-MM-dd HH:mm:ss"
                                   :readonly="!customerInfo3.inviteResult || customerInfo3.inviteResult == $config.basicDataCode.inviteResult2"/>
                  </DkFormItem>
                  <!--下次邀约时间（提醒时间）-->
                  <DkFormItem :label="$t('nextFollowTime2')" prop="nextFollowTime">
                    <DatePickerPop type="datetime" v-model="customerInfo3.nextFollowTime" transfer
                                   format="yyyy-MM-dd HH:mm:ss"/>
                  </DkFormItem>
                  <!--跟进内容-->
                  <DkFormItem prop="followData">
                    <InputPop textarea-flag v-model="customerInfo3.followData" maxlength="100"/>
                  </DkFormItem>
                </DkForm>
                <Card class="follow-card">
                  <p slot="title">
                    {{ $t('appendix') }}
                  </p>
                  <DkPicWall v-model="customerInfo3.annexPaths"
                             folder="t_csm_follow/annex_paths"></DkPicWall>
                </Card>
              </template>
            </DkTabs>
          </Card>
        </DkCollapse>
      </div>
    </DkModal>
  </div>
</template>

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

export default {
  components: {},
  mixins: [formMixin],
  name: 'cus-follow',
  model: {
    prop: 'value',
    event: 'close'
  },
  watch: {
    value(val) {
      this.visible = val
    },
    visible(val) {
      if (!val) {
        this.$emit('close', false)
      } else {
      }
    },
    goodsSpecsListBackUp() {
      //设置意向
      this.setGoodsSpecs()
    },
    customerData() {
      this.customerInfo = this.customerData
    },
    'customerInfo3.inviteResult'(val) {

    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    //  商品规格品类备份
    goodsSpecsListBackUp: {
      type: Array
    },
    customerData: {
      type: Object
    },
    intentionList: {
      type: Array
    },
    inviteResultList: {
      type: Array
    },
    stayTimeLen: {
      type: Array
    }
  },
  data() {
    let self = this
    return {
      tabsOptions: [
        { label: self.$t('followDaily'), name: '1' },
        { label: self.$t('followToStore'), name: '2' },
        { label: self.$t('followReserve'), name: '3' }
      ],
      cityData: [],
      customerInfo: self.customerData,
      // 客户信息 日常跟进
      customerInfo1: {
        intention: null,//客户意向
        followData: null,//跟进内容
        nextFollowTime: null,//提醒时间
        stayTimeLen:null,//留店时长
        annexPaths: null,
      },
      // 客户信息 邀约进店
      customerInfo2: {
        intention: null,//客户意向
        inviteResult: null,//邀约结果
        inviteTime: null,//邀约时间
        followData: null,//跟进内容
        nextFollowTime: null,//提醒时间
        annexPaths: null,
      },
      // 客户信息 预约量尺
      customerInfo3: {
        designUser: null,//设计师
        inviteResult: null,//邀约结果
        inviteTime: null,//邀约时间
        followData: null,//跟进内容
        nextFollowTime: null,//提醒时间
        annexPaths: null,
      },
      // 错误信息
      errMessage: {},
      // 跟进的tab切换
      followTab: '1',
      otherIntentionInfo: null,
      regionData: [],
      // 日常跟进校验
      rule1: {
        followData: [
          {required: true, trigger: 'none'},
        ],
        followType: [
          {required: true, trigger: 'none'},
        ],
        intention: [
          {required: true, trigger: 'none'},
        ],
        loseReasonIds: [
          {required: true, trigger: 'none', type: 'array'},
        ],
      },
      // 邀约进店校验
      rule2: {
        intention: [
          {required: true, trigger: 'none'},
        ],
        followData: [
          {required: true, trigger: 'none'},
        ],
        inviteResult: [
          {required: true, trigger: 'none'},
        ],
        inviteTime: [
          {required: true, trigger: 'none', type: 'date'},
        ],
        followType: [
          {required: true, trigger: 'none'},
        ],
        loseReasonIds: [
          {required: true, trigger: 'none', type: 'array'},
        ],
      },
      // 预约量尺校验
      rule3: {
        followData: [
          {required: true, trigger: 'none'},
        ],
        inviteResult: [
          {required: true, trigger: 'none'},
        ],
        inviteTime: [
          {required: true, trigger: 'none', type: 'date'},
        ],
        followType: [
          {required: true, trigger: 'none'},
        ],
      },
      visible: false,
      loading: false,
    }
  },
  methods: {
    /**
     * @desc   : checkBox修改意向品类
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    selectIntentionInfo(val) {
      if (val.value) {
        val.count = 1
      } else {
        val.count = 0
      }
    },
    /**
     * @desc   : 关闭跟进弹窗
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    cancelFollow() {
      this.visible = false
      this.$refs['formFollow' + this.followTab].$refs['dk-form'].resetFields()
      this.$refs.customerInfo.$refs['dk-form'].resetFields()
      this['customerInfo' + this.followTab].annexPaths = null
      // 清空客户区域
      if (this.$refs.customerDistrict) {
        this.$refs.customerDistrict.clear();
      }
      if (this.followTab == '1') {
        this.otherIntentionInfo = null
        //设置意向
        this.setGoodsSpecs()
      }
    },
    /**
     * @desc   : 改变邀约结果
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    changeInviteResult(val) {
      if (val == this.$config.basicDataCode.inviteResult1) {
        this.customerInfo2.nextFollowTime = null
        this.customerInfo2.followData = ''
        this.$set(this, 'rule2', {
          intention: [
            {required: true, trigger: 'none'},
          ],
          followData: [
            {required: true, trigger: 'none'},
          ],
          inviteResult: [
            {required: true, trigger: 'none'},
          ],
          inviteTime: [
            {required: true, trigger: 'none', type: 'date'},
          ],
          followType: [
            {required: true, trigger: 'none'},
          ],
          loseReasonIds: [
            {required: true, trigger: 'none', type: 'array'},
          ],
        })
      } else {
        this.customerInfo2.inviteTime = null
        this.$set(this, 'rule2', {
          intention: [
            {required: true, trigger: 'none'},
          ],
          followData: [
            {required: false, trigger: 'none'},
          ],
          inviteResult: [
            {required: true, trigger: 'none'},
          ],
          inviteTime: [
            {required: false, trigger: 'none', type: 'date'},
          ],
          followType: [
            {required: true, trigger: 'none'},
          ],
          loseReasonIds: [
            {required: true, trigger: 'none', type: 'array'},
          ],
        })
        this.$refs['formFollow2_inviteTime'].$refs.formItem.resetField()
        // this.$refs['formFollow2_followData'].$refs.formItem.resetField()
        this.customerInfo2.followData = '拒绝邀约'
      }
    },
    /**
     * @desc   : 选择地址
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    chooseAddress(value) {
      this.customerInfo.customerDistrict = value;
      // 说明是清空
      if (!value) {
        this.customerInfo.addressName = null;
        this.customerInfo.addressFull = null;
        this.customerInfo.addressGcj02 = {};
        this.customerInfo.addressArea = {};
      } else {
        //按照title进行
        this.customerInfo.addressName = value.addressName;
        this.customerInfo.addressGcj02 = value.addressGcj02;
        this.customerInfo.addressArea = value.addressArea;
        this.customerInfo.addressFull = value.addressFull + (this.customerInfo.addressNo ? this.customerInfo.addressNo : '');
      }
    },
    /**
     * @desc   : 清除竞争品牌
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    cleanCompetitiveBrands(val, col) {

    },
    /**
     * @desc   : 校验
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    getErrMessage(prop) {
      return this.errMessage[prop]
    },
    /**
     * @desc   : 查询流失原因
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    getDictionaryData() {

    },
    /**
     * @desc   : 校验
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    onValidate({prop, status, error}) {
      this.$set(this.errMessage, prop, error)
    },
    /**
     * @desc   : 意向 其他分类选中事件
     * @author : 付斌
     * @date   : 2024-02-01 14:33
     */
    radioChange(val) {

    },
    /**
     * @desc   : 拼写详细地址
     * @author : 付斌
     * @date   : 2024-02-01 14:34
     */
    setAddressFull(e) {

    },
    /**
     * @desc   : 设置意向
     * @author : 付斌
     * @date   : 2024-02-01 14:34
     */
    setGoodsSpecs() {

    },
    /**
     * @desc   : 提交跟进
     * @author : 付斌
     * @date   : 2024-02-01 14:34
     */
    submitFollow() {
      this.$refs['formFollow' + this.followTab].validate().then(valid => {
        // 校验
        if (!valid) {
          return
        }
        this.$refs.customerInfo.validate().then(valid2=>{
          // 校验
          if (!valid2) {
            return
          }

          let param = Object.assign({
            cusId: this.customerInfo.cusId,
            followOrg:this.$store.state.user.orgId,
            followStatus: this.$config.basicDataCode['followStatus' + this.followTab],
          }, this['customerInfo' + this.followTab])

          param = Object.assign(param, {
            cusName: this.customerInfo.cusName,
            cusPhone:this.customerInfo.cusPhone,
            addressFull: this.customerInfo.addressFull,
            addressNo: this.customerInfo.addressNo,
            addressArea: this.customerInfo.addressArea,
            addressGcj02: this.customerInfo.addressGcj02,
            addressName: this.customerInfo.addressName,
          })

          // 下次邀约时间
          if (param.nextFollowTime && param.nextFollowTime instanceof Date) {
            param.nextFollowTime = param.nextFollowTime.format()
          }
          // 邀约时间
          if (param.inviteTime) {
            param.inviteTime = param.inviteTime.format()
          }

          /* 沈博 2022年10月12日10:20:57 */
          let requestBody = {...param}

          //附件
          if (requestBody.annexPaths) {
            requestBody.annexPaths = requestBody.annexPaths.map(m => m.path)
          }
          this.loading = true
          //保存
          this.excute(this.$service.cusFollowService, this.$service.cusFollowService.insert, param).then(res => {
            this.loading = false
            if (res.code === this.$config.SUCCESS_CODE) {
              this.$Message.success(res.message)
              //关闭跟进弹窗
              this.cancelFollow()
            } else {
              this.$Message.error(res.message)
            }
          })

        })
      })
    },
    /**
     * @desc   : 上传文件
     * @author : 付斌
     * @date   : 2024-02-01 14:34
     */
    uploadFile(response, file, fileList) {
      if (response.code == 200) {
        if (!this['customerInfo' + this.followTab].files) {
          this['customerInfo' + this.followTab].files = []
        }
        this['customerInfo' + this.followTab].files.push(response.data)
      } else {
        this.$Message.error(response.message)
      }
    },
  },
  created() {
    if (this.customerInfo && this.customerInfo.addressFull) {
      // console.log('ttt',this.customerInfo)
      // 设置地址（不包括门牌号）
      this.customerInfo.customerDistrict = {}
      this.customerInfo.addressFull = this.customerInfo.addressFull.replace(this.customerInfo.addressNo, '')
    }
    //设置意向
    this.setGoodsSpecs()
    // 查询流失原因
    this.getDictionaryData()
    //设置默认城市
    if (this.$store.state.user.cityName) {
      let city = this.$store.state.user.cityName;
      // 给城市赋值
      this.cityData.push({key: city, value: city})
    }
  }
}
</script>
