<template>
  <div class="AddBuildingBox">
    <PageHeader></PageHeader>
    <div class="AddBuildingBody">
      <div class="title">基本信息</div>
      <div class="addBuildingFormBody">
        <el-form class="formBody" label-position="right" ref="formRef" :model="form" :rules="rules" label-width="130px">
          <el-form-item label="账单名称" prop="billName">
            <el-input disabled v-model.trim="form.billName" placeholder="请输入账单名称" />
          </el-form-item>

          <el-form-item label="收费项目" prop="chargeItemId">
            <el-select disabled v-model="form.chargeItemId" @change="handleChange">
              <el-option v-for="(item, index) in chargeitemlist" :key="index" :value="item.id" :label="item.itemName"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="收费范围" prop="chargeScope" v-if="itemTypeincludes">
            <el-radio-group disabled v-model="form.chargeScope">
              <el-radio v-for="(item, index) in com_questionnaire_scope" :key="index" :value="item.value" :label="item.label"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="收费范围" prop="chargeScope" v-else>
            <el-radio-group disabled v-model="form.chargeScope">
              <el-radio v-for="(item, index) in com_bill_charge_scope_house" :key="index" :value="item.value" :label="item.label"></el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="费用周期" prop="chargePeriod">
            <el-select disabled v-model="form.chargePeriod">
              <el-option v-for="(item, index) in com_bill_charge_period" :key="index" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="起止日期" prop="price">
            <el-date-picker
              disabled
              v-model="StartEndDate"
              @update:model-value="handleDate"
              :value-format="datepicker[form.chargePeriod].format"
              :format="datepicker[form.chargePeriod].format"
              :type="datepicker[form.chargePeriod].type"
              range-separator="到"
              start-placeholder="开始"
              end-placeholder="结束"
            />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input disabled type="textarea" :rows="5" v-model.trim="form.remark" placeholder="请输入" />
          </el-form-item>

          <el-form-item style="margin-top: 30px">
            <el-button @click="cancelForm">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PageHeader from '@/components/Pageheader/index.vue';
import { listChargeItem } from '@/api/system/SdbChargeItem';
import { getBill, listBillHouse } from '@/api/system/SdbBill';
import type { DatePickerType } from 'element-plus';
import { formatDate, formatDate2 } from '@/utils';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_questionnaire_scope } = toRefs<any>(proxy?.useDict('com_questionnaire_scope'));
const { com_bill_charge_scope_house } = toRefs<any>(proxy?.useDict('com_bill_charge_scope_house'));
const { com_bill_charge_period } = toRefs<any>(proxy?.useDict('com_bill_charge_period'));

type PickerConfig = {
  type: DatePickerType;
  format: string;
};
const datepicker: Record<string, PickerConfig> = {
  '0': { type: 'yearrange', format: 'YYYY' },
  '1': { type: 'monthrange', format: 'YYYY-MM' },
  '2': { type: 'daterange', format: 'YYYY-MM-DD' }
};

const router = useRouter();
const route = useRoute();
const formRef = ref();
const form = ref({
  'id': null,
  'billName': '',
  'chargeItemId': '', // 收费项目
  'chargeScope': '0', // 业主范围
  'chargePeriod': '2', // 费用周期
  houseIds: [], // 部分业主
  charegeTypeId: '', // typeid
  'startDate': '', // 开始
  'endDate': '', // 结束
  'remark': ''
});
const rules = ref({
  billName: [{ required: true, message: '请输入 项目名称', trigger: 'blur' }],
  chargeItemId: [{ required: true, message: '请选择 收费项目', trigger: 'blur' }],
  chargePeriod: [{ required: true, message: '请选择 费用周期', trigger: 'blur' }],
  startDate: [{ required: true, message: '请选择 起止日期', trigger: 'blur' }],
  endDate: [{ required: true, message: '请选择 起止日期', trigger: 'blur' }]
});
const userid = ref(null);

// 开始结束日期
const StartEndDate = ref([]);
function handleDate(val: string[]) {
  form.value.startDate = formatDate2(val[0]);
  form.value.endDate = formatDate2(val[1]);
}
const chargeitemlist = ref([]);
// 电费 水费
const typeNamelist = ref(['2049026484272791553', '2049026503017136129']);

const itemTypeincludes = computed(() => {
  return typeNamelist.value.includes(form.value.chargeItemId);
});

const houselist = ref([]);

const checkoutUses = ref([]);
async function init() {
  const res1 = await listBillHouse({ pageNum: 1, pageSize: 999999 });
  houselist.value = res1.rows;

  listChargeItem().then(async (res) => {
    chargeitemlist.value = res.rows;

    if (route.query.id) {
      userid.value = route.query.id;
      getBill(userid.value).then((res1) => {
        form.value = {
          ...form.value,
          ...res1.data
        };
        form.value.houseIds = res1.data.houseIds || [];
        StartEndDate.value = [formatDate(res1.data.startDate), formatDate(res1.data.endDate)];
        checkoutUses.value = form.value.houseIds
          .map((item) => {
            if (item) {
              const find = houselist.value.find((it) => it.houseId === item);
              if (find) {
                return find;
              } else {
                return null;
              }
            }
          })
          .filter((item) => item !== null);
      });
    }
  });
}

init();

// 获取收费类型id
function handleChange(val) {
  const find = chargeitemlist.value.find((item) => item.id === val);
  if (find) {
    form.value.charegeTypeId = find.typeId;
  }
}

// !== 提交 =============================================================================

// 推出
const cancelForm = () => {
  router.back();
};
</script>

<style scoped lang="scss">
.AddBuildingBox {
  padding: 15px;
  height: 100%;
  width: 100%;
}
.AddBuildingBody {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: white;
  padding: 15px;
  .title {
    height: 40px;
    line-height: 40px;
    padding-left: 20px;
    background-color: rgba(255, 255, 255, 1);
    color: rgba(16, 16, 16, 1);
    border-bottom: 1px solid #bbbbbb;
    margin-bottom: 60px;
  }

  .formBody {
    width: 650px;
    margin: 0 auto;

    .residentBox {
      max-height: 400px;
      overflow-y: auto;
      padding: 10px 5px;
    }
    .checkoutSideUsesbox {
      // display: grid;
      // grid-template-columns: repeat(3, 1fr);
      // gap: 5px;
      display: flex;
      flex-wrap: wrap;
      align-content: flex-start;
      justify-content: space-between;
      padding: 10px 5px;
      li {
        margin-right: 10px;
        margin-bottom: 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        min-width: 240px;
        height: 35px;
        padding: 0 10px;
        line-height: 35px;

        background-color: #f3f9ff;
        border: 1px solid #8fc0f1;

        margin-bottom: 10px;

        @media (max-width: 2000px) {
          font-size: 12px;
        }
        @media (max-width: 1650px) {
          font-size: 10px;
        }
        @media (max-width: 1650px) {
          font-size: 8px;
        }
        .el-icon {
          margin-left: 20px;
          cursor: pointer;
          &:hover {
            color: red;
          }
        }
        &.add {
          cursor: pointer;
          justify-content: center;
          color: #409eff;
          border: 1px dashed #409eff;
          background-color: transparent;
          &:hover {
            background-color: #40a0ff10;
          }
          span {
            margin-left: 10px;
          }
        }
      }
    }
    &:deep(.el-form-item__content, .el-select, .el-input) {
      width: 350px !important;
      margin-right: 10px;
      .el-cascader {
        width: 100%;
      }
    }
    .el-form-item {
      margin-bottom: 20px;
      align-items: center;
    }
    .el-button {
      width: 80px;
      height: 35px;
      margin-right: 20px;
    }
  }
}
</style>
