<!--
 * @Author: tengmingxue 1473375109@qq.com
 * @Date: 2023-08-15 22:08:21
 * @LastEditors: tengmingxue 1473375109@qq.com
 * @LastEditTime: 2023-08-17 15:24:30
 * @FilePath: \xld-gis-admin\src\views\resource\map\SourceDetail.vue
 * @Description: 地图资源明细界面
-->
<template>
  <div class="source-detail">
    <a-form ref="formRef" :model="formState" :rules="rules" style="width: 100%">
      <div class="compontents">
        <div class="title">基本信息</div>
        <a-row class="form-container">
          <a-col :span="12" class="form-col">
            <a-form-item ref="code" label="资源编码" name="code" class="label-form-item">
              <a-input v-model:value="formState.code" />
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item
              ref="servicename"
              label="资源名称"
              name="servicename"
              class="label-form-item"
            >
              <a-input v-model:value="formState.servicename" />
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item
              ref="servicealiasname"
              label="资源别名"
              name="servicealiasname"
              class="label-form-item"
            >
              <a-input v-model:value="formState.servicealiasname" />
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item ref="keywords" label="资源标签" name="keywords" class="label-form-item">
              <a-select v-model:value="formState.keywords" mode="multiple">
                <template v-for="tag in serviceTags" :key="tag.code">
                  <a-select-option :value="tag.value">{{ tag.label }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="24" class="form-col">
            <a-form-item
              ref="description"
              label="资源描述"
              name="description"
              class="label-form-item"
            >
              <a-textarea v-model:value="formState.description" :rows="2" />
            </a-form-item>
          </a-col>

          <a-col :span="24" class="form-col">
            <a-form-item ref="thumbnail" label="缩略图" name="thumbnail" class="label-form-item">
              <a-upload action="" v-model:file-list="fileList">
                <a-button style="background-color: #0671dd; color: #fff; border-radius: 4px">
                  <upload-outlined></upload-outlined>
                  文件上传
                </a-button>
              </a-upload>
            </a-form-item>
          </a-col>
        </a-row>
      </div>

      <div class="compontents">
        <div class="title">描述信息</div>
        <a-row class="form-container">
          <a-col :span="12" class="form-col">
            <a-form-item
              ref="servicetype"
              label="资源类型"
              name="servicetype"
              class="label-form-item"
            >
              <a-select v-model:value="formState.servicetype">
                <template v-for="tag in serviceTypes" :key="tag.id">
                  <a-select-option :value="tag.id">{{ tag.name }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item ref="coordinate" label="坐标系" name="coordinate" class="label-form-item">
              <a-select v-model:value="formState.coordinate">
                <template v-for="tag in sysCoors" :key="tag.GEOMNAME">
                  <a-select-option :value="tag.GEOMNAME">{{ tag.GEOMTITLE }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24" class="form-col">
            <a-form-item ref="mapingurl" label="服务地址" name="mapingurl" class="label-form-item">
              <a-input v-model:value="formState.mapingurl" />
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item ref="dataScope" label="数据范围" name="dataScope" class="label-form-item">
              <a-input v-model:value="formState.dataScope" />
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item ref="source" label="数据来源" name="source" class="label-form-item">
              <a-input v-model:value="formState.source" />
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item ref="restype" label="密级" name="secrets" class="label-form-item">
              <a-select v-model:value="formState.secrets">
                <template v-for="tag in secrets" :key="tag.code">
                  <a-select-option :value="tag.code">{{ tag.name }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item
              ref="sourcetable"
              label="数据表"
              name="sourcetable"
              class="label-form-item"
            >
              <a-input v-model:value="formState.formdatabase" />
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item ref="collect" label="保管单位" name="collect" class="label-form-item">
              <a-select v-model:value="formState.collect">
                <template v-for="tag in collectDept" :key="tag.code">
                  <a-select-option :value="tag.code">{{ tag.name }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item ref="respUnit" label="责任处室" name="respUnit" class="label-form-item">
              <a-select v-model:value="formState.respUnit">
                <template v-for="tag in respDept" :key="tag.code">
                  <a-select-option :value="tag.code">{{ tag.name }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item ref="source" label="发布日期" name="source" class="label-form-item">
              <!-- <a-input v-model:value="formState.publishtime" disabled /> -->
              <a-date-picker
                v-model:value="formState.publishtime"
                format="YYYY-MM-DD hh:mm:ss"
                style="width: 100%"
                disabled
                show-time
              >
                <template #renderExtraFooter>extra footer</template>
              </a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item ref="source" label="发布者" name="source" class="label-form-item">
              <a-input v-model:value="formState.publisher" disabled />
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item
              ref="updatetype"
              label="更新方式"
              name="updatetype"
              class="label-form-item"
            >
              <a-input v-model:value="formState.updateType" />
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item
              ref="updatetime"
              label="更新时间"
              name="updatetime"
              class="label-form-item"
            >
              <a-date-picker
                v-model:value="formState.updatetime"
                format="YYYY-MM-DD hh:mm:ss"
                style="width: 100%"
                disabled
                show-time
              >
                <template #renderExtraFooter>extra footer</template>
              </a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :span="12" class="form-col">
            <a-form-item ref="epsgcode" label="EPSGCode" name="epsgcode" class="label-form-item">
              <a-select v-model:value="formState.epsgcode">
                <template v-for="tag in EPSGCodes" :key="tag.code">
                  <a-select-option :value="tag.code">{{ tag.name }}</a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="12" class="form-col">
            <a-form-item
              ref="runtimestatus"
              label="运行状态"
              name="runtimestatus"
              class="label-form-item"
            >
              <a-switch
                checked-children=""
                un-checked-children=""
                v-model:checked="formState.runtimestatus"
              />
            </a-form-item>
          </a-col>

          <a-col :span="6" class="form-col">
            <a-form-item ref="public" label="是否公开" name="public" class="label-form-item">
              <a-switch
                checked-children=""
                un-checked-children=""
                v-model:checked="formState.public"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6" class="form-col">
            <a-form-item ref="share" label="是否共享" name="share" class="label-form-item">
              <a-switch
                checked-children=""
                un-checked-children=""
                v-model:checked="formState.share"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6" class="form-col">
            <a-form-item
              ref="externalApply"
              label="是否外部申请"
              name="externalApply"
              class="label-form-item"
            >
              <a-switch
                checked-children=""
                un-checked-children=""
                v-model:checked="formState.externalApply"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6" class="form-col">
            <a-form-item ref="searched" label="是否展示" name="searched" class="label-form-item">
              <a-switch
                checked-children=""
                un-checked-children=""
                v-model:checked="formState.searched"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </div>
    </a-form>
  </div>
</template>
<script lang="ts">
import {
  defineComponent,
  ref,
  toRefs,
  toRaw,
  unref,
  computed,
  unref,
  nextTick,
  reactive,
  UnwrapRef,
  onMounted,
  watch,
} from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { UploadOutlined } from '@ant-design/icons-vue';
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
import { useMessage } from '/@/hooks/web/useMessage';
const { t } = useI18n(); //加载国际化
// 加载菜单数据
import { useI18n } from '/@/hooks/web/useI18n';
import Moment from 'moment';
import { insertService } from '/@/api/resource/map';
import {
  serviceTypes,
  serviceTags,
  secrets,
  sysCoors,
  respDept,
  collectDept,
  EPSGCodes,
} from './map.data';
import { session } from '/@/utils/Memory.js';
const props = {
  formData: { type: Object, default: ref(null) },
  isUpdate: { type: Boolean, default: ref(false) },
};
interface FormState {
  code: string;
  servicename: string;
  servicealiasname: string;
  keywords: string[];
  description: string;
  servicetype: string;
  coordinate: string;
  mapingurl: string;
  dataScope: string;
  source: string;
  secrets: string;
  formdatabase: string;
  collect: string;
  respUnit: string;
  publishtime: string;
  publisher: string;
  updateType: string;
  updatetime: string;
  epsgcode: string;
  runtimestatus: boolean;
  public: boolean;
  share: boolean;
  searched: boolean;
  externalApply: boolean;
  date1: undefined;
}
export default defineComponent({
  name: 'SourceDetail',
  components: { BasicForm, UploadOutlined },
  props,
  setup(props, { emit }) {
    const data = reactive({
      detail: props.formData,
      isUpdate: props.isUpdate,
      fileList: [],
      serviceTags: serviceTags,
      secrets: secrets, //密级
      sysCoors: sysCoors, //坐标系
      serviceTypes: serviceTypes, //资源类型
      respDept: respDept, //责任科室
      collectDept: collectDept, //保管部门
      EPSGCodes: EPSGCodes, //EPSGCode
    });
    const formRef = ref();
    const formState: UnwrapRef<FormState> = reactive({
      code: '',
      servicename: '',
      servicealiasname: '',
      keywords: [],
      description: '',
      servicetype: '',
      coordinate: '',
      mapingurl: '',
      dataScope: '',
      source: '',
      secrets: '',
      formdatabase: '',
      collect: '',
      respUnit: '',
      publishtime: '',
      publisher: '',
      updateType: '',
      updatetime: '',
      epsgcode: '',
      runtimestatus: true,
      public: true,
      share: true,
      searched: true,
      externalApply: true,
      date1: undefined,
    });
    const moment = Moment;
    const rules = {
      servicename: [
        { required: true, message: '请填写资源名称', trigger: 'blur' },
        { min: 3, max: 20, message: '长度必须在3到20个字符', trigger: 'blur' },
      ],
      servicealiasname: [
        { required: true, message: '请输入资源别名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度必须在3到20个字符', trigger: 'blur' },
      ],
      systag: [{ required: true, message: '请选择系统标签', trigger: 'change' }],
      servicetype: [{ required: true, message: '请选择资源类型', trigger: 'change' }],
      coordinate: [{ required: true, message: '请选择坐标系', trigger: 'change' }],
      mapingurl: [{ required: true, message: '请输入服务地址', trigger: 'blur' }],

      date1: [{ required: true, message: 'Please pick a date', trigger: 'change', type: 'object' }],
      type: [
        {
          type: 'array',
          required: true,
          message: 'Please select at least one activity type',
          trigger: 'change',
        },
      ],
      resource: [{ required: true, message: 'Please select activity resource', trigger: 'change' }],
      desc: [{ required: true, message: 'Please input activity form', trigger: 'blur' }],
    };
    const { createMessage } = useMessage();
    watch(
      () => props.formData,
      (obj) => {
        data.detail = obj;
        if (data.isUpdate) setFormData();
      }
    );
    watch(
      () => props.isUpdate,
      (obj) => {
        data.isUpdate = obj;
      }
    );
    const submitForm = () => {
      try {
        formRef.value
          .validate()
          .then(async () => {
            //console.log('values', formState, toRaw(formState));
            const params = setSubmitForm();
            if (params) {
              if (data.isUpdate) {
              } else {
                const res = await insertService(params);
                if (res && res?.status !== '-1') {
                  createMessage.success('新增地图资源成功！');
                  resetForm();
                  emit('RtnMain', true);
                } else createMessage.error('新增地图资源失败！,失败原因：' + res?.message, 1);
              }
            }
          })
          .catch((error: ValidateErrorEntity<FormState>) => {
            createMessage.error('请完成必填信息！', 1);
            console.log('error', error);
          });
      } catch (ex) {
        console.log(ex);
      }
    };
    const resetForm = () => {
      formRef.value.resetFields();
    };

    /**
     * 设置提交数据
     */
    const setSubmitForm = () => {
      const userinfo = session.getItem('userInfo');
      const collect = collectDept.find((item) => item.code === formState.collect);
      const rboffices = respDept.find((item) => item.code === formState.respUnit);
      const secretlevels = secrets.find((item) => item.code === formState.secrets);
      const coor = sysCoors.find((item) => item.GEOMNAME === formState.coordinate);
      const EPSGCode = EPSGCodes.find((item) => item.code === formState.epsgcode);
      return {
        type: 'MR',
        servicebase: {
          publisher: userinfo?.EMPLOYEE?.NAME, //'系统管理员',
          username: userinfo?.EMPLOYEE?.NAME, //'系统管理员',
          userid: userinfo?.EMPLOYEE?.EMPLOYEE_ID, //'27AF004A-8BDN-885T-30FU-89DE3388762B',
          servicealiasname: formState.servicealiasname, //资源别名
          description: formState.description, //资源描述
          ispublic: formState.public ? '1' : '0', //是否公开
          searched: formState.searched ? '1' : '0', //是否展示
          servicetype: formState.servicetype, //资源类型
          externalApply: formState.externalApply ? '1' : '0', //外部申请
          runtimestatus: formState.runtimestatus ? '1' : '0', //运行状态
          servicename: formState.servicename, //资源名
          mapingurl: formState.mapingurl, //服务地址
          otherService: '',
        },
        metadata: {
          mapingurl: formState.mapingurl, //服务地址
          name: formState.servicename, //资源名
          servicealiasname: formState.servicealiasname, //资源别名
          thumbnail: '',
          description: formState.description,
          keywords: formState.keywords.toString(),
          servicetype: formState.servicetype, //资源类型
          dataScope: formState.dataScope, //数据范围
          updateType: formState.updateType, //更新方式
          source: formState.source, //数据来源
          formdatabase: formState.formdatabase, //数据表
          rboffice: rboffices ? rboffices.codeName : '', //责任单位
          keepingunit: collect ? collect.codeName : '', //保管单位
          secretlevel: secretlevels ? secretlevels.codeName : '',
          crs: coor ? coor.GEOMNAME : '',
          epsgCode: EPSGCode ? EPSGCode.codeName : '',
          workSpace: '',
          runtimestatus: formState.runtimestatus ? '1' : '0', //运行状态
          ishistory: '0',
          shsqlc: 'ggdata',
          urlInfo: '',
          otherService: '',
          bz: '',
          ispublic: formState.public ? '1' : '0', //是否公开
          searched: formState.searched ? '1' : '0', //是否展示
          externalApply: formState.externalApply ? '1' : '0', //外部申请
          isShow: formState.share ? '1' : '0',
          publisher: userinfo?.EMPLOYEE?.NAME, //'系统管理员',
          username: userinfo?.EMPLOYEE?.NAME, //'系统管理员',
          userid: userinfo?.EMPLOYEE?.EMPLOYEE_ID, //'27AF004A-8BDN-885T-30FU-89DE3388762B',
        },
        sourcetraces: [],
        dataVersionConf: {},
      };
    };
    /**
     * 设置表单数据
     */
    const setFormData = () => {
      console.log('表单数据请看', data.detail);
      console.log('服务类型', serviceTypes);
      console.log('服务标签', serviceTags);
      console.log('密级', secrets);
      console.log('坐标系', sysCoors);
      console.log('责任科室', respDept);
      console.log('保管部门', collectDept);
      console.log('EPSGCodes', EPSGCodes);
      formState.public = data.detail.ispublic === '1' ? true : false;
      formState.servicename = data.detail?.servicename;
      formState.publishtime = data.detail?.publishdate
        ? moment(data.detail?.publishdate).format('YYYY-MM-DD HH:mm:ss')
        : '';
      formState.publisher = data.detail?.publisher;
      formState.runtimestatus = data.detail.runtimestatus === 0 ? false : true;
      formState.servicealiasname = data.detail?.servicealiasname;
      formState.code = data.detail?.serviceid;
      formState.servicetype = data.detail?.servicetype;
      formState.updatetime = data.detail?.updatedate
        ? moment(data.detail?.updatedate).format('YYYY-MM-DD HH:mm:ss')
        : '';
      formState.servicetype = data.detail?.servicetype;
      formState.coordinate = data.detail?.crs;
      formState.searched = data.detail.searched === 0 ? false : true;
    };

    onMounted(() => {
      resetForm();
    });
    return {
      ...toRefs(data),
      formRef,
      moment,
      rules,
      formState,
      submitForm,
      resetForm,
      createMessage,
      setSubmitForm,
      setFormData,
    };
  },
});
</script>
  
  <style scoped lang="less">
.source-detail {
  height: 100%;
  width: 100%;
  padding: 0 20px;
  .compontents {
    width: 100%;
    height: auto;
    .title {
      height: 19px;
      line-height: 18px;
      padding-left: 6px;
      margin-bottom: 20px;
      border-left: 3px solid #0671dd;
      font-family: Source Han Sans CN;
      font-size: 16px;
      font-weight: 350;
      letter-spacing: 0px;
    }
    .form-container {
      width: 100%;
      height: auto;
      .form-col {
        height: 100%;
      }
      .form-col-left {
        padding-right: 20px;
      }
    }
  }
}
.label-form-item {
  :deep(.ant-form-item-label) {
    width: 90px;
  }
  :deep(.ant-form-item-control) {
    width: calc(100% - 90px);
  }
}
</style>
  