<template>
  <ns-drawer
    v-model:visible="visible"
    width="520"
    :title="infoObject?.id ? '编辑' : '新增'"
    :ok="btnClick"
    :cancel="handleClose"
    placement="right">
    <div style="padding: 18px">
      <a-form ref="formRef" :model="infoObject" :rules="rules">
        <a-form-item ref="name" label="站点" name="name">
          <a-tree-select
            v-model:value="infoObject.name"
            show-search
            style="width: 100%"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
            placeholder="请选择站点"
            allow-clear
            tree-default-expand-all
            :tree-data="zdTreeData"
            tree-node-filter-prop="label">
            <!-- 特殊处理情况 -->
            <!-- <template #title="{ value: val, label }">
                  <b v-if="val === 'parent 1-1'" style="color: #08c">sss</b>
                  <template v-else>{{ label }}</template>
                </template> -->
          </a-tree-select>
        </a-form-item>
        <a-form-item label="设备类型" name="sbtype">
          <a-cascader
            v-model:value="infoObject.sbtype"
            :options="sbOptions"
            :show-search="{ filter }"
            placeholder="请选择设备类型" />
        </a-form-item>
        <a-form-item label="设备名称" name="sbName">
          <a-select
            v-model:value="infoObject.sbName"
            style="width: 100%"
            :autoClearSearchValue="true"
            placeholder="请选择设备名称">
            <a-select-option :key="1" :value="1"> AC001电表 </a-select-option>
            <a-select-option :key="2" :value="2"> AC002电表 </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="设备点位" name="sbAdress">
          <a-select
            v-model:value="infoObject.sbAdress"
            show-search
            placeholder="请选择设备点位"
            style="width: 100%"
            :options="dwOptions"
            :filter-option="filterOption"
            @focus="handleFocus"
            @change="handleChange" />
        </a-form-item>
        <a-form-item label="启用规则" name="delivery">
          <a-switch
            v-model:checked="infoObject.delivery"
            :class="{
              'blue-background': infoObject.delivery,
              'grey-background': !infoObject.delivery,
            }" />
        </a-form-item>
        <a-form-item label="取值类型" name="qzType">
          <a-select
            v-model:value="infoObject.qzType"
            show-search
            placeholder="请选择设备点位"
            style="width: 100%"
            :options="qzOptions"
            :filter-option="filterOption"
            @change="handleQzChange" />
        </a-form-item>
        <a-form-item label="规则类型" name="resource">
          <a-radio-group v-model:value="infoObject.resource">
            <a-radio value="1">且(and)</a-radio>
            <a-radio value="2">或(or)</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="异常描述" name="desc">
          <a-textarea
            v-model:value="infoObject.desc"
            style="height: 32px"
            :autoSize="{ minRows: 1, maxRows: 1 }"
            show-count
            :maxlength="30" />
        </a-form-item>
        <template v-for="index in infoObject.alarmList?.length" :key="index">
          <a-form-item :label="`逻辑${index}`" name="alarm">
            <a-input style="width: 60px" v-model:value="infoObject.alarmList[index - 1].alarm" />
          </a-form-item>
          <a-form-item :label="`数值${index}`" name="number">
            <a-input
              style="width: 60px"
              type="number"
              v-model:value="infoObject.alarmList[index - 1].number" />
          </a-form-item>
        </template>
      </a-form>
    </div>
  </ns-drawer>
</template>
<script lang="ts" setup>
  import { NsMessage } from '/nerv-lib/component';
  import { ref, toRaw } from 'vue';
  import type { CascaderProps, TreeSelectProps, SelectProps } from 'ant-design-vue';
  import type { ShowSearchType } from 'ant-design-vue/es/cascader';

  import type { Rule } from 'ant-design-vue/es/form';

  const visible = ref(false);
  //表单数据
  const infoObject = ref({
    id: null,
    name: null,
    resource: null,
    desc: null,
    sbtype: null,
    delivery: null,
    alarmList: [{ alarm: null, number: null }],
  });
  const formRef = ref();
  //站点数
  const zdTreeData = ref<TreeSelectProps['treeData']>([
    {
      label: '铁路总局(T01)',
      value: 'T01',
      children: [
        {
          label: '济阳站(T0101)',
          value: 'T0101',
        },
      ],
    },
  ]);
  //设备类型树
  const sbOptions: CascaderProps['options'] = [
    {
      value: '3',
      label: '3.电梯',
      children: [
        {
          value: '301',
          label: '301.扶梯',
        },
        {
          value: '302',
          label: '301.直梯',
        },
      ],
    },
    {
      value: '4',
      label: '4.冷热源',
      children: [
        {
          value: '401',
          label: '402.冷水机组',
        },
        {
          value: '402',
          label: '403.热泵机组',
        },
        {
          value: '403',
          label: '403.锅炉',
        },
        {
          value: '404',
          label: '404.水处理机组',
        },
        {
          value: '405',
          label: '405.板式热交换机组',
        },
      ],
    },
  ];
  //设备点位
  const dwOptions = ref<SelectProps['options']>([
    { value: '电压 (U)', label: '电压 (U)', code: '1' },
    { value: '电流 (I)', label: '电流 (I)', code: '2' },
    { value: '电流 (I)', label: 'a相电流 (Ia)', code: '3' },
  ]);
  const filter: ShowSearchType['filter'] = (inputValue: any, path: any) => {
    return path.some(
      (option: any) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1,
    );
  };
  //取值类型
  const qzOptions = ref<SelectProps['options']>([
    { value: '实时值', label: '实时值', code: '1' },
    { value: '平均值', label: '平均值', code: '2' },
  ]);
  //设备点位方法
  const filterOption = (input: string, option: any) => {
    console.log('搜索', option.value);
    return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  };
  const handleFocus = () => {
    console.log('点击');
  };
  const handleChange = (value: string) => {
    console.log(`selected ${value}`);
    console.log('选择', infoObject.value.sbAdress);
    //获得code
    dwOptions.value.forEach((item) => {
      if (item.value === value) {
        console.log('符合', item.code);
      }
    });
  };
  const handleQzChange = (value: string) => {
    //获得code
    qzOptions.value.forEach((item) => {
      if (item.value === value) {
        console.log('符合', item.code);
      }
    });
  };
  const emit = defineEmits(['editObject']);
  //父调子 页面显示方法
  const toggle = (value: any) => {
    //判断 是新增 还是修改
    if (value) {
      infoObject.value = value;
    } else {
      infoObject.value = {
        id: null,
        name: null,
        resource: null,
        desc: null,
        sbtype: null,
        delivery: null,
        alarmList: [{ alarm: null, number: null }],
      };
    }

    visible.value = !visible.value;
  };
  //表单 判断规格
  const rules: Record<string, Rule[]> = {
    name: [{ required: true, message: '请选择站点', trigger: 'change' }],
    sbtype: [{ required: true, message: '请选择设备类型', trigger: 'change' }],
    delivery: [{ required: true, message: '请选择启用规则', trigger: 'change' }],
    sbName: [{ required: true, message: '请选择设备名称', trigger: 'change' }],
    sbAdress: [{ required: true, message: '请选择设备点位', trigger: 'change' }],
    qzType: [{ required: true, message: '请选择取值类型', trigger: 'change' }],
    resource: [{ required: true, message: '请选择规则类型', trigger: 'change' }],
    desc: [{ required: true, message: '请输入异常描述', trigger: 'blur' }],
    alarm: [{ required: true, message: '请选择逻辑', trigger: 'blur' }],
    number: [{ required: true, message: '请输入数值', trigger: 'blur' }],
  };
  // 开关选择
  const changeSwitch = () => {
    console.log(infoObject.value.selectSwitch, '开关');
  };
  // 确认按钮
  const btnClick = () => {
    console.log(infoObject.value, '数据');
    console.log(dwOptions.value, '点位');
    dwOptions.value?.forEach;
    //数据是否验证通过
    formRef.value.validate().then(() => {
      console.log('values', infoObject, toRaw(infoObject));
    });
    //调用接口
    // http
    //         .post(props.api, data)
    //         .then(() => {
    //           isLoading.value = false;
    //           NsMessage.success('操作成功', 1, () => {
    //             navigateBack();
    //           });
    //         })
    //         .catch(() => {
    //           isLoading.value = false;
    //         });
  };
  //取消按钮
  const handleClose = () => {
    // 清楚校验错误信息
    formRef.value.resetFields();
    //对象清空
    infoObject.value = {
      id: null,
      name: null,
      resource: null,
      desc: null,
      sbtype: null,
      delivery: null,
      alarmList: [{ alarm: null, number: null }],
    };
    visible.value = false;
    NsMessage.success('操作成功');
  };
  defineExpose({
    toggle,
    handleClose,
    dwOptions,
    formRef,
  });
</script>
<style scoped lang="less">
  .drawerContainer {
    height: 100%;
    display: flex;
    justify-content: space-between;
  }
  .blue-background.ant-switch-checked {
    background-color: linear-gradient(
      180deg,
      rgba(1, 206, 255, 1) 0%,
      rgba(0, 150, 229, 1) 100%
    ) !important;
  }

  .grey-background.ant-switch {
    background-color: grey !important;
  }

  .blue-background.ant-switch-checked .ant-switch-handle {
    background-color: linear-gradient(
      180deg,
      rgba(1, 206, 255, 1) 0%,
      rgba(0, 150, 229, 1) 100%
    ) !important;
  }

  .grey-background.ant-switch .ant-switch-handle {
    background-color: grey !important;
  }
  /deep/ .ant-form-item-label {
    z-index: 20;
    text-align: right;
    width: 20%;
  }
</style>
