<template>
  <q-dialog v-model="persistent" persistent content-class="bg-grey-1" transition-show="scale" transition-hide="scale">
    <q-card style="width: 600px;max-width:600px;">
      <q-card-section>
        <div class="text-h6">
          {{ objOption.Id > 0 ? "修改业务员" : "修改业务员" }}
        </div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="row wrap">
          <q-select filled stack-label :dense="false" v-model="objOption.CreateBy" :options="EmployeeList"
            option-value="Id" option-label="EmployeeName" emit-value map-options label="业务员" clearable use-input
            :rules="[(val) => !!val || '请选择业务员']" @filter="filterFn" class="col-12">
            <template v-slot:no-option>
              <q-item>
                <q-item-section class="text-grey">
                  未找到相关数据
                </q-item-section>
              </q-item>
            </template>
          </q-select>
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeVisitCreateForm" />
        <q-btn label="保存" color="accent q-px-md" style="font-weight:400 !important" :loading="saveVisitorCreateLoading"
          @click="saveVisitoryCreateBy" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    setVisitorCreateBy
  } from '../../api/scheduling/schedu'
  import {
    queryEmployee
  } from '../../api/users/user'
  import {
    format
  } from 'quasar';
  export default {
    components: {},
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          Id: 0, //编号
          CreateBy: 0, //创建人
        },
        EmployeeList: [],
        allEmployeeListOption: [], //
        saveVisitorCreateLoading: false
      };
    },
    created() {
      this.getEmployeeList();
    },
    mounted() {
      if (this.saveObj && this.saveObj.Id > 0) {
        this.objOption.Id = this.saveObj.Id;
        this.objOption.CreateBy = this.saveObj.CreateBy;
      }
    },
    methods: {
      //获取员工列表
      getEmployeeList() {
        var qMsg = {
          Dept_Id: 0
        }
        queryEmployee(qMsg).then(res => {
          if (res.Code == 1) {
            this.EmployeeList = res.Data;
            this.allEmployeeListOption = res.Data;
          }
        })
      },
      //筛选员工
      filterFn(val, update) {
        update(() => {
          if (val === "") {
            this.EmployeeList = JSON.parse(
              JSON.stringify(this.EmployeeList)
            );
          } else {
            const needle = val.toLowerCase();
            this.EmployeeList = this.allEmployeeListOption.filter(
              (v) => v.EmployeeName.toLowerCase().indexOf(needle) > -1
            );
          }
        });
      },
      //关闭弹窗
      closeVisitCreateForm() {
        this.$emit("close");
        this.persistent = false;
      },
      //保存业务员
      saveVisitoryCreateBy() {
        this.saveVisitorCreateLoading = true;
        setVisitorCreateBy(this.objOption).then(res => {
          this.saveVisitorCreateLoading = false;
          this.closeVisitCreateForm();
          this.$emit('success');
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
        })
      },
    }
  };

</script>
