<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="title"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
    :confirmLoading="loading"
    :width="420"
    :min-height="0"
  >
    <div class="pt-2px pr-3px">
      <div class="mx-5 BasicForm flex">
        <div class="input">
          <a-input
            style="width: 350px"
            maxlength="100"
            showCount
            allowClear
            v-model:value="fileFlow.title"
            placeholder="请输入"
          />
        </div>
        <!-- <a-button type="primary" @click="handleAdd"> 新增</a-button> -->
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted, reactive, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { sceneMove } from '/@/api/operate';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { getByKey, addOrUpdate, del } from '/@/api/media';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    TableImg,
    FormProps,
  } from '/@/components/Table';
  import { caseaddOrUpdateApi, operateSceneList } from '/@/api/operate';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicTable, TableAction },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const loading = ref(false);
      const title = ref('重命名');
      const fileFlow = reactive({
        title: '',
        caseTitle: '',
        file: null,
        dylist: [],
        mslist: [],
      });
      const { createMessage, createConfirm } = useMessage();
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        fileFlow.caseTitle = '';
        title.value = data.caseTitle?'重命名':'名称';
        modelRef.value = data;
        fileFlow.dylist = [];
        fileFlow.mslist = [];
        fileFlow.title = data.caseTitle;
        fileFlow.caseTitle = '';
      }
      function handleAdd() {
        console.log('handleAdd', fileFlow.dictName);
        let params = {
          dictName: fileFlow.dictName.trim(),
        };
        if (!fileFlow.dictName.trim()) {
          return createMessage.warning('请输入名称');
        }
        addOrUpdate(params).then((res) => {
          createMessage.success('添加成功');
          fileFlow.dictName = '';
        });
      }
      const handleSubmit = async () => {
        if (!fileFlow.caseTitle?.trim()) {
          return createMessage.warning('请输入案件名称');
        }
        try {
          loading.value = true;
          const apiData = {
            caseTitle: fileFlow.caseTitle?.trim(),
            sceneNumParam: [
              {
                type: 0,
                numList: fileFlow.dylist,
              },
              {
                type: 1,
                numList: fileFlow.mslist,
              },
            ],
          };
          console.log('res', apiData);
          const res = await caseaddOrUpdateApi(apiData);
          console.log('res', res);
          closeModal();
          createMessage.success('新增案件成功。');
          emit('update');
          loading.value = false;
        } catch (error) {
          loading.value = false;
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        // console.log(v);
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      async function handleDelete(record: Recordable) {
        console.log('handleDelete', record);
        createConfirm({
          title: '删除',
          content: '确定要删除吗？',
          onOk: async () => {
            del({ id: record.id }).then(() => {
              createMessage.success({
                content: '删除成功',
              });
              reload();
            });
          },
        });
      }
      return {
        register,
        handleAdd,
        model: modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        handleDelete,
        t,
        title,
        loading,
      };
    },
  });
</script>
