<script setup lang="ts">
import { ElForm, ElMessage } from "element-plus";
import { shallowRef, ref, onMounted, nextTick, watch } from "vue";
import BusinessQuery from "/@/components/BasicForm/src/fields/business-permission/index.vue";
import DepartQuery from "/@/components/BasicForm/src/fields/depart.vue";
import apis from "./config/apis";
import dayjs from "dayjs";
import { isBeyondTime } from "../../_utils";
import { useUserStore } from "/@/store/modules/user";


defineProps<{ isBusiness?: Boolean }>();

const emit = defineEmits(["refresh"]);

const visible = shallowRef(false);
const loading = shallowRef(false);

const execList = ref<Array<Record<string, string>>>([]);

const createInitialData = () => ({
  exec_id: "28",
  date: [],
  supplierNo: "QS2203150147013805",
  depart_id: "0",
  plat_type: "",
  exec_type: "1",
  No_companyNo: ""
});

const rules = {
  date: [{ required: true, message: "请选择数据时间", trigger: "change" }],
  exec_id: [{ required: true, message: "请输入业务表", trigger: "change" }],
  depart_id: [{ required: true, message: "请选择所在部门", trigger: "change" }],
  plat_type: [{ required: true, message: "请选择平台类型", trigger: "change" }],
  companyNo: [{ required: true, message: "请选择业务公司", trigger: "change" }],
  exec_type: [{ required: true, message: "请选择执行类型", trigger: "change" }]
};

const formRef = ref<InstanceType<typeof ElForm> | null>(null);
const formData = ref({ ...createInitialData() });



async function onSubmit() {
  try {
    await formRef.value?.validate();
    const { date, ...rest } = formData.value;

    const daysInMonth = dayjs(new Date(date)).daysInMonth();
    const start = dayjs(new Date(date)).format('YYYY-MM') + '-01 00:00:00'
    const end = dayjs(new Date(date)).format('YYYY-MM') + `-${daysInMonth} 23:59:59`

    loading.value = true;
    const response = await apis.create_exec({ addField:[], ...rest, start, end });
    loading.value = false;

    if (response.code === 1) {
      visible.value = false;
      emit("refresh");
    } else {
      ElMessage.warning(response.message);
    }
  } catch (error) {
    console.log(error);
  }
}

function handleClose() {
  formData.value = createInitialData();
  visible.value = false;
}

onMounted(async () => {
  const response = await apis.exce_list({ size:100 });
  if (response.code !== 1) return;
  execList.value = response.data.list;
});


const disabledDate = (time: Date) => { 
  if (dayjs(time).year() < dayjs(Date.now()).year()) { 
    return false
  }

  if (dayjs(time).year() === dayjs(Date.now()).year()) { 
    return dayjs(time).month() >= dayjs(Date.now()).month()
  }

  return true
}


defineExpose({
  onDisplay: companyNo => {
    visible.value = true;
    // nextTick(() => 'GS2302231125079621' && businessQueryRef.value.initialData('GS2302231125079621'));
  }
});
</script>

<template>
  <el-dialog v-model="visible" title="修改报表需求" center @close="handleClose">
    <el-form label-width="90px" :rules="rules" :model="formData" ref="formRef">
      <el-form-item label="业务表:" prop="exec_id">
        <el-select
          v-model="formData.exec_id"
          style="width: 100%"
          placeholder="业务表"
          disabled
        >
          <el-option
            v-for="exec in execList"
            :key="exec.id"
            :label="exec.exec_name"
            :value="exec.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="数据时间:" prop="date">
        <el-date-picker v-model="formData.date"  type="month" style="width:100%" placeholder="数据时间"  :disabled-date="disabledDate" />
      </el-form-item>

      <el-form-item label="执行类型" prop="exec_type">
        <el-select
          v-model="formData.exec_type"
          placeholder="执行类型"
          style="width: 100%"
        >
          <el-option value="1" label="立即执行" />
          <el-option value="2" label="延迟执行" />
        </el-select>
      </el-form-item>

      <el-form-item label="供应商" prop="supplierNo" v-if="isBusiness">
        <el-input modelValue="北京百辰荣达国际科贸有限公司" disabled />
      </el-form-item>

      <el-form-item>
        <div class="w-full flex justify-between">
          <el-alert
            type="warning"
            style="width: 260px; margin-right: 10px; padding: 0px"
            size="small"
            :closable="false"
            v-if="String(formData.exec_type) === '1'"
          >
            立即执行:文件会在申请后几分钟内生成;
          </el-alert>

          <el-alert
            type="warning"
            style="width: 260px; margin-right: 10px; padding: 0px"
            size="small"
            :closable="false"
            v-if="String(formData.exec_type) === '2'"
          >
            延迟执行:文件会在申请当天20:00开始执行;
          </el-alert>

          <el-button class="float-right" type="primary" @click="onSubmit"
            >保存</el-button
          >
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
