<template>
  <ns-drawer
    v-model:visible="visible"
    width="550"
    :title="' '"
    :footer-style="{ textAlign: 'right' }"
    :ok="btnClick"
    :cancel="handleClose"
    placement="right"
    @close="handleClose">
    <a-tabs>
      <a-tab-pane key="1" tab="更新状态">
        <div style="width: 100%; padding: 24px">
          <a-form ref="formRef" :model="infoObject" :rules="rules">
            <a-form-item ref="state" label="当前状态" createName="state">
              <a-select
                v-model:value="infoObject.state"
                show-search
                placeholder="请选择设备点位"
                style="width: 85%"
                :options="stateOptions"
                :disabled="showEdit"
                :filter-option="filterDevicePoint" />
              <ns-icon
                size="20"
                @click="changeShowEdit"
                style="margin-left: 20px"
                :createName="showEdit ? 'bianji' : 'baocun'" />
            </a-form-item>
            <a-form-item label="备注" createName="desc">
              <a-textarea
                v-model:value="infoObject.desc"
                placeholder="请输入异常描述"
                :disabled="showEdit"
                style="width: 85%"
                :autoSize="{ minRows: 4, maxRows: 4 }" />
            </a-form-item>
          </a-form>
        </div>
      </a-tab-pane>
      <a-tab-pane key="2" tab="状态流程">
        <!-- 自动生成工单 -->
        <div v-if="infoObject.createWorkOrder === 1"> xxxx </div>
        <!-- 没有自动生成工单 -->
        <NsSteps v-else v-bind="config" />
      </a-tab-pane>
    </a-tabs>
    <template #footer>
      <a-button type="primary" @click="btnClick">确定</a-button>
    </template>
  </ns-drawer>
</template>
<script>
  import { defineComponent } from 'vue';
  import { ref } from 'vue';
  import NsSteps from '/@/components/ns-steps.vue';
  import { NsMessage } from '/nerv-lib/component';
  import { http } from '/nerv-lib/util';
  import { equipmentAlarmApi } from '/@/api/alarmManagement/equipmentAlarm';

  export default defineComponent({
    components: { NsSteps },

    setup() {
      const visible = ref(false);
      const showEdit = ref(true);
      const infoObject = ref({});
      const equipmentAlarm = ref({});
      const stateOptions = ref();
      const logList = ref([
        { createName: '李四', state: 3, createTime: '2024-03-10 10:00:00', desc: '完成' },
        { createName: '王五', state: 5, createTime: '2024-03-10 10:00:00' },
        { createName: '王五', state: 4, createTime: '2024-03-10 10:00:00' },
        { createName: '王五', state: 2, createTime: '2024-03-10 10:00:00', desc: '创建工单' },
        { createName: '赵六', state: 1, createTime: '2024-03-10 10:00:00' },
      ]);
      const config = ref({
        size: logList.value.length,
        dataSource: logList.value,
      });
      const handleClose = () => {
        equipmentAlarm.value = P;
        visible.value = false;
      };
      const btnClick = () => {
        NsMessage.success('操作成功');
        delete infoObject.value.createTime;
        console.log(infoObject.value, equipmentAlarm.value, '操作');
        console.log('btnClick');
      };
      //修改状态
      const changeShowEdit = () => {
        if (equipmentAlarm.value.createWorkOrder === 1) {
          showEdit.value = !showEdit.value;
        }
      };
      const toggle = async (data) => {
        equipmentAlarm.value = { ...data };
        infoObject.value = { ...logList.value[0] };
        await http
          .post(equipmentAlarmApi.getSelectAlarmEquipmentLogStatusProcess, {
            id: data.id,
          })
          .then((res) => {
            console.log(res);
          });
        if (equipmentAlarm.value.createWorkOrder === 1) {
          stateOptions.value = [
            { value: 1, label: '待处理' },
            { value: 2, label: '处理中' },
            { value: 3, label: '已完成' },
            { value: 4, label: '超时' },
            { value: 5, label: '已关闭' },
          ];
        } else {
          stateOptions.value = [
            { value: 1, label: '待处理' },
            { value: 2, label: '处理中' },
            { value: 3, label: '已完成' },
            { value: 5, label: '已关闭' },
          ];
        }
        let statusMap = {
          1: '待处理',
          2: '处理中',
          3: '已完成',
          4: '超时',
          5: '已关闭',
        };
        let colorMap = {
          1: '#ff7602',
          2: '#00a1e6',
          3: '#04d919',
          4: '#d9001b',
          5: '#a6a6a6',
        };
        logList.value.forEach((item) => {
          item.stateName = statusMap[item.state];
          item.color = colorMap[item.state];
          item.src = 'state-' + item.state;
        });
        visible.value = true;
      };
      return {
        infoObject,
        changeShowEdit,
        showEdit,
        equipmentAlarm,
        stateOptions,
        btnClick,
        visible,
        logList,
        config,
        handleClose,
        toggle,
      };
    },
  });
</script>
<style scoped lang="less">
  :deep(.ant-form-item-label) {
    z-index: 20;
    text-align: right;
    width: 17%;
  }
</style>
