<template>
  <div>
    <a-form layout='vertical'>
      <a-spin :spinning="loading">
        <a-tabs v-model="tabName" type="card">
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.name')"
              key="name">
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.language')">
                  <a-select
                      @change="currentNlsChange"
                      :defaultValue="defaultNls">
                    <a-select-option
                        v-for="(item, index) in currentNls"
                        :key="index"
                        :value="item.lkey">
                      {{ $t('global-dict.select-nls.' + item.lkey) }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item required :label="$t('functional-analysis.property-modal.name')">
                  <a-input v-model="name" :maxLength="1000" @change="nameChange"/>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.id')">
                  {{ seriesNo }}
                </a-form-item>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.function-type')"
              key="functionType">
            <a-table
                :data-source="ftTableData"
                :columns="ftTableColumns"
                :customRow="ftCustomRow"
                :rowKey="(row, index) => {return index;}"
                :row-selection="{
                type: 'radio',
                selectedRowKeys: ftSelectedRowKeys,
                onChange: ftRadioChange,
              }"
                bordered
                size="small"
                :scroll="{ y: 'calc(88vh - 400px)' }"
                :pagination="false">
            </a-table>
          </a-tab-pane>
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.user-defined-characteristics')"
              key="userDefinedProperties">
            <a-table
                :data-source="userTableData"
                :columns="userTableColumns"
                :customRow="userCustomRow"
                :rowKey="(row, index) => {return index;}"
                :row-selection="{
                selectedRowKeys: userSelectedRowKeys,
                onChange: userCheckboxChange
               }"
                bordered
                size="small"
                :scroll="{ y: 'calc(88vh - 400px)' }"
                :pagination="false"/>
          </a-tab-pane>
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.specification')"
              key="specification">
            <a-row :gutter='24'>
              <a-col :md="24" :sm="24" class="tl">
                <a-tag id="copySuffix1" @click="handleCopySuffix('copySuffix1','Ⓤ')">Ⓤ</a-tag>
                <a-tag id="copySuffix2" @click="handleCopySuffix('copySuffix2','Ⓜ')">Ⓜ</a-tag>
                <a-tag id="copySuffix3" @click="handleCopySuffix('copySuffix3','Ⓛ')">Ⓛ</a-tag>
                <a-tag id="copySuffix4" @click="handleCopySuffix('copySuffix4','Ⓔ')">Ⓔ</a-tag>
                <a-tag id="copySuffix5" @click="handleCopySuffix('copySuffix5','Ⓟ')">Ⓟ</a-tag>
                <a-tag id="copySuffix6" @click="handleCopySuffix('copySuffix6','Ⓕ')">Ⓕ</a-tag>
                <a-tag id="copySuffix7" @click="handleCopySuffix('copySuffix7','Ⓣ')">Ⓣ</a-tag>
                <a-tag id="copySuffix8" @click="handleCopySuffix('copySuffix8','ⓘ')">ⓘ</a-tag>
                <a-tag id="copySuffix9" @click="handleCopySuffix('copySuffix9','▷')">▷</a-tag>
                <a-tag id="copySuffix10" @click="handleCopySuffix('copySuffix10','∅')">∅</a-tag>
                <a-tag id="copySuffix11" @click="handleCopySuffix('copySuffix11','S∅')">S∅</a-tag>
                <a-tag id="copySuffix12" @click="handleCopySuffix('copySuffix12','R')">R</a-tag>
                <a-tag id="copySuffix13" @click="handleCopySuffix('copySuffix13','SR')">SR</a-tag>
                <a-tag id="copySuffix14" @click="handleCopySuffix('copySuffix14','CR')">CR</a-tag>
                <a-tag id="copySuffix15" @click="handleCopySuffix('copySuffix15','◻')">◻</a-tag>
                <a-tag id="copySuffix16" @click="handleCopySuffix('copySuffix16','( )')">( )</a-tag>
                <a-tag id="copySuffix17" @click="handleCopySuffix('copySuffix17','︵')">︵</a-tag>
                <a-tag id="copySuffix18" @click="handleCopySuffix('copySuffix18','⟷')">⟷</a-tag>
                <a-tag id="copySuffix19" @click="handleCopySuffix('copySuffix19','∆')">∆</a-tag>
                <a-tag id="copySuffix20" @click="handleCopySuffix('copySuffix20','→')">→</a-tag>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24" class="mt10">
                <a-form-item :label="$t('functional-analysis.property-modal.value')">
                  <a-select
                      class="mb_8"
                      @change="featureLogicChange"
                      :defaultValue="featureLogic"
                      allowClear
                  >
                    <a-select-option
                        v-for="(item, index) in featureValueSelect"
                        :key="index"
                        :value="item.value">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                  <a-textarea
                      :placeholder="$t('functional-analysis.property-modal.click-to-enter-value')"
                      allowClear
                      v-model="featureValue"
                      :autoSize="{ minRows: 5 }"
                      :maxLength="500"/>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.unit')">
                  <a-input v-model="unit" :maxLength="30"/>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.tolerance')">
                  <a-input v-model="tolerance" :maxLength="30"/>
                </a-form-item>
              </a-col>
            </a-row>
            <!--形位公差符号-->
            <div v-for="(item,index) in form.dynamicItem" :key="index">
              <a-row :gutter='24'>
                <a-col :md="12" :sm="12">
                  <a-form-item :label="$t('functional-analysis.property-modal.form-tolerance-symbol')">
                    <a-select
                        :value="item.symbol"
                        @change="handleSymbolChange($event, item)"
                        allowClear
                    >
                      <a-select-option
                          v-for="(item, index) in geometricValueSelect"
                          :key="index"
                          :value="item.value"
                      >
                        <span
                            style="font-family: Verisurf;"
                            :class="item.name"
                        />
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="12" :sm="12">
                  <a-form-item :label="'\xa0'">
                    <a-button type="primary" class="mr10" v-if="item.default" @click="addItem">
                      {{ $t('global-btn.add') }}
                    </a-button>
                    <a-button class="mr10" v-if="!item.default" @click="deleteItem(item)">
                      {{ $t('global-btn.delete') }}
                    </a-button>
                    <a-button type="primary" class="mr10" v-if="item.datumChildren.length < 5" @click="addDatum(item)">
                      {{ $t('global-btn.add-datum') }}
                    </a-button>
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row :gutter='24'>
                <a-col :md="12" :sm="12">
                  <a-form-item :label="$t('functional-analysis.property-modal.tolerance')">
                    <a-input :value="item.content" @change="handleContentChange($event, item)" :maxLength="100"/>
                  </a-form-item>
                </a-col>
              </a-row>
              <span v-for="(datum,datumIndex) in item.datumChildren" :key="datumIndex">
                <a-row :gutter='24'>
                  <a-col :md="12" :sm="12">
                    <a-form-item :label="$t('functional-analysis.property-modal.datum')">
                      <a-input :value="datum.value" @change="handleDatumChange($event, datum)" :maxLength="100"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="6">
                    <a-form-item :label="$t('functional-analysis.property-modal.suffix')">
                      <a-select
                          :value="datum.suffix"
                          @change="handleSuffixChange($event, datum)"
                          allowClear
                      >
                        <a-select-option
                            v-for="(item, index) in suffixSelect"
                            :key="index"
                            :value="item.value"
                        >
                          {{ item.name }}
                        </a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="6">
                    <a-form-item :label="'\xa0'">
                      <a-button @click="deleteDatum(item.datumChildren, datum)">
                        {{ $t('global-btn.delete') }}
                      </a-button>
                    </a-form-item>
                  </a-col>
                </a-row>
              </span>
            </div>
          </a-tab-pane>
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.feature-classification')"
              key="featureClassification">
            <a-table
                :data-source="fcTableData"
                :columns="fcTableColumns"
                :customRow="fcCustomRow"
                :rowKey="(row, index) => {return index;}"
                :row-selection="{
                type: 'radio',
                selectedRowKeys: fcSelectedRowKeys,
                onChange: fcRadioChange
              }"
                bordered
                size="small"
                :scroll="{ y: 'calc(88vh - 400px)' }"
                :pagination="false">
            </a-table>
          </a-tab-pane>
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.note')"
              key="remarks">
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.note-group')">
                  <a-select
                      @change="nodeGroupChange"
                      v-model="currentNodeGroupKey"
                  >
                    <a-select-option
                        v-for="(item, index) in nodeGroups"
                        :key="index"
                        :value="item.annotationGroup"
                    >
                      {{ item.groupName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.note')">
              <span v-for="(item, index) in struNls" :key="index">
                <a-textarea
                    v-if="defaultNls[0] === item.lkey"
                    :placeholder="$t('functional-analysis.property-modal.click-to-enter-note')"
                    allowClear
                    v-model="item.description"
                    :autoSize="{ minRows: 5 }"
                    :maxLength="500"
                />
              </span>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.attachment')">
                  <a-upload
                      name="file"
                      action=""
                      accept=".jpg,.txt,.png,.doc,.docx,.xls,.xlsx,.pdf,.pptx"
                      :fileList="fileList"
                      :customRequest="customRequest"
                      :remove="removeFile">
                    <a-button v-if="$api.isLook !== '1'">
                      <a-icon type="upload"/>
                      {{ $t('global-btn.select-file') }}
                    </a-button>
                  </a-upload>
                </a-form-item>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane
              :tab="$t('functional-analysis.property-modal.about')"
              key="about">
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.create-time')">
                  {{ insertTime }}
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter='24'>
              <a-col :md="12" :sm="24">
                <a-form-item :label="$t('functional-analysis.property-modal.update-time')">
                  {{ updateTime }}
                </a-form-item>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </a-spin>
    </a-form>
    <div slot="footer"
         v-if="$api.isLook !== '1'"
         class="mt10 text-right">
      <a-button @click="closeModal">
        {{ $t('global-btn.cancel') }}
      </a-button>
      <a-button
          type="primary"
          class="ml10"
          @click="save()"
          :loading="saveLoading"
          v-preventReClick>
        {{ $t('global-btn.confirm') }}
      </a-button>
    </div>
  </div>
</template>
<script>

import {dateFormat} from "@/utils/date";
import {checkHotTableLength} from "@/utils/validate";
import {v4 as uuidv4} from "uuid";
import Clipboard from "clipboard";
/**
 * 渲染特性分类表格
 * @param value
 * @param row
 * @param index
 * @returns {{children, attrs: {}}}
 */
const renderContent = (value, row, index) => {
  const obj = {
    children: value,
    attrs: {},
  };
  if (!row.detailId) {
    obj.attrs.colSpan = 0;
  }
  return obj;
};

export default {
  name: "PropertyRequire",
  components: {},
  props: {
    tabKey: {
      type: String,
      default: "",
    },
    checkedNode: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      form: {
        /** 行为公差符号 */
        dynamicItem: [
          {
            key: uuidv4(),
            default: true,
            symbol: "",
            content: "",
            /** 基准数组 */
            datumChildren: []
          }
        ]
      },
      /** tab默认选中 */
      tabName: this.tabKey || "name",
      /** 语言下拉数组 */
      currentNls: [],
      /** 语言名称数组 */
      struNls: [],
      /** 默认语言标识 */
      defaultNls: [],
      /** 名称 */
      name: "",
      /** 序号 */
      seriesNo: "",
      /** 功能种类：表格数据 */
      ftTableData: [],
      /** 功能种类：表格表头 */
      ftTableColumns: [
        {
          title: () => this.$t('functional-analysis.property-modal.name'),
          align: "center",
          dataIndex: 'lname',
          ellipsis: true,
        },
      ],
      /** 功能种类：选中索引 */
      ftSelectedRowKeys: [],
      /** 功能种类：选中数据 */
      ftSelectedRow: {},
      /** 用户定义的特性：表格数据 */
      userTableData: [],
      /** 用户定义的特性：表格表头 */
      userTableColumns: [
        {
          title: () => this.$t('functional-analysis.property-modal.name'),
          align: "center",
          dataIndex: "memo",
          key: "memo",
          ellipsis: true,
        },
      ],
      /** 用户定义的特性：选中索引 */
      userSelectedRowKeys: [],
      /** 用户定义的特性：选中数据 */
      userSelectedRows: [],
      /** 特性分类：表格数据 */
      fcTableData: [],
      /** 特性分类：表格表头 */
      fcTableColumns: [
        {
          title: () => this.$t('functional-analysis.property-modal.icon/mark'),
          align: "center",
          dataIndex: 'classifyType',
          customRender: (text, row, index) => {
            /** 1：图标，2：标识 */
            if (row.classifyType === "1") {
              let url = this.$api.baseUrl + '/DFMEA/file/' + row.fileName;
              text = <img width="25" height="25" src={url} alt=""/>;
            } else if (row.classifyType === "2") {
              text = row.lexplain;
            }
            if (row.detailId) return text;
            return {
              children: text,
              attrs: {
                colSpan: 3,
              },
            };
          },
        },
        {
          title: () => this.$t('functional-analysis.property-modal.name'),
          align: "center",
          dataIndex: 'lname',
          customRender: renderContent,
          ellipsis: true,
        }, {
          title: () => this.$t('functional-analysis.property-modal.note'),
          align: "center",
          dataIndex: 'ldescription',
          customRender: renderContent,
          ellipsis: true,
        },
      ],
      /** 特性分类：选中索引 */
      fcSelectedRowKeys: [],
      /** 特性分类：选中数据 */
      fcSelectedRow: {},
      /** 用户定义的特性：数值下拉 */
      featureValueSelect: [
        {value: "01", name: "="},
        {value: "02", name: ">"},
        {value: "03", name: "<"},
        {value: "04", name: "≥"},
        {value: "05", name: "≤"},
        {value: "06", name: "±"},
      ],
      geometricValueSelect: [
        {value: "⏥", name: "verisurf_005B"},
        {value: "⏤", name: "verisurf_005F"},
        {value: "◯", name: "verisurf_007B"},
        {value: "⌭", name: "verisurf_007D"},
        {value: "⌒", name: "verisurf_0021"},
        {value: "⌓", name: "verisurf_007E"},
        {value: "⫽", name: "verisurf_002A"},
        {value: "⟂", name: "verisurf_0026"},
        {value: "∠", name: "verisurf_005E"},
        {value: "⌖", name: "verisurf_0023"},
        {value: "◎", name: "verisurf_0024"},
        {value: "⌯", name: "verisurf_0025"},
        {value: "↗", name: "verisurf_003B"},
        {value: "⌰", name: "verisurf_005C"},
      ],
      /** 后缀UMLE */
      suffixSelect: [
        {value: "Ⓤ", name: "Ⓤ"},
        {value: "Ⓜ", name: "Ⓜ"},
        {value: "Ⓛ", name: "Ⓛ"},
        {value: "Ⓔ", name: "Ⓔ"},
        {value: "Ⓟ", name: "Ⓟ"},
        {value: "Ⓕ", name: "Ⓕ"},
        {value: "Ⓣ", name: "Ⓣ"},
        {value: "ⓘ", name: "ⓘ"},
        {value: "▷", name: "▷"},
        {value: "∅", name: "∅"},
        {value: "S∅", name: "S∅"},
        {value: "R", name: "R"},
        {value: "SR", name: "SR"},
        {value: "CR", name: "CR"},
        {value: "SR", name: "SR"},
        {value: "◻", name: "◻"},
        {value: "( )", name: "( )"},
        {value: "︵", name: "︵"},
        {value: "⟷", name: "⟷"},
        {value: "∆", name: "∆"},
        {value: "→", name: "→"},
      ],
      /** 规格：符号 */
      featureLogic: "",
      /** 规格：数值 */
      featureValue: "",
      /** 规格：单位 */
      unit: "",
      /** 规格：公差 */
      tolerance: "",
      /** 备注分组 */
      nodeGroups: [],
      /** 默认备注分组Key */
      currentNodeGroupKey: undefined,
      /** 文件集合 */
      fileList: [],
      /** 创建时间 */
      insertTime: "",
      /** 更新时间 */
      updateTime: "",
      /** 保存加载中... */
      saveLoading: false,
      /** 加载中状态 */
      loading: false,
    };
  },
  mounted() {
    this.list();
  },
  methods: {
    /**
     * 复制后缀
     * @param eleId
     * @param val
     */
    handleCopySuffix(eleId, val) {
      let _that = this;
      let clipboard = new Clipboard('#' + eleId, {
        text: () => {
          return val
        }
      });
      clipboard.on('success', e => {
        _that.$message.success(_that.$t('functional-analysis.property-modal.copy-success'), 1);
        //  释放内存
        clipboard.destroy();
      });
      clipboard.on('error', e => {
        // 不支持复制
        _that.$message.warning(_that.$t('functional-analysis.property-modal.browser-no-copy'),1)
        // 释放内存
        clipboard.destroy();
      });
    },
    /**
     * 处理行位公差符号 -- 下拉框变化
     * @param val
     * @param item
     */
    handleSymbolChange(val, item) {
      item.symbol = val || '';
    },
    /**
     * 处理后缀 -- 下拉框变化
     * @param val
     * @param item
     */
    handleSuffixChange(val, item) {
      item.suffix = val || '';
    },
    /**
     * 处理公差 -- 输入框变化
     * @param e
     * @param item
     */
    handleContentChange(e, item) {
      item.content = e.target.value;
    },
    /**
     * 处理基准 -- 输入框变化
     * @param e
     * @param item
     */
    handleDatumChange(e, item) {
      item.value = e.target.value;
    },
    /**
     *添加一行形位公差符号
     */
    addItem() {
      this.form.dynamicItem.push({
        key: uuidv4(),
        default: false,
        symbol: "",
        content: "",
        /** 基准数组 */
        datumChildren: []
      });
    },
    /**
     * 添加基准
     * @param item
     */
    addDatum(item) {
      item.datumChildren.push({
        key: uuidv4(),
        value: '',
        suffix: ''
      });
    },
    /**
     * 删除一行形位公差
     * @param item
     */
    deleteItem(item) {
      let _that = this;
      _that.$confirm({
        title: this.$t('global-message.warn'),
        content: this.$t('global-message.warn.delete-row'),
        icon: "aaoopqa", //图标，不想用就随便写一个
        okText: this.$t('global-btn.ok'),
        cancelText: this.$t('global-btn.cancel'),
        onOk() {
          let delIndex = _that.form.dynamicItem.findIndex(val => {
            return item.key === val.key;
          });
          _that.form.dynamicItem.splice(delIndex, 1);
        },
        onCancel() {
        }
      });
    },
    /**
     * 删除基准
     * @param datumChildren
     * @param item
     */
    deleteDatum(datumChildren, item) {
      let delIndex = datumChildren.findIndex(val => {
        return item.key === val.key;
      });
      datumChildren.splice(delIndex, 1);
    },
    /**
     * 关闭模态框
     */
    closeModal() {
      this.$emit("closeModal");
    },
    /**
     * 刷新右侧结构树
     */
    initRightTree() {
      this.$emit("initRightTree");
    },
    /**
     * 名称改变时
     */
    nameChange(e) {
      this.struNls.forEach((item, index) => {
        if (item.lkey === this.defaultNls[0]) {
          item.name = e.target.value;
        }
      });
    },
    /**
     * 语言下拉选择时
     */
    currentNlsChange(val) {
      let struNls = this.struNls.filter((item) => {
        return item.lkey === val;
      })[0];
      this.defaultNls[0] = val;
      this.name = struNls.name;
    },
    /**
     * 备注分组下拉选择时
     */
    nodeGroupChange(val) {
      let _that = this;
      _that.currentNodeGroupKey = val;
      _that.$api.getNodeGroupSelect({
        fmeaId: _that.$store.getters.rfmeaId,
        struId: _that.checkedNode.id,
        annotationGroup: _that.currentNodeGroupKey,
      }).then(res => {
        if (res.flag) {
          // 处理附件
          _that.handleFileList(res.data.mfmeaAttachmentList || []);
          // 处理备注分组多语言
          let nodeGroupNls = res.data.mfmeaAnnotationNlsList || [];
          const newStruNls = [..._that.struNls];
          (nodeGroupNls || []).forEach(nodeGroup => {
            (newStruNls || []).forEach(nls => {
              if (nls.lkey === nodeGroup.lkey) {
                nls.description = nodeGroup.lvalue;
              }
            })
          });
          _that.struNls = newStruNls;
        } else {
          _that.$message.error(res.message, 1);
        }
      });
    },
    /**
     * 符号下拉选择时
     */
    featureLogicChange(val) {
      this.featureLogic = val;
    },
    /**
     * 处理文件列表
     * @param files 文件
     */
    handleFileList(files) {
      this.fileList = []
      if (files.length === 0) return;
      for (let i = 0, len = files.length; i < len; i++) {
        this.fileList.push({
          data: {
            hiddenFileName: files[i].attachmentName,
            hiddenNewFileName: files[i].storageName,
            hiddenDir: files[i].attachmentPath,
            hiddenFileSize: files[i].attachmentSize,
          },
          uid: files[i].attachmentId,
          name: files[i].attachmentName,
          status: 'done',
          url: this.$api.requestUrl + '/DFMEA/mfmea/funAnalysis/Rb0426341/downLoad?fmeaId=' + this.$store.getters.rfmeaId
              + '&attachmentId=' + files[i].attachmentId,
        });
      }
    },
    /**
     * 列表
     */
    list() {
      let _that = this;
      _that.loading = true;
      _that.$api.getRequireProperties({
        fmeaId: _that.$store.getters.rfmeaId,
        struId: _that.checkedNode.id,
      }).then((res) => {
        if (res.flag) {
          /**************** 名称 ****************/
          if (!res.data.display_nls) res.data.display_nls = 1;
          _that.defaultNls[0] = res.data.display_nls;
          _that.currentNls = res.data.mfmeacurrentNls || [];
          // 语言Key升序
          _that.struNls = (res.data.mfmeaStruNlss || []).sort((a, b) => {
            return a.lkey - b.lkey;
          });
          let struNls = _that.struNls.filter((item) => {
            return res.data.display_nls === item.lkey;
          })[0];
          _that.name = struNls.name;
          _that.seriesNo = res.data.mfmeaStruTree.seriesNo;
          /**************** 功能种类 ****************/
          _that.ftTableData = res.data.rfmAttrDetailModels || [];
          if (_that.ftTableData && _that.ftTableData.length > 0)
            _that.ftTableData.push({
              detailId: "",
              lname: this.$t('functional-analysis.property-modal.not-set')
            });
          _that.ftTableData.forEach((item, index) => {
            if (res.data.mfmeaRequire.category === item.detailId) {
              _that.ftSelectedRowKeys[0] = index;
              _that.ftSelectedRow = item;
            }
          });
          // 默认：未设置
          if (!res.data.mfmeaRequire.category) {
            let ftSize = _that.ftTableData.length;
            _that.ftSelectedRowKeys[0] = ftSize - 1;
            _that.ftSelectedRow = _that.ftTableData[ftSize - 1];
          }
          /**************** 用户定义的特性 ****************/
          _that.userTableData = res.data.mfmeaStruAttributeModels || [];
          _that.userTableData.forEach((item, index) => {
            if (item.checkFlag) {
              _that.userSelectedRowKeys.push(index);
              _that.userSelectedRows.push(item);
            }
          });
          /**************** 规格 ****************/
          _that.featureLogic = res.data.mfmeaRequire.featureLogic;
          _that.featureValue = res.data.mfmeaRequire.featureValue;
          _that.unit = res.data.mfmeaRequire.unit;
          _that.tolerance = res.data.mfmeaRequire.tolerance;
          // 行为公差符号
          if (res.data.geometric) {
            _that.form.dynamicItem = JSON.parse(res.data.geometric);
          }
          /**************** 特性分类 ****************/
          _that.fcTableData = res.data.rfmPductClassifyDetailModels || [];
          if (_that.fcTableData && _that.fcTableData.length > 0)
            _that.fcTableData.push({
              detailId: "",
              classifyType: "2",
              lexplain: this.$t('functional-analysis.property-modal.not-set')
            });
          _that.fcTableData.forEach((item, index) => {
            if (res.data.mfmeaRequire.classifyId === item.detailId) {
              _that.fcSelectedRowKeys[0] = index;
              _that.fcSelectedRow = item;
            }
          });
          // 默认：未设置
          if (!res.data.mfmeaRequire.classifyId) {
            let ftSize = _that.fcTableData.length;
            _that.fcSelectedRowKeys[0] = ftSize - 1;
            _that.fcSelectedRow = _that.fcTableData[ftSize - 1];
          }
          /**************** 备注 ****************/
          _that.currentNodeGroupKey = res.data.currentGroupId;
          _that.nodeGroups = res.data.mfmeaAnnotationGroupList || [];
          let nodeGroupNls = res.data.mfmeaAnnotationNlsList || [];
          // 处理备注分组多语言
          (nodeGroupNls || []).forEach(nodeGroup => {
            (_that.struNls || []).forEach(nls => {
              if (nls.lkey === nodeGroup.lkey) {
                nls.description = nodeGroup.lvalue;
              }
            })
          });
          // 处理附件
          _that.handleFileList(res.data.mfmeaAttachmentList || []);
          /**************** 关于 ****************/
          _that.insertTime = dateFormat(res.data.mfmeaRequire?.insertTime);
          _that.updateTime = dateFormat(res.data.mfmeaRequire?.updateTime);
        } else {
          _that.$message.error(res.message, 1);
        }
      }).catch((error) => {
        _that.$message.error(error.response.data.message, 1);
      }).finally(() => {
        _that.loading = false;
      });
    },
    /**
     * 保存
     */
    save() {
      let _that = this;
      // 校验
      if (!_that.validatePropertyData()) return;

      /**************** 数据封装 Start ***********/
      let struNames = _that.struNls.map((item) => {
        return item.name;
      });
      let descriptions = _that.struNls.map((item) => {
        return item.description || '';
      });
      let userDefineIds = _that.userSelectedRows.map((item) => {
        return item.userDefineId;
      });
      let attrDetailId = _that.ftSelectedRow ? _that.ftSelectedRow.detailId : '';
      let pductClassifyDetailId = _that.fcSelectedRow ? _that.fcSelectedRow.detailId : '';
      let hiddenFileName = [];
      let hiddenNewFileName = [];
      let hiddenDir = [];
      let hiddenFileSize = [];
      let files = _that.fileList;
      for (let i = 0, len = files.length; i < len; i++) {
        let file = files[i].data;
        hiddenFileName.push(file.hiddenFileName);
        hiddenNewFileName.push(file.hiddenNewFileName);
        hiddenDir.push(file.hiddenDir);
        hiddenFileSize.push(file.hiddenFileSize);
      }
      /**************** 数据封装 End ***********/

      _that.saveLoading = true; // 开启：加载中...
      _that.$api.saveRequireProperties({
        fmeaId: _that.$store.getters.rfmeaId,
        struId: _that.checkedNode.id,
        struNames: struNames, /** 名称 */
        attrDetailId: attrDetailId, /** 功能种类 */
        pductClassifyDetailId: pductClassifyDetailId, /** 特性分类 */
        userDefineIds: userDefineIds, /** 用户定义的特性 */
        featureLogic: _that.featureLogic, /** 符号 */
        featureValue: _that.featureValue, /** 数值 */
        unit: _that.unit, /** 单位 */
        tolerance: _that.tolerance, /** 公差 */
        descriptions: descriptions, /** 备注 */
        lkey: _that.defaultNls[0], /** 备注分组语言Key */
        annotationGroup: _that.currentNodeGroupKey, /** 备注分组Key */
        hiddenFileName: hiddenFileName, /** 隐藏的附件名称 */
        hiddenNewFileName: hiddenNewFileName, /** 隐藏的新生成附件名称 */
        hiddenDir: hiddenDir, /** 隐藏的新生成附件名称 */
        hiddenFileSize: hiddenFileSize, /** 隐藏的新生成附件大小 */
        geometric: JSON.stringify(_that.form.dynamicItem), /** 形位公差 */
      }).then((res) => {
        if (res.flag) {
          // 添加成功，关闭弹窗，刷新结构树
          _that.$message.success(res.message, 1);
          _that.closeModal();
          _that.initRightTree();
        } else {
          _that.$message.error(res.message, 1);
        }
      }).catch((error) => {
        _that.$message.error(error.response.data.message, 1);
      }).finally(() => {
        _that.saveLoading = false; // 关闭：加载中...
      })
    },
    /**
     * 校验：属性数据
     */
    validatePropertyData() {
      let flag = true;
      this.struNls.some((item, idx) => {
        let nlsKey = this.$t('global-dict.select-nls.' + item.lkey);
        if (!item.name) {
          this.$message.warning(this.$t('global-message.warn.name-cannot-be-empty', [nlsKey]));
          flag = false;
          return true;
        }
        if (checkHotTableLength(item.name, 1000)) {
          this.$message.warning(this.$t('global-message.warn.name-cannot-be-empty', [nlsKey]));
          flag = false;
          return true;
        }
      });
      if (checkHotTableLength(this.featureValue, 500)) {
        this.$message.warning(this.$t('global-message.warn.feature-value-length'));
        flag = false;
        return true;
      }
      if (checkHotTableLength(this.unit, 30)) {
        this.$message.warning(this.$t('global-message.warn.unit-length'));
        flag = false;
        return true;
      }
      if (checkHotTableLength(this.tolerance, 30)) {
        this.$message.warning(this.$t('global-message.warn.tolerance-length'));
        flag = false;
        return true;
      }
      return flag;
    },
    /**
     * 单选框改变事件
     */
    ftRadioChange(rowKeys, row) {
      this.ftSelectedRowKeys = rowKeys;
      this.ftSelectedRow = row[0];
    },
    /**
     * 行点击事件
     */
    ftCustomRow(record, index) {
      let _that = this;
      return {
        on: {
          // 鼠标单击行
          click: () => {
            _that.ftSelectedRowKeys = [index];
            _that.ftSelectedRow = record;
          }
        }
      };
    },
    /**
     * 复选框改变事件
     */
    userCheckboxChange(rowKeys, row) {
      this.userSelectedRows = row;
      this.userSelectedRowKeys = rowKeys;
    },
    /**
     * 行点击事件
     */
    userCustomRow(record, index) {
      let _that = this;
      return {
        on: {
          // 鼠标单击行
          click: () => {
            if (_that.userSelectedRowKeys.indexOf(index) !== -1) {
              _that.userSelectedRows.splice(_that.userSelectedRowKeys.indexOf(index), 1);
              _that.userSelectedRowKeys.splice(_that.userSelectedRowKeys.indexOf(index), 1);
            } else {
              _that.userSelectedRowKeys.push(index)
              _that.userSelectedRows.push(record);
            }
          }
        }
      };
    },
    /**
     * 单选框改变事件
     */
    fcRadioChange(rowKeys, row) {
      this.fcSelectedRowKeys = rowKeys;
      this.fcSelectedRow = row[0];
    },
    /**
     * 行点击事件
     */
    fcCustomRow(record, index) {
      let _that = this;
      return {
        on: {
          // 鼠标单击行
          click: () => {
            _that.fcSelectedRowKeys = [index];
            _that.fcSelectedRow = record;
          }
        }
      };
    },
    /**
     * 通过覆盖默认的上传行为，可以自定义自己的上传实现
     * @param obj 回调对象
     */
    customRequest(obj) {
      let _that = this;
      if (obj.file.size > 52428800) {
        _that.$message.warning(this.$t('global-message.warn.uploaded-files-cannot-exceed'));
        return;
      }
      _that.loading = true;
      let fd = new FormData();
      fd.append("file", obj.file);
      fd.append("fileType", "10");
      _that.$api.uploadFile(fd).then((res) => {
        if (res.flag) {
          _that.$message.success(res.message, 1);
          obj.file.data = {
            hiddenFileName: res.data.fileName,
            hiddenNewFileName: res.data.newFileName,
            hiddenDir: res.data.filePath,
            hiddenFileSize: res.data.fileSize,
          };
          _that.fileList.push(obj.file);
        } else {
          _that.$message.error(res.message, 1);
        }
      }).catch((error) => {
        _that.$message.error(error.response.data.message, 1);
      }).finally(() => {
        _that.loading = false;
      });
    },
    /**
     * 删除文件
     * @param file 文件
     */
    removeFile(file) {
      let files = this.fileList;
      for (let i = files.length - 1; i >= 0; i--) {
        if (file.uid === files[i].uid) files.splice(i, 1);
      }
    },
  }
};
</script>
<style lang="less" scoped>


/deep/ .ant-table-tbody {
  .ant-table-row-cell-ellipsis:nth-of-type(2) {
    text-align: left !important;
  }

  .ant-table-row-cell-ellipsis:nth-of-type(3) {
    text-align: left !important;
  }

  .ant-table-row-cell-ellipsis:nth-of-type(4) {
    text-align: left !important;
  }
}

/*/deep/ .ant-table-fixed-header > .ant-table-content > .ant-table-scroll > .ant-table-body {
  overflow-y: auto !important;
}*/

/deep/ a {
  color: #1890FF;
}
/deep/ .ant-table-fixed-header .ant-table-scroll .ant-table-header {
  overflow: hidden!important;
  margin-bottom: 0!important;
  padding-right: 8px;
  background: rgb(250, 250, 250);
}
</style>
