<!--
 * @Descripttion: (客服/tb_customer_service 添加弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-10-21)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-10-21)
-->
<template>
  <el-dialog v-model="props.modelValue" title="添加客服信息" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">


        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="名称" prop="customerServiceName">
            <el-input v-model="formData.customerServiceName" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="电话" prop="customerServicePhone">
            <el-input v-model="formData.customerServicePhone" placeholder="请输入电话" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="图片" prop="customerServiceImg">
            <UploadImage ref="uploadRef" v-model="formData.customerServiceImg" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
          </el-form-item>
        </el-col>

        <el-col :lg="24">

          <!-- workingHoursBeginTime workingHoursEndTime -->
          <el-form-item :label-width="labelWidth" label="工作时间" prop="">
            <el-time-picker 
              v-model="dateRange" 
              style="width: 240px" 
              is-range
              format = 'HH:mm' 
              value-format = 'HH:mm'
              range-separator="-"
              start-placeholder="上班时间" 
              end-placeholder="下班时间">
            </el-time-picker>
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="customerServiceSort">
            <el-input-number v-model.number="formData.customerServiceSort" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>

    <template #footer>
      <div key="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)" :loading="loadingStatus">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>


<script setup>
import { reactive, ref, watch } from "vue";
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { addOrUpdateCustomerService } from '@/api/business/CustomerServices/customerService.js';


// 打开弹窗时回调
const openDialog = async () => {


}

// -业务参数
const dateRange = ref([])


// -业务方法



// -基础参数
const loadingStatus = ref(false)
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const formData = reactive({
  customerServiceSort: 100
});
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const imgData = ref({
  fileDir: "CustomerService"
})

// 验证
const rules = reactive({
  customerServiceGuid: [{ required: true, message: "不能为空", trigger: "blur", type: "number" }],
  customerServiceName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  customerServicePhone: [{ required: true, message: "电话不能为空", trigger: "blur" }],
  customerServiceImg: [{ required: true, message: "图片不能为空", trigger: "blur" }],
  workingHoursBeginTime: [{ required: true, message: "工作时间不能为空", trigger: "blur" }],
  workingHoursEndTime: [{ required: true, message: "工作时间-下班时间不能为空", trigger: "blur" }],
  customerServiceSort: [{ required: true, message: "排序不能为空", trigger: "blur", type: "number" }],
});

// -基础方法

// 提交
const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    loadingStatus.value = true

    if(dateRange.value){
      formData.workingHoursBeginTime = dateRange.value[0]
      formData.workingHoursEndTime = dateRange.value[1]
    }
    else{
      modal.msgError("请选择客户工作时间")
      return
    }
    const { code } = await addOrUpdateCustomerService(formData);
    if (code == 200) {
      modal.msgSuccess('添加成功')
      closeDialog();
      loadingStatus.value = false
    }
  });
};
const closeDialog = () => {
  handleResetClick(formRef.value);
  props.done();
  emits("update:modelValue", false);
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>