<template>
  <div class="m-4">
    <div>
      <XTTitle title="机器消毒" />
      <PageWrapper>
        <Card
          :bordered="false"
          :active-tab-key="activeKey"
          :tab-list="tabList"
          style="height: 800px"
          @tabChange="handleTabChange"
        >
          <div :key="0" v-if="activeKey == 0">
            <Row>
              <Col :span="24">
                <BasicTable @register="registerTable">
                  <template #bodyCell="{ column, record }">
                    <template v-if="column.key === 'inpatientWard'">
                      {{ record.inpatientWard + record.bed }}
                    </template>
                    <template v-if="column.key === 'disinfectionType'">
                      <span
                        :color="formatDictColor(disinfectionTypeOptions, record.disinfectionType)"
                        :style="
                          'color:' +
                          formatDictFontColor(disinfectionTypeOptions, record.disinfectionType)
                        "
                      >
                        {{ formatDictValue(disinfectionTypeOptions, record.disinfectionType) }}
                      </span>
                    </template>
                    <template v-if="column.key === 'disinfectant'">
                      <span>
                        {{ getDisinfectant(record) }}
                      </span>
                    </template>
                    <template v-if="column.key === 'otherMaintenanceDisinfectant'">
                      <span>
                        {{ record.otherMaintenanceDisinfectant ? '已更换' : '未更换' }}
                      </span>
                    </template>
                    <template v-if="column.key === 'otherMaintenanceBacterialFilter'">
                      <span>
                        {{ record.otherMaintenanceBacterialFilter ? '已更换' : '未更换' }}
                      </span>
                    </template>
                    <template v-if="column.key === 'updatorName'">
                      <img :src="record.updateAvatar" class="table-avatar" />
                      {{ record.updatorName }}
                    </template>
                    <template v-if="column.key === 'action'">
                      <TableAction
                        :actions="[
                          // {
                          //   auth: 'bizSys:wardInfo:edit',
                          //   icon: 'icon-xt-details_edit_default|iconfont',
                          //   tooltip: '编辑',
                          //   onClick: handleEditDisinfectRecord.bind(null, record),
                          // },
                          {
                            auth: 'bizSys:wardInfo:remove',
                            icon: 'icon-xt-details_delete_default|iconfont',
                            tooltip: '删除',
                            popConfirm: {
                              title: '是否确认删除',
                              placement: 'left',
                              confirm: handleDelDisinfectRecord.bind(null, record),
                            },
                          },
                        ]"
                      />
                    </template>
                  </template>
                </BasicTable>
              </Col>
            </Row>
          </div>
          <div :key="1" v-else>
            <div class="card-list">
              <DeviceDisinfectCard
                class="cards"
                v-for="(item, index) in disinfectMaintenanceList"
                :data="item"
                :editAuth="['biz:deviceDisinfectMaintenance:edit']"
                :key="index"
                @edits="handleEditDisinfectMaintenance"
              />
            </div>
          </div>
        </Card>
      </PageWrapper>
    </div>
  </div>
  <DisinfectMaintenanceFormModal
    @register="registerModal"
    @success="handleEditDisinfectMaintenanceSuccess"
  />
</template>

<script lang="ts" setup>
  import { onBeforeMount, ref } from 'vue';
  import { XTTitle } from '/@/components/XTTitle/index';
  import { PageWrapper } from '/@/components/Page';
  import { DeviceDisinfectCard } from '@/components/XTCard/index';
  import { Card, Row, Col } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import DisinfectMaintenanceFormModal from './disinfectMaintenanceFormModal.vue';
  import { BasicTable, useTable, TableAction } from '/@/components/TableCard';
  import { disinfectRecordColumns } from './data';
  import {
    getDisinfectRecordPage,
    disinfectRecordRemove,
    disinfectMaintenance,
  } from '/@/api/biz/visit/deviceDisinfectApi';
  import { listDictModel, listDictModelBatch } from '/@/api/common';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { formatDictColor, formatDictFontColor, formatDictValue } from '/@/utils';
  const { createMessage } = useMessage(); // createConfirm,
  const tabList = ref([
    { key: 0, tab: '消毒记录', type: 'WARD' },
    { key: 1, tab: '机器消毒维护', type: 'WORK' },
  ]);
  const activeKey = ref(0);
  const disinfectionTypeOptions = ref();
  const disinfectantOptions = ref();
  const disinfectMaintenanceList = ref();

  const [registerModal, { openModal: openDisinfectMaintenance }] = useModal();
  onBeforeMount(async () => {
    disinfectionTypeOptions.value = await listDictModel({ dictCode: 'tv_ddt' });
    const bizDictData = [] as any;
    disinfectionTypeOptions.value.forEach(item => {
      bizDictData.push({
        key: item.value,
        dictCode: item.value,
      });
    });
    disinfectantOptions.value = await listDictModelBatch(bizDictData.map(ele => ele.dictCode));
    await getDeviceDiisinfect();
  });

  const [registerTable, { reload }] = useTable({
    api: getDisinfectRecordPage,
    rowKey: 'id',
    columns: disinfectRecordColumns,
    showIndexColumn: false,
    striped: false,
    formConfig: {
      labelWidth: 120,
      autoSubmitOnEnter: true,
      baseColProps: { xs: 24, sm: 12, md: 12, lg: 8 },
      resetButtonOptions: {
        preIcon: 'icon-delete|iconfont',
      },
      submitButtonOptions: {
        preIcon: 'icon-search|iconfont',
      },
    },
    useSearchForm: false,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
    beforeFetch: handleBeforeFetch,
  });

  const tableSort = ref([
    {
      field: 'sort',
      direction: 'ASC',
    },
  ]);
  const selectType = ref();

  // 方法区
  // 获取机器消毒维护列表
  async function getDeviceDiisinfect() {
    disinfectMaintenanceList.value = await disinfectMaintenance();
    console.log('disinfectMaintenanceList:::', disinfectMaintenanceList.value);
  }
  // 获取病区信息前事件
  function handleBeforeFetch(params) {
    return { ...params, propertiesId: selectType.value, orders: tableSort.value };
  }
  // 停用启用病区方法
  async function handleDelDisinfectRecord(record) {
    await disinfectRecordRemove([record.id]);
    createMessage.success('删除成功');
    reload();
  }
  function getDisinfectant(record) {
    for (const key in disinfectantOptions.value) {
      if (key == record.disinfectionType) {
        return formatDictValue(disinfectantOptions.value[key], record.disinfectant);
      }
    }
  }
  // 编辑机器消毒维护
  function handleEditDisinfectMaintenance(record) {
    openDisinfectMaintenance(true, {
      record,
    });
  }
  // 编辑机器消费维护成功回调方法
  async function handleEditDisinfectMaintenanceSuccess() {
    await getDeviceDiisinfect();
  }

  // 切换Tab
  function handleTabChange(key) {
    activeKey.value = key;
    if (key) {
      getDeviceDiisinfect();
    }
  }

  // 编辑消毒记录
  // function handleEditDisinfectRecord() {}
</script>

<style lang="less" scoped>
  ::v-deep(.ant-card-head) {
    background-color: #f6f8fa !important;
    color: #000a18;
    font-weight: 600;
    font-size: 18px;
  }

  ::v-deep(.ant-card) {
    background-color: #f6f8fa !important;
  }

  ::v-deep(.ant-btn-link) {
    color: rgb(61 65 85 / 100%);
  }

  .card-list {
    width: 100%;

    .cards {
      margin-left: 20px;
      margin-top: 20px;
      background-color: #fff !important;
      display: inline-block;
    }
  }
</style>
