<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"
      >
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title" style="display: flex; align-items: center">
            <div class="text-overflow">物料信息</div>
            <el-button
              @click="openDialog"
              type="primary"
              style="margin-left: 10px"
              size="small"
              icon="el-icon-plus"
              >添加物料</el-button
            >
          </div>
        </div>
        <div class="common-table-content btn-pagenation" ref="tableContent">
          <el-table
            ref="singleTable"
            class="table"
            :data="parameter.detailsList"
            style="width: 100%"
            row-key="id"
            default-expand-all
            highlight-current-row
            :cell-style="{'text-align': 'center'}"
            :header-cell-style="{'text-align': 'center'}"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column type="index" width="55" label="序号" />
            <el-table-column
              label="企业物料编号"
              min-width="120"
              show-overflow-tooltip
              prop="matCode"
            >
            </el-table-column>
            <el-table-column
              label="平台物料编号"
              min-width="120"
              show-overflow-tooltip
              prop="matPlatcode"
            >
            </el-table-column>
            <el-table-column
              label="物料名称"
              show-overflow-tooltip
              min-width="110"
              prop="matName"
            >
            </el-table-column>
            <el-table-column
              label="物料牌号"
              min-width="110"
              show-overflow-tooltip
              prop="matMaterial"
            ></el-table-column>
            <el-table-column
              label="规格型号"
              show-overflow-tooltip
              min-width="130"
              prop="matSpec"
            >
            </el-table-column>
            <el-table-column
              label="规格型号"
              show-overflow-tooltip
              min-width="130"
              prop="matSpec"
            >
            </el-table-column>
            <el-table-column
              label="计量单位"
              show-overflow-tooltip
              min-width="80"
              prop="matUnit"
            >
            </el-table-column>
            <el-table-column
              label="品牌/产地"
              show-overflow-tooltip
              min-width="100"
              prop="matBrand"
            >
            </el-table-column>
            <el-table-column
              label="基础扩展"
              show-overflow-tooltip
              min-width="140"
              prop="matBaseExt"
            >
            </el-table-column>
            <el-table-column
              label="其他扩展"
              show-overflow-tooltip
              min-width="140"
              prop="matOtherExt"
            >
            </el-table-column>
            <el-table-column label="税率" min-width="80" prop="matTaxrate">
            </el-table-column>
            <el-table-column
              fixed="right"
              align="right"
              label="操作"
              width="180px"
              :index="999"
            >
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click="addList(scope.$index, scope.row)"
                >
                  添加下级
                </el-button>

                <el-button
                  type="danger"
                  size="mini"
                  @click="delTable(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </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>
    <!-- 物料明细 -->
    <purchaseMaterial
      @addDetail="addDetail"
      :useOrgList="methodUnitsList"
      ref="troubleshooting"
    />
  </div>
</template>
  <script>
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import utils from '@/utils.js'
import purchaseMaterial from './purchaseMaterial'
export default {
  name: 'add',
  components: {Breadcrumb, purchaseMaterial},

  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: [],
      parameter: {
        orgCode: '',
        orgId: '',
        orgName: '',
        detailsList: [],
        noticeCode: '',
      },
      matData: [],
      cascaderCheck: false,
      detailList: [],
    }
  },
  created() {
    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
  },
  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 = '新增'
      }
    },
    //获取修改回显数据
    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
        })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'materialGradeManage',
        })
      } else {
        this.$router.push({
          name: 'materialGradeManage',
        })
      }
      this.parameter = {}
    },
    // 打开物料弹窗
    openDialog() {
      this.$refs.troubleshooting.add('0')
    },
    addList(index,row) {
      this.$refs.troubleshooting.add('0')
    },
    delTable(index,row){
        
    },
    addDetail(list) {
      if (this.parameter.detailsList.length == 0) {
        this.parameter.detailsList = list
        this.parameter.detailsList.forEach((item, index) => {
          item['matId'] = item.id
          this.$set(item,'children',[])
        })
      } else {
        this.parameter.detailsList.forEach((item) => {
          list.forEach((val) => {
            val['matId'] = val.id
            if (item.id != val.id) {
              item.children.push(val)
            }
          })
        })
      }
      this.$forceUpdate()
      console.log(this.parameter.detailsList)
      this.methodUnitsList = this.parameter.detailsList
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 保存
    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>
        