<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="'90%'"
    :visible="visible"
    :confirm-loading="loading"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑结算' : '添加结算'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 6 }, sm: { span: 20 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 24 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-card title="填报人信息" :bordered="false">
        <a-row :gutter="16">
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label="填报人">
              <a-input disabled v-model:value="loginUser.username" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label="填报时间">
              <a-input disabled v-model:value="now" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-card>
      <a-card title="设备信息" :bordered="false">
        <a-row :gutter="16">
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="项目名称" v-bind="validateInfos.projectId">
              <ProjectSelectModel
                :placeholder="`请选择项目`"
                v-model:value="form.projectName"
                @done="chooseProject"
              />
            </a-form-item>
            <a-form-item label="自编号">
              <p style="margin: 0">{{ form.equipmentNo }}</p>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="合同编号" v-bind="validateInfos.contractNo">
              <a-input v-model:value="form.contractNo" />
            </a-form-item>
            <a-form-item label="出场编号">
              <p style="margin: 0">{{ form.factoryNo }}</p>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="设备名称">
              <TowerEquipmentModel
                :placeholder="`请选择设备名称`"
                v-model:value="form.equipmentName"
                @done="chooseEquipmentName"
              />
            </a-form-item>
            <a-form-item label="备案编号">
              <p style="margin: 0">{{ form.filingNo }}</p>
            </a-form-item>
          </a-col>
        </a-row>
      </a-card>
      <a-card title="结算信息" :bordered="false">
        <a-row :gutter="16">
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="设备结算租金含税金额" v-bind="validateInfos.amountWithTax">
              <a-input v-model:value="form.amountWithTax" placeholder="设备结算租金含税金额">
                <template #suffix>元</template>
              </a-input>
            </a-form-item>
            <a-form-item label="进出场费税率" v-bind="validateInfos.inOutTax">
              <a-input v-model:value="form.inOutTax" placeholder="进出场费税率">
                <template #suffix>%</template>
              </a-input>
            </a-form-item>
            <a-form-item label="其他费用含税金额" v-bind="validateInfos.otherAmountWithTax">
              <a-input v-model:value="form.otherAmountWithTax" placeholder="其他费用含税金额">
                <template #suffix>元</template>
              </a-input>
            </a-form-item>
            <a-form-item label="结束日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择结束日期"
                v-model:value="form.endDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="设备结算租金税率" v-bind="validateInfos.taxAmount">
              <a-input v-model:value="form.taxAmount" placeholder="设备结算租金税率">
                <template #suffix>%</template>
              </a-input>
            </a-form-item>
            <a-form-item label="劳务费含税金额" v-bind="validateInfos.workerAmountWithTax">
              <a-input v-model:value="form.workerAmountWithTax" placeholder="劳务费含税金额">
                <template #suffix>元</template>
              </a-input>
            </a-form-item>
            <a-form-item label="其他费用税率" v-bind="validateInfos.otherTax">
              <a-input v-model:value="form.otherTax" placeholder="其他费用税率">
                <template #suffix>元</template>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="进出场费含税金额" v-bind="validateInfos.inOutWithTax">
              <a-input v-model:value="form.inOutWithTax" placeholder="进出场费含税金额">
                <template #suffix>元</template>
              </a-input>
            </a-form-item>
            <a-form-item label="劳务费税率" v-bind="validateInfos.workerTax">
              <a-input v-model:value="form.workerTax" placeholder="劳务费税率">
                <template #suffix>%</template>
              </a-input>
            </a-form-item>
            <a-form-item label="开始日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择开始日期"
                v-model:value="form.startDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-card>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
import { ref, reactive, watch, computed } from "vue";
import { Form, message } from "ant-design-vue";
import { assignObject, EleProTable } from "ele-admin-pro";
import { useUserStore } from "@/store/modules/user";
import dayjs from "dayjs";

import { HistorySettle } from "@/api/tower/historySettle/model";
import { addHistorySettle, updateHistorySettle } from "@/api/tower/historySettle";

const userStore = useUserStore();
// 当前用户信息
const loginUser = computed(() => userStore.info ?? {});

const now = dayjs().format("YYYY-MM-DD HH:mm:ss");

// 是否是修改
const isUpdate = ref(false);
const useForm = Form.useForm;
const props = defineProps<{
  // 弹窗是否打开
  visible: boolean;
  // 修改回显的数据
  data?: HistorySettle | null;
}>();

const emit = defineEmits<{
  (e: "done"): void;
  (e: "update:visible", visible: boolean): void;
}>();

// 提交状态
const loading = ref(false);
// 是否显示最大化切换按钮
const maxable = ref(true);
const activeKey = ref(0);

const chooseProject = (res) => {
  form.projectName = res.projectName;
  form.projectId = res.projectId;
};

const chooseEquipmentName = (res) => {
  form.equipmentId = res.equipmentId;
  form.equipmentNo = res.equipmentNo;
  form.equipmentName = res.name;
  form.filingNo = res.filingNo;
  form.equipmentModel = res.model;
  form.factoryNo = res.factoryNo;
};

// 用户信息
const form = reactive<HistorySettle>({
  historySettleId: "",
  projectId: "",
  projectName: "",
  equipmentName: "",
  equipmentModel: "",
  factoryNo: "",
  filingNo: "",
  equipmentNo: "",
  contractNo: "",
  equipmentId: "",
  amountWithTax: "",
  taxAmount: "",
  inOutWithTax: "",
  inOutTax: "",
  workerAmountWithTax: "",
  workerTax: "",
  otherAmountWithTax: "",
  otherTax: "",
  startDate: "",
  endDate: "",
  userId: loginUser.value.userId
});

/* 更新visible */
const updateVisible = (value: boolean) => {
  emit("update:visible", value);
};

// 表单验证规则
const rules = reactive({
  projectId: [
    {
      required: true,
      message: "请选择项目",
      trigger: "blur"
    }
  ],
  contractNo: [
    {
      required: true,
      message: "请输入合同编号",
      trigger: "blur"
    }
  ],
  amountWithTax: [
    {
      required: true,
      message: "请输入设备结算租金含税金额",
      trigger: "blur"
    }
  ],
  inOutTax: [
    {
      required: true,
      message: "请输入进出场费税率",
      trigger: "blur"
    }
  ],
  otherAmountWithTax: [
    {
      required: true,
      message: "请输入其他费用含税金额",
      trigger: "blur"
    }
  ],
  taxAmount: [
    {
      required: true,
      message: "请输入设备结算租金税率",
      trigger: "blur"
    }
  ],
  workerAmountWithTax: [
    {
      required: true,
      message: "请输入劳务费含税金额",
      trigger: "blur"
    }
  ],
  otherTax: [
    {
      required: true,
      message: "请输入其他费用税率",
      trigger: "blur"
    }
  ],
  inOutWithTax: [
    {
      required: true,
      message: "请输入进出场费含税金额",
      trigger: "blur"
    }
  ],
  workerTax: [
    {
      required: true,
      message: "请输入劳务费税率",
      trigger: "blur"
    }
  ],

});
const { resetFields, validate, validateInfos } = useForm(form, rules);

/* 保存编辑 */
const save = () => {
  validate()
    .then(async () => {
      loading.value = true;
      const data = {
        ...form
      };
      const saveOrUpdate = isUpdate.value ? updateHistorySettle : addHistorySettle;
      const res = await saveOrUpdate(data).catch((e) => {
        loading.value = false;
        message.error(e.message);
      });
      message.success(res?.message);
      updateVisible(false);
      emit("done");
    })
    .catch(() => {
    });
};

watch(
  () => props.visible,
  (visible) => {
    if (visible) {
      if (props.data) {
        loading.value = false;
        assignObject(form, props.data);
        isUpdate.value = true;
      } else {
        isUpdate.value = false;
      }
    } else {
      resetFields();
    }
  }
);
</script>
<style lang="less">
.title {
  font-weight: bold;
  font-size: 1.1rem;
  padding-bottom: 10px;
  border-bottom: 1px solid #dcdfe6;
  color: #494949;
  margin-bottom: 10px;
}
</style>
