<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="24">
            <el-form-item label="企业名称" prop="procureOrgName">
              <el-input
                clearable
                readonly
                v-model="parameter.procureOrgName"
                placeholder="请选择企业名称"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="审批规则" prop="approvalRules">
              <el-select
                style="width: 100%"
                v-model="parameter.approvalRules"
                placeholder="请选择审批规则"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="菜单设置" prop="approvalMenuAlias">
              <el-cascader
                :options="menuList"
                v-model="parameter.approvalMenuAlias"
                @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>
    <expertPoolUnits
      @addDetail="addDetail"
      :useOrgList="methodUnitsList"
      ref="troubleshooting"
    />
  </div>
</template>
  
  <script>
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import procurementManagement from '@/api/procurementManagement'
import expertPoolUnits from '../../expertManagement/component/expertPoolUnits.vue'
export default {
  name: 'add',
  components: {Breadcrumb, expertPoolUnits},
  props: {
    editData: Object,
  },
  data() {
    return {
      breadcrumbOpt: [{breadcrumbName: '审核设置'}, {breadcrumbName: '新增'}],
      saveing: false,
      loading: false,
      methodUnitsList: [],
      deptsProps: {
        value: 'alias',
        label: 'name',
        children: 'children',
        multiple: true,
        checkStrictly: true,
      },
      options: [
        {value: 1, label: '流程审批'},
        {value: 2, label: '跳过审批'},
      ],
      rules: {
        procureOrgName: [
          {
            required: true,
            message: '请选择企业名称',
            trigger: 'blur',
          },
        ],
      },
      menuList: [],
      parameter: {
        procureOrgName: '',
        approvalRules: 2,
        procureOrgId: '',
        procureOrgCode: '',
        purchasingRulesDetailList: [],
      },
    }
  },
  created() {
    // this.$http
    //   .get('${portal}/sys/sysMenu/v1/getCurrentUserMenu?menuAlias=front_menu')
    //   .then((res) => {
    //     this.menuList = this.getTreeData(res.data.value)
    //   })
    this.menuList = this.getTreeData(
      JSON.parse(sessionStorage.getItem('currentFrontMenus'))
    )
    this.addModifyBtn()
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.procureOrgName = this.currentUser.org[0].orgName
    this.parameter.procureOrgId = this.currentUser.org[0].orgId
    this.parameter.procureOrgCode = this.currentUser.org[0].orgCode
  },
  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
    },
    openOrg() {
      this.$refs.troubleshooting.add()
    },
    addDetail(list) {
      this.parameter.procureOrgName = list[0].name
      this.parameter.procureOrgId = list[0].id
      this.parameter.procureOrgCode = list[0].code
    },
    //编辑
    addModifyBtn() {
      if (this.$route.query.editRows) {
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
        this.modifychoData(this.$route.query.editRows)
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    //获取修改回显数据
    modifychoData(val) {
      this.loading = true
      this.menuList = this.getTreeData(
        JSON.parse(sessionStorage.getItem('currentFrontMenus'))
      )
      this.$http
        .post(
          '${portal}/purchasingApplicationRules/v1/getDataById?id=' + val.id
        )
        .then((res) => {
          this.loading = false
          this.parameter = res.data.value
          this.parameter.approvalMenuAlias =
            this.parameter.purchasingRulesDetailList.map((item) => {
              return item.approvalMenuAlias
            })
        })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'auditSettings',
        })
      } else {
        this.$router.push({
          name: 'auditSettings',
        })
      }
      this.parameter = {}
    },
    changeAlias(val) {
      this.parameter.purchasingRulesDetailList = []
      val.forEach(item=>{
        this.parameter.purchasingRulesDetailList.push({
          approvalMenuAlias :item
        })
      })
      this.$forceUpdate()
    },
    // 保存
    handleSave() {
      console.log(this.parameter, 'this.parameter')
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          this.$http
            .post(
              '${portal}/purchasingApplicationRules/v1/saveOrUpdate',
              this.parameter
            )
            .then((res) => {
              if (res.data.state) {
                this.$message.success(res.data.message)
                if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
                  this.$router.push({
                    name: 'auditSettings',
                  })
                } else {
                  this.$router.push({
                    name: 'auditSettings',
                  })
                }
              }
              this.saveing = false
            })
        }
      })
    },
  },
}
</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>
  