<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="12">
            <el-form-item label="规则名称" prop="name">
              <el-input
                clearable
                v-model="parameter.name"
                placeholder="请输入规则名称"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input
                clearable
                readonly
                v-model="parameter.enterpriseName"
                placeholder="请选择企业名称"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="添加人姓名" prop="addUserName">
              <el-input
                clearable
                v-model="parameter.addUserName"
                placeholder="请输入企业名称"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购分类" prop="matCategoryCode">
              <el-cascader
                ref="cascader"
                @change="cascaderChange"
                :options="matData"
                @remove-tag="removeTag"
                v-model="parameter.matCategoryCode"
                :props="{
                  value: 'code',
                  label: 'name',
                  checkStrictly: true,
                  emitPath: false,
                  multiple: true,
                }"
                clearable
                style="width: 100%"
              ></el-cascader>
              <!-- <el-cascader
                ref="cascader"
                @change="cascaderChange"
                :options="matData"
                :props="{
                  value: 'code',
                  label: 'name',
                  checkStrictly: true,
                  emitPath: false,
                
                }"
                clearable
                style="width:100%"
              ></el-cascader> -->
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12">
            <el-form-item label="分级" prop="level">
              <el-input v-model="parameter.level" placeholder="请输入分级" clearable></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="资质类别" prop="list">
              <el-select
                multiple
                style="width: 100%"
                v-model="parameter.list"
                @change="changeQualificat"
                placeholder="请选择资质类别"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <!-- <el-col :span="12">
            <el-form-item label="是否必传" prop="necessaryStatus">
              <dict-select
                dictCode="sfsczz"
                :hasDefaultOption="true"
                v-model="parameter.necessaryStatus"
                style="width: 100%"
                clearable
                placeholder="请选择"
              />
            </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,
      },
      matCategoryList: [],
      options: [],
      nodes: [],
      rules: {
        enterpriseName: [
          {
            required: true,
            message: '请输入企业名称',
            trigger: 'blur',
          },
        ],
        addUserName: [
          {
            required: true,
            message: '请输入添加人姓名',
            trigger: 'blur',
          },
        ],
        matCategoryCode: [
          {
            required: true,
            message: '请选择采购分类',
            trigger: 'blur',
          },
        ],
        addTime: [
          {
            required: true,
            message: '请选择时间',
            trigger: 'blur',
          },
        ],
        list: [
          {
            required: true,
            message: '请选择资质类别',
            trigger: 'blur',
          },
        ],
        matCategoryName: [
          {
            required: true,
            message: '请选择分类',
            trigger: 'blur',
          },
        ],
        qualificationCategoryName: [
          {
            required: true,
            message: '请选择资质类别',
            trigger: 'blur',
          },
        ],
        name: [
          {
            required: true,
            message: '请输入规则名称',
            trigger: 'blur',
          },
        ],
      },
      parameter: {
        enterpriseName: '',
        enterpriseId: '',
        enterpriseCode: '',
        addTime: '',
        matCategoryCode: '',
        matCategoryName: '',
        configQualificationList: [],
      },
      matData: [],
      cascaderCheck: false,
    }
  },
  created() {
    this.getSupplierType()
    this.getQualification()
    this.addModifyBtn()
    this.getMatClass()
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.enterpriseName = this.currentUser.org[0].orgName
    this.parameter.enterpriseId = this.currentUser.org[0].orgId
    this.parameter.enterpriseCode = this.currentUser.org[0].orgCode
    this.parameter.addUserName = this.currentUser.user.fullname
    let moment = require('moment')
    this.parameter.addTime = moment().format('YYYY-MM-DD HH:mm:ss')
  },
  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
    },
    // 获取企业物料分类
    getMatClass() {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
      }
      this.matData = []
      this.$http
        .post(
          '${portal}/bizMaterialCategory/v1/getEnterpriseCategoryTree',
          params
        )
        .then((res) => {
          if (res.data.state) {
            let data = this.getTreeData(res.data.value)
            if (this.currentUser.org[0].orgCode == '00001513') {
              data.forEach((item) => {
                if (
                  item.code == '01' ||
                  item.code == '02' ||
                  item.code == '03' ||
                  item.code == '04' ||
                  item.code == '07'
                ) {
                  this.matData.push(item)
                }
              })
            } else {
              data.forEach((item) => {
                if (
                  item.code != '01' &&
                  item.code != '02' &&
                  item.code != '03' &&
                  item.code != '04' &&
                  item.code != '07'
                ) {
                  this.matData.push(item)
                }
              })
            }
          }
        })
    },
    getSupplierType() {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [],
      }
      this.$http
        .post('${portal}/bizSupplierType/v1/page', params)
        .then((res) => {
          if (res.data.state) {
            this.matCategoryList = res.data.value.rows
          }
        })
    },
    cascaderChange(val) {
      this.cascaderCheck = true
      this.nodes = this.$refs['cascader'].$refs.panel.getCheckedNodes()
      console.log(this.nodes, 111111)
      this.parameter.matCategoryCode = val.toString()
      this.$forceUpdate()
    },
    removeTag(val) {
      this.$nextTick(() => {
        let data = this.$refs['cascader'].$refs.panel.getCheckedNodes(false)
        this.nodes = data
      })
    },
    getQualification() {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
      }
      this.$http
        .post('${portal}/bizQualificationCategory/v1/page', params)
        .then((res) => {
          if (res.data.state) {
            let data = res.data.value.rows
            this.options = data
          }
        })
    },
    changeQualificat(item) {
      this.parameter.configQualificationList = []
      item.forEach((val) => {
        this.parameter.configQualificationList.push({
          qualificationCategoryId: val,
        })
      })
      this.$forceUpdate()
      console.log(this.parameter.configQualificationList)
    },
    changeMat(item) {
      this.parameter.matCategoryId = item.id
      this.parameter.matCategoryCode = item.code
      this.parameter.matCategoryName = item.name
    },
    openOrg() {
      this.$refs.troubleshooting.add()
    },
    addDetail(list) {
      this.parameter.enterpriseName = list[0].name
      this.parameter.enterpriseId = list[0].id
      this.parameter.enterpriseCode = 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.$http
        .post('${portal}/admissionConfig/v1/getDetailById?id=' + val.id)
        .then((res) => {
          this.loading = false
          this.parameter = res.data.value
          this.parameter.list = this.parameter.qualificationCategoryList.map(
            (item) => {
              return item.id
            }
          )
          console.log(this.parameter.list);
          this.parameter.matCategoryCode =
            res.data.value.matCategoryCode.split(',')
        })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'accessRule',
        })
      } else {
        this.$router.push({
          name: 'accessRule',
        })
      }
      this.parameter = {}
    },
    // 保存
    handleSave() {
      if (this.cascaderCheck == true) {
        this.parameter.matCategoryName = this.nodes
          .map((item) => {
            return item.data.name
          })
          .join(',')
        this.parameter.matCategoryId = this.nodes
          .map((item) => {
            return item.data.id
          })
          .join(',')
      }
      if(this.breadcrumbOpt[1].breadcrumbName == '编辑'){
        this.parameter.configQualificationList = []
      this.parameter.list.forEach(item=>{
        this.parameter.configQualificationList.push({
          qualificationCategoryId: item,
        })
      })
      }
      
      this.parameter.matCategoryCode = this.parameter.matCategoryCode.toString()
      console.log(this.parameter)
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          this.$http
            .post(
              '${portal}/admissionConfig/v1/saveOrUpdateEntity',
              this.parameter
            )
            .then((res) => {
              if (res.data.state) {
                this.$message.success(res.data.message)
                this.handleClose()
              }
              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>
    