<!-- 选择估价员 -->
<template>
  <a-drawer width="30%" :visible="visible" title="指派估价员" @close="close">
    <a-steps :current="step" @change="stepChange">
      <a-step title="选择估价员" />
      <a-step title="选择上门时间" />
    </a-steps>
    <br />
    <div v-show="step == 0">
      <a-form layout="inline">
        <a-form-item label="名称">
          <a-input v-model="name" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" icon="search" @click="search" />
        </a-form-item>
      </a-form>
      <br />

      <a-radio-group v-model="id">
        <template v-for="(valuator, i) in valuators">
          <div class="valuator-selector-item-box" :key="i">
            <a-radio :value="valuator.id" />
            <div class="valuator-selector-item">
              <p>姓名：{{ valuator.name }}</p>
              <p>电话：{{ valuator.phone }}</p>
              <div class="valuator-selector-item-assign-list">
                <template v-for="(assign, idx) in valuator.assignTime">
                  <p :key="idx">已派时间：{{ assign }}</p>
                </template>
              </div>
            </div>
          </div>
        </template>
      </a-radio-group>
    </div>
    <div v-show="step == 1">
      <span>选择上门时间</span>
      <a-form :form="form">
        <a-form-item label="上门时间">
          <a-date-picker
            size="large"
            v-decorator="[
              'expectArrivalTime',
              { rules: [{ required: true, message: '请选择上门时间' }] }
            ]"
            format="YYYY-MM-DD HH:mm"
            valueFormat="YYYY-MM-DD HH:mm"
            :show-time="{
              format: 'HH:mm',
              valueFormat: 'HH:mm',
              minuteStep: 30,
              defaultValue: defaultTime
            }"
            :show-today="false"
          />
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea
            v-decorator="[
              'remark',
              { rules: [{ required: true, message: '请填写备注' }] }
            ]"
            :autoSize="{ minRows: 5 }"
          />
        </a-form-item>
      </a-form>
    </div>
    <div class="drawer-form-bottom-toolbar">
      <a-button v-show="step == 0" type="primary" @click="choose">
        选择
      </a-button>
      <a-button type="primary" v-show="step == 1" @click="dispatch">
        指派
      </a-button>
    </div>
  </a-drawer>
</template>
<script>
import { getValuators } from '@/api/valuator'
import { dispatchValuator, reDispatchValuator } from '@/api/order'
import moment from 'moment'

export default {
  name: 'ChooseValuator',
  props: ['afterChoose', 'orderId'],
  data() {
    return {
      visible: false,
      id: null,
      name: null,
      valuators: [],
      step: 0,
      form: this.$form.createForm(this),
      mode: null,
      defaultTime: moment("1995-12-25 00:00")
    }
  },
  methods: {
    choose() {
      if (!this.id) {
        return
      }
      console.info('chosen id', this.id)
      this.step = 1
    },
    close() {
      this.id = null
      this.name = null
      this.step = 0
      this.mode = null
      this.form.resetFields()
      this.visible = false
    },
    show(mode) {
      this.visible = true
      this.mode = mode
    },
    search() {
      getValuators(this.name).then(({ data }) => {
        console.info('valuators', data)
        this.valuators = data
      })
    },
    stepChange(step) {
      if (step == 0) {
        this.step = 0
      }
    },
    dispatch() {
      this.form.validateFieldsAndScroll((err, values) => {
        if (err) {
          return
        }
        const reqData = {
          ...values,
          id: this.orderId,
          hostId: this.id
        }
        console.info('reqData', reqData)
        const dispatch =
          this.mode == 'first' ? dispatchValuator : reDispatchValuator
        dispatch(reqData).then(({ code }) => {
          if (code == 200) {
            this.$message.success('指派成功')
            this.close()
            this.afterChoose()
          }
        })
      })
    }
  },
  mounted() {
    this.search()
  }
}
</script>
<style lang="less" scoped>
.valuator-selector-item-box {
  display: flex;
  .valuator-selector-item {
    margin-left: 10px;
    font-size: 15px;
    font-weight: bold;
    .valuator-selector-item-assign-list {
      padding-left: 5px;
      color: rgb(139, 139, 139);
    }
  }
}
</style>