<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-icon_personalcenter"
        @breadcrumb-click="handleClose"
      />
    </div>
    <div class="form-content">
      <el-form
        ref="formInfo"
        :model="parameter"
        label-width="auto"
        label-position="right"
        v-loading="loading"
        :rules="rules"
      >
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">基本信息</div>
          </div>
        </div>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="发起企业" prop="orgName">
              <el-input
                readonly
                v-model="parameter.orgName"
                placeholder="请输入发起企业"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="共享企业" prop="sharingOrgName">
              <el-input
                clearable
                @focus="open"
                prefix-icon="el-icon-search"
                v-model="parameter.sharingOrgName"
                placeholder="请选择共享企业"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="共享项目" prop="noticeCode">
              <el-input
                clearable
                @focus="onClick"
                prefix-icon="el-icon-search"
                v-model="parameter.noticeCode"
                placeholder="请选择共享项目"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="菜单设置" prop="sharingMenuAlias">
              <el-cascader
                :options="menuList"
                v-model="parameter.sharingMenuAlias"
                @change="changeAlias"
                ref="cascader"
                :props="{
                  value: 'alias',
                  label: 'name',
                  emitPath: false,
                  multiple: true,
                }"
                filterable
                placeholder="请选择共享菜单"
                clearable
                style="width: 100%"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
    </div>
    <div slot="footer" class="footer">
      <el-button @click="handleClose">{{ $t('domestic.cancel') }}</el-button>
      <el-button type="primary" :loading="saveing" @click="handleSave">{{
        $t('domestic.save')
      }}</el-button>
    </div>
    <expertOrgUnits
      @addDetail="addDetail"
      :useOrgList="methodUnitsList"
      ref="troubleshooting"
    ></expertOrgUnits>
    <!-- 共享项目弹窗 -->
    <projectDialog ref="project" @load="getProject"></projectDialog>
  </div>
</template>
<script>
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import projectDialog from './projectDialog'
import expertOrgUnits from './expertOrgUnits.vue'
import utils from '@/utils.js'
export default {
  name: 'add',
  components: {Breadcrumb, expertOrgUnits, projectDialog},

  data() {
    return {
      breadcrumbOpt: [{breadcrumbName: '数据共享'}, {breadcrumbName: '新增'}],
      saveing: false,
      loading: false,
      dialogVisible: false,
      methodUnitsList: [],
      multipleSelection: [],
      deptsProps: {
        value: 'alias',
        label: 'name',
        children: 'children',
        multiple: true,
        checkStrictly: true,
      },
      matCategoryList: [],
      menuList: [],
      options: [],
      searchForm: {
        releaseStatus: '2',
      },
      nodes: [],
      rules: {
        sharingOrgName: [
          {
            required: true,
            message: '请选择共享数据企业',
            trigger: 'blur',
          },
        ],
        sharingMenuAlias: [
          {
            required: true,
            message: '请选择共享数据菜单',
            trigger: 'blur',
          },
        ],
      },
      parameter: {
        sharingOrgName: '',
        sharingOrgId: '',
        sharingOrgCode: '',
        orgCode: '',
        orgId: '',
        orgName: '',
        sharingMenuAlias: '',
        detailList: [],
        noticeCode: '',
      },
      matData: [],
      cascaderCheck: false,
      detailList: [],
    }
  },
  created() {
    this.menuList = this.getTreeData(
      JSON.parse(sessionStorage.getItem('currentFrontMenus'))
    )
    this.addModifyBtn()
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.orgName = this.currentUser.org[0].orgName
    this.parameter.orgId = this.currentUser.org[0].orgId
    this.parameter.orgCode = this.currentUser.org[0].orgCode
    //   this.parameter.addUserName = this.currentUser.user.fullname
  },
  mounted() {},
  methods: {
    getTreeData(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          // 最后一级没有数据将children变成undefined
          delete data[i].children
        } else {
          // children不为空时继续调用该方法
          this.getTreeData(data[i].children)
        }
      }
      return data
    },
    //编辑
    addModifyBtn() {
      if (this.$route.query.editRows) {
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
        this.modifychoData(this.$route.query.editRows)
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    addDetail(data) {
      this.parameter.sharingOrgCode = data
        .map((item) => {
          return item.code
        })
        .join(',')
      this.parameter.sharingOrgName = data
        .map((item) => {
          return item.name
        })
        .join(',')
      this.parameter.sharingOrgId = data
        .map((item) => {
          return item.id
        })
        .join(',')
        this.methodUnitsList = data
      this.$nextTick(() => {
        this.$refs.formInfo.clearValidate('sharingOrgName')
      })
    },
    //获取修改回显数据
    modifychoData(val) {
      this.menuList = this.getTreeData(
        JSON.parse(sessionStorage.getItem('currentFrontMenus'))
      )
      this.loading = true
      this.$http
        .post('${portal}/dataSharing/v1/getDataById?id=' + val.id)
        .then((res) => {
          this.loading = false
          this.parameter = res.data.value
          this.multipleSelection = this.parameter.detailList
          this.parameter.noticeCode = this.parameter.detailList
            .map((item) => {
              return item.sharingNoticeCode
            })
            .join(',')
        })
    },
    changeAlias(val) {
      this.parameter.purchasingRulesDetailList = []
      val.forEach((item) => {
        this.parameter.purchasingRulesDetailList.push({
          sharingMenuAlias: item,
        })
      })
      this.$forceUpdate()
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'dataSharing',
        })
      } else {
        this.$router.push({
          name: 'dataSharing',
        })
      }
      this.parameter = {}
    },
    open() {
      this.$refs.troubleshooting.add()
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    openNotice() {
      this.dialogVisible = true
    },
    onClick() {
      // this.dialogVisible = true
      this.$refs.project.open()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getProject(data) {
      console.log(data)
      this.parameter.noticeCode = data
        .map((item) => {
          return item.projectNumber
        })
        .join(',')
      data.forEach((item) => {
        this.parameter.detailList.push({
          sharingNoticeCode: item.projectNumber,
          sharingNoticeId: item.id,
        })
      })
    },
    // 保存
    handleSave() {
      this.parameter.sharingMenuAlias = 'materialNotice,projectNotification'
      console.log(this.parameter)
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          this.$http
            .post('${portal}/dataSharing/v1/saveOrUpdateEntity', this.parameter)
            .then((res) => {
              if (res.data.state) {
                this.$message.success(res.data.message)
                this.handleClose()
              }
              this.saveing = false
            })
        }
      })
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
  },
}
</script>
      
      <style lang="scss" scoped>
// /deep/.el-row {
//   padding: 0 100px;
// }
.rows {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebebeb;
  margin-bottom: 16px;
  padding: 10px 0;
  .rows-left {
    margin-right: 10px;
    .rows-hr {
      width: 3px;
      height: 18px;
      background-color: #0e6eff;
      border-radius: 2px;
    }
  }
  .rows-title {
    .text-overflow {
      width: 100%;
      font-size: 16px;
    }
  }
}
</style>
      