<template>
    <a-modal
      title="转让潜客"
      ok-text="确认" 
      cancel-text="取消"
      :visible="changeShow"
      :confirm-loading="changeLoading"
      @ok="changeSalesOk"
      @cancel="changeSalesCancel">
      <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;"><span style="color:tomato;">*</span>顾问选择:</span>
        <a-select style="width: 176px;margin-left: 8px;" placeholder="请选择顾问" v-model="sales" >
          <template v-for="(item)  in lecturerOptions" >
            <a-select-option :value="item.value" v-if="item.value === salesOp" disabled>
              {{item.text}}
            </a-select-option>
            <a-select-option :value="item.value" v-else>
              {{item.text}}
            </a-select-option>
          </template>
        </a-select>
      </a-col>
    </a-row>
  </a-modal>
  </template>
  <script>
  
  import { postAction } from '@/api/manage'
  import {ajaxGetDictItems} from '@/api/api'
  import store from '@/store'
  export default {
    name: 'CustomerChangeSales',
    data () {
      return {
        changeLoading: false,
        sales: undefined,
        salesOp: undefined,
        lecturerOptions: undefined,
      }
    },
    props: ['changeShow','dataPara'],
    watch: {
      immediate: true,
      changeShow: {
        handler() {
          this.salesOp = this.dataPara.saleCode;
          ajaxGetDictItems('sys_user,realname,id', null).then((res) => {
            if (res.success) {
                this.lecturerOptions = res.result;
            }
          });
        },
      }
    },
    methods: {
        changeSalesOk() {
        if(this.sales === undefined || this.sales === null) {
          this.$message.warn('请选择顾问.');
          return;
        } else {
          let newSales = this.lecturerOptions.find(item => {return item.value == this.sales }).text;
          let params = {
            id: this.dataPara.id,
            saleCode: this.sales,
            newSales: newSales,
            oldSales: this.dataPara.saleCode_dictText,
            createBy: store.getters.userInfo.username,
            schoolDistrictId: this.dataPara.schoolDistrictId
          }
          this.changeLoading = true;
          postAction("/customer/customerInfo/changeSales", params).then((res) => {
            if (res.success) {
              this.$emit('ok');
              this.$message.success(res.message);
            } else {
              this.$message.warn('修改失败:' + res.message);
            }
          }).finally(() => {
            this.$emit('change-event', false);
            this.changeLoading = false;
            this.changeSaleRow = {};
            this.sales = undefined;
          });
        }
      },
      changeSalesCancel() {
        this.$emit('change-event', 'change');
        this.changeLoading = false;
        this.changeSaleRow = {};
        this.sales = undefined;
      },
    }
  }
  </script>
  