<template>
  <div>
    <div style="display: flex;justify-content: right;margin-bottom: 15px">
      <el-button type="primary" style="text-align: right" @click="customerFollow.dialogVisible = true">{{$t('新增')}}</el-button>
    </div>

    <el-table
      :data="customerFollowList"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        :label="$t('序号')"
      >
      </el-table-column>
      <el-table-column
        prop="followType"
        :label="$t('跟进类型')"
        :formatter="(row, column, cellValue) => getDictDataLabel(DICT_TYPE.CUSTOMER_FOLLOW_TYPE, cellValue)"
      >
      </el-table-column>
      <el-table-column
        prop="contactName"
        :label="$t('联系人')"
      >
      </el-table-column>
      <el-table-column
        prop="followMethod"
        :label="$t('跟进方式')"
        :formatter="(row, column, cellValue) => getDictDataLabel(DICT_TYPE.CUSTOMER_FOLLOW_METHOD, cellValue)"
      >
      </el-table-column>
      <el-table-column
        prop="followTime"
        :label="$t('跟进时间')"
        :formatter="(row, column, cellValue) => parseTime(cellValue)"
      >
      </el-table-column>
      <el-table-column
        prop="feedback"
        :label="$t('客户反馈')"
      >
      </el-table-column>
      <el-table-column
        prop="result"
        :label="$t('处理结果')"
      >
      </el-table-column>
      <el-table-column
        prop="followUserName"
        :label="$t('客户经理')"
      >
      </el-table-column>
    </el-table>
    <pagination  :total="total" :page.sync="pageNo" :limit.sync="pageSize"
                @pagination="getCustomerFollowList"/>
    <el-dialog
      append-to-body
      :title="$t('客户跟进')"
      :visible.sync="customerFollow.dialogVisible"
      :close-on-click-modal="false"
      :before-close="customerFollowClose"
      width="680px">
      <el-form ref="customerFollowForm" :model="customerFollow.form" label-width="80px">
        <el-row :gutter="10">
          <el-col>
            <el-form-item :label="$t('跟进类型')" required>
              <!-- <dict-selector ref="dictType" form-type="radio" v-model="customerFollow.form.followType" :type="DICT_TYPE.CUSTOMER_FOLLOW_TYPE"></dict-selector> -->
              <el-radio v-model="customerFollow.form.followType" label="3">{{$t('投诉回访')}}</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('跟进时间')" required>
              <el-date-picker v-model="customerFollow.form.followTime" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" type="datetime" :placeholder="$t('选择跟进时间')"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('联系人')" required>
              <el-select v-model="customerFollow.form.contactName" :placeholder="$t('请选择')">
                <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="$t('客户经理')" required>
              <el-select v-model="customerFollow.form.followUserId" :placeholder="$t('请选择')">
                <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="$t('跟进方式')" required>
              <dict-selector ref="dictMethod" v-model="customerFollow.form.followMethod" :type="DICT_TYPE.CUSTOMER_FOLLOW_METHOD"></dict-selector>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item :label="$t('客户反馈')" required>
              <el-input type="textarea" v-model="customerFollow.form.feedback"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item :label="$t('处理结果')" required>
              <el-input type="textarea" v-model="customerFollow.form.result"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="customerFollowClose">{{$t('取 消')}}</el-button>
        <el-button type="primary" @click="customerFollowSubmit">{{$t('确 定')}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {createCustomerFollow, getCustomerFollowPage, getCustomerFollowPage2} 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,
    customerQuery:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      DICT_TYPE,
      getDictDataLabel,
      parseTime,
      customerFollowList: [],
      serviceUserList: [],
      customerContactsList: [],

      customerFollow: {
        dialogVisible: false,
        form: {
          followType:'3'
        }
      },
      pageNo:1,
      pageSize:10,
      total:10,
    }
  },

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

  }
}
</script>

<style scoped>

</style>