<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']"/>
      </a-form-item>
      <a-form-item style="display:none">
        <a-input v-decorator="['customerId']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="请选择手机号">
<!--        <a-input placeholder="联系人" v-decorator="['name', {rules: [{required: true, message: '请输入联系人'}]}]"/>-->

        <a-select
          show-search
          v-decorator="['mobile', {rules: [{required: true, message: '请选择手机号码'}]}]"
          placeholder="请选择手机号码"
          style="width: 200px"
          :default-active-first-option="false"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
          @search="handleSearch"
          @change="handleChange"
        >
          <a-select-option v-for="d in data" :key="d.id">
            {{ d.mobile }}
          </a-select-option>
        </a-select>

      </a-form-item>
<!--      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">-->
<!--        <a-input placeholder="联系电话" v-decorator="['phone']"/>-->
<!--      </a-form-item>-->
<!--      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">-->
<!--        <a-input placeholder="备注" v-decorator="['remark']"/>-->
<!--      </a-form-item>-->
    </a-form>
  </a-modal>
</template>
<script>
import { saveCustomerContacts,getStaffList,updateStaff,getStaffListNotId } from '@/api/admin/customer'
import pick from 'lodash.pick'
export default {
  name: 'CustomerContactsModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      staffId:'',
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      customerId: undefined,
      contactId: undefined,
      form: this.$form.createForm(this),
      data:[],
    }
  },
  beforeCreate () {
  },
  created () {

  },
  methods: {
    handleSearch(value){
      let param = {
        mobile:value
      }
      getStaffList(param).then(res =>{
        this.data =res.rows
      })
    },
    handleChange(value){
      console.log(value);
      this.staffId = value;
      // fetch(value, data => (this.data = data));
    },

    add (contactInfo) {
      this.customerId = undefined
      this.customerId = contactInfo.customerId
      this.contactId = undefined
      this.contactId = contactInfo.id
      this.form.resetFields()
      this.edit(contactInfo)

      let param = {
        icsCustomerId:contactInfo.customerId
      }
      getStaffListNotId(param).then(res =>{
        this.data =res.rows
      })

    },
    edit (record) {
      this.mdl = Object.assign(record)
      console.log(this.mdl)
      // this.mdl = record
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'customerId', 'name', 'phone', 'remark'))
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          // values.customerId = this.customerId
          console.log('Received values of form: ', values)
          values.id = this.staffId
          console.log(values)
          this.confirmLoading = true
          updateStaff(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
