<template>
  <div>
    <el-dialog
      append-to-body
      title="客户跟进"
      :visible.sync="customerFollow.dialogVisible"
      :close-on-click-modal="false"
      :before-close="customerFollowClose"
      width="680px">
      <el-form ref="customerFollowForm" :model="form" label-width="80px">
        <el-row :gutter="10">
          <el-col>
            <el-form-item label="跟进类型" required>
              <dict-selector ref="dictTag" form-type="radio" v-model="form.followType" :type="DICT_TYPE.CUSTOMER_FOLLOW_TYPE"></dict-selector>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跟进时间" required>
              <el-date-picker v-model="form.followTime" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择跟进时间"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" required>
              <el-select v-model="form.contactName" placeholder="请选择">
                <el-option
                  v-for="(item, index) in customerContactsList"
                  :key="index"
                  :label="item.name"
                  :value="item.name">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跟进业务" required>
              <el-select v-model="form.followUserId" placeholder="请选择">
                <el-option
                  v-for="item in serviceUserList"
                  :key="item.id"
                  :label="item.nickname"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跟进方式" required>
              <dict-selector ref='dictMethod' v-model="form.followMethod" :type="DICT_TYPE.CUSTOMER_FOLLOW_METHOD"></dict-selector>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="客户反馈" required>
              <el-input type="textarea" v-model="form.feedback"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="处理结果" required>
              <el-input type="textarea" v-model="form.result"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="customerFollowClose">取 消</el-button>
        <el-button type="primary" @click="customerFollowSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {createCustomerFollow, getCustomerFollowPage} from "@/api/ecw/customerFollow"
import { DICT_TYPE, getDictDataLabel } from '@/utils/dict'
import { getCustomerContactsListByCustomer } from '@/api/ecw/customerContacts'
import {listServiceUser} from "@/api/system/user"
import { parseTime } from '@/utils/ruoyi'
export default {
  /**
   * 客户跟进
   * 客户投诉跟进
   */
  name: "CustomerFollow",
  props: {
    /**
     * 如果是客户投诉跟进，则id为客户投诉id；如果是客户跟进，则id为客户id
     */
    id: Number,
    customerId: Number,
  },
  data() {
    return {
      DICT_TYPE,
      getDictDataLabel,
      parseTime,
      customerFollowList: [],
      serviceUserList: [],
      customerContactsList: [],
      customerFollow:{
        dialogVisible: false,
      },
      form: {}
    }
  },

  created() {
    // this.resetCustomerFollowForm()
    if (!!this.customerId) getCustomerContactsListByCustomer({customerId: this.customerId}).then(r => {
      this.customerContactsList = r.data
      // this.$set(form, 'customerId', this.customerId)
      // this.customerFollow.form.customerId = this.customerId
    })
    listServiceUser().then(r => {
      this.serviceUserList = r.data
    })
  },
  methods: {
    customerFollowSubmit() {
      console.log(this.form)
      this.$refs["customerFollowForm"].validate(valid => {
        if (!valid) {
          return
        }
       if(!this.form.followType){
          this.$modal.msgError("请选择跟进类型");
          return
        }
        if(!this.form.followTime){
          this.$modal.msgError("请选择跟进时间");
          return
        }
        if(!this.form.contactName){
          this.$modal.msgError("请选择联系人");
          return
        }
        if(!this.form.followUserId){
          this.$modal.msgError("请选择跟进业务");
          return
        }
        if(!this.form.followMethod){
          this.$modal.msgError("请选择跟进方式");
          return
        }
        if(!this.form.feedback){
          this.$modal.msgError("请输入客户反馈");
          return
        }
        if(!this.form.result){
          this.$modal.msgError("请输入处理结果");
          return
        }
        this.$set(this.form, 'customerId', this.customerId)
        createCustomerFollow(this.form).then(r => {
          this.customerFollowClose()
        })
      })
    },
    customerFollowClose(){
      this.resetCustomerFollowForm()
      this.customerFollow.dialogVisible = false
    },
    resetCustomerFollowForm() {
      var form = {
        "bizId":this.id,
        "contactName": undefined,
        "feedback": undefined,
        "followMethod": undefined,
        "followTime": undefined,
        "followType": undefined,
        "followUserId": undefined,
        "result": undefined
      }
      this.form = Object.assign({},form)
      this.$refs.dictTag.changeValue(this.form.followType);
       this.$refs.dictMethod.changeValue(this.form.followMethod);
    },

  },
  watch:{
    customerId(val){
      this.form.bizId = val;
      if (!!this.customerId) getCustomerContactsListByCustomer({customerId: this.customerId}).then(r => {
        this.customerContactsList = r.data
      })
    }
  }
}
</script>

<style scoped>

</style>
