<template>
  <dialog-form
    width="750px"
    ref="dialog"
    :title="formEdit.id ? '编辑' : '新增'"
    :form-edit="formEdit"
    :form-data="formData"
    labelWidth="120px"
    @handleConfirm="handleConfirm"
  ></dialog-form>
</template>

<script>
import { feignField } from "@/api/field.js"
import { getCoopGroupList } from "@/api/coop-group.js"
export default {
  name: "DialogFormSelf",

  data() {
    return {
      formEdit: {},
      formData: [
        {
          label: "疾病名",
          prop: "diseaseName",
          spanCount: 12,
          type: "input",
        },
        {
          label: "机构名",
          prop: "orgName",
          spanCount: 12,
          type: "input",
        },
        {
          label: "协作组",
          prop: "groupId",
          type: "select",
          opts: [],
        },
        {
          label: "表单名",
          prop: "formName",
          spanCount: 12,
          type: "input",
        },
        {
          label: "表单id",
          prop: "formId",
          spanCount: 12,
          type: "input",
        },
        {
          label: "原表名",
          prop: "sourceTableName",
          spanCount: 12,
          type: "input",
        },
        {
          label: "原表code",
          prop: "sourceTableCode",
          spanCount: 12,
          type: "input",
        },
        {
          label: "原字段名",
          prop: "sourceFieldName",
          spanCount: 12,
          type: "input",
        },
        {
          label: "原字段code",
          prop: "sourceFieldCode",
          spanCount: 12,
          type: "input",
        },
        {
          label: "目标表名",
          spanCount: 12,
          type: "input",
          prop: "targetTableCode",
        },
        {
          label: "目标表名code",
          spanCount: 12,
          type: "input",
          prop: "targetTableName",
        },
        {
          label: "目标字段名",
          type: "input",
          spanCount: 12,
          prop: "targetFieldName",
        },
        {
          label: "目标字段名code",
          type: "input",
          spanCount: 12,
          prop: "targetFieldCode",
        },
        {
          label: "字典",
          type: "input",
          spanCount: 12,
          prop: "dictType",
        },
        {
          label: "创建人",
          prop: "createUser",
          spanCount: 12,
          type: "input",
        },
        {
          label: "格式化方式",
          prop: "formatType",
          spanCount: 12,
          type: "select",
          optsFormatter: () => {
            return this.dictMap && this.dictMap["data_format_type"]
          },
        },
      ],
    }
  },

  methods: {
    // 获得协作组列表
    getCoopGroupList() {
      getCoopGroupList().then((res) => {
        const d = res.data
        this.formData[2].opts = d.map((item) => {
          return {
            label: item.groupName,
            value: item.id,
          }
        })
      })
    },

    handleAdd(row) {
      this.formEdit = Object.assign({}, row)
      this.$refs.dialog.open()
    },
    handleConfirm(form) {
      const data = Object.assign({}, form)

      data.groupName = this.$handle.formatDicList(
        this.formData[2].opts,
        form.groupId
      )
      const msg = data.id ? "编辑成功" : "新增成功"
      feignField(data).then((res) => {
        if (res.code === 1) {
          this.$message.success(msg)
          this.$refs.dialog.close()
          this.$emit("handleConfirm")
        }
      })
    },
  },
  created() {
    this.getCoopGroupList()
  },
}
</script>

<style lang="scss" scoped>
.drawer__container {
  padding: 0 20px;
  .drawer__content {
    height: calc(100vh - #{"160px"});
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 0px;
      height: 0px;
    }
    &::-webkit-scrollbar-thumb {
      background-color: #fff;
    }
  }
  .footer {
    margin-top: 20px;
    display: flex;
    button {
      flex: 1;
    }
  }
}
</style>
