<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-model"
        @breadcrumb-click="handleClose"
      />
    </div>
    <div class="form-content">
      <el-form
        ref="formInfo"
        :model="parameter"
        label-width="auto"
        label-position="right"
        :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="是否是第一级">
              <el-select
                style="width:100%"
                v-model="judgmentValue"
                placeholder="请选择"
              >
                <el-option label="是" value="1"> </el-option>
                <el-option label="否" value="0"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-row :gutter="24" v-if="judgmentValue == '0'">
          <el-col :span="12">
            <el-form-item label="上级分类" prop="parentCode">
              <el-cascader
                ref="cascader"
                @change="cascaderChange"
                v-model="parameter.parentCode"
                :options="options"
                :props="{
                  value: 'code',
                  label: 'name',
                  checkStrictly: true,
                  emitPath: false
                }"
                disabled
                clearable
                style="width:100%"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="分类名称" prop="name">
              <el-input
                disabled
                v-model="parameter.name"
                placeholder="请输入分类名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="分类编号" prop="code">
              <el-input
                disabled
                v-model="parameter.code"
                placeholder="请输入分类编号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="渝企采分类名称" prop="yqcCategory">
              <el-select @change="changeYqc" style="width: 100%;" filterable v-model="parameter.yqcCategory" placeholder="请选择渝企采分类名称">
                  <el-option
                      v-for="item in yqcList"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="渝企采分类编码" prop="yqcCategoryCode">
              <el-input
                disabled
                v-model="parameter.yqcCategoryCode"
                placeholder="渝企采分类编码"
              ></el-input>
            </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>
  </div>
</template>

<script>
import basicData from '@/api/basicData'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
export default {
  name: 'domesticDialog',
  components: {Breadcrumb},
  props: {
    editData: Object
  },
  data() {
    return {
      breadcrumbOpt: [{breadcrumbName: '物料分类'}, {breadcrumbName: '新增'}],
      judgmentValue: '1',
      options: [],
      yqcList:[],
      saveing:false,
      rules: {
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ],
        judgmentValue: [
          {
            required: true,
            message: '请选择是否是第一级',
            trigger: 'blur'
          }
        ],
        parentCode: [
          {
            required: true,
            message: '请选择上级分类',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入分类编号',
            trigger: 'blur'
          }
        ]
      },
      parameter: {
        name: '',
        code: '',
        parentCode: '',
        is_dele_: 0,
        yqcCategoryName:'',
        yqcCategoryCode:'',
        parentId: '',
        status:'1'
      }
    }
  },
  created() {
    this.selectTree()
    this.addModifyBtn()
    this.getYqc()
  },
  mounted() {},
  methods: {
    cascaderChange(val) {
      this.parameter.parentCode = this.$refs[
        'cascader'
      ].getCheckedNodes()[0].data.code
      this.parameter.parentId = this.$refs[
        'cascader'
      ].getCheckedNodes()[0].data.id
    },
    //查询物料分类树结构
    selectTree() {
      basicData.bizMaterialCategorySelectTree().then(res => {
        this.options = res.value
      })
    },
    // 获取渝企采分类
    getYqc(){
      let data = {
        pageBean:{
          page:-1,
          pageSize:-1
        }
      }
      this.$http.post('${portal}/yqcGovernmentCategory/v1/page',data).then(res=>{
        if(res.data.message){
          this.yqcList = res.data.value.rows
        }
      })
    },
    //编辑
    addModifyBtn() {
      console.log(this.$route.query.editRows)
      if (this.$route.query.editRows) {
        if (this.$route.query.editRows.parentId === '0') {
          this.judgmentValue = '1'  
        } else {
          this.judgmentValue = '0'
        }
        this.parameter.parentCode = this.$route.query.editRows.code
        this.parameter.parentId = this.$route.query.editRows.id
        this.parameter.parentName = this.$route.query.editRows.name
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
        this.modifychoData(this.$route.query.editRows)
      }else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    //获取修改回显数据
    modifychoData(val) {
      console.log(val,'val');
      this.parameter = val
      this.parameter.yqcCategory = val.yqcCategoryCode
    },
    // 关闭//面包屑点击
    handleClose() {
      if (
        this.breadcrumbOpt[1].breadcrumbName === '编辑' ||
        this.breadcrumbOpt[1].breadcrumbName === '添加下级'
      ) {
        this.$router.push({
          name: 'materialManagement',
          params: {
            tabsActiveName: 'materialClassification',
            pageBean: this.$route.query.pageBean
          }
        })
      } else {
        this.$router.push({
          name: 'materialManagement',
          params: {
            tabsActiveName: 'materialClassification'
          }
        })
      }

      this.parameter = {}
    },
    changeYqc(val){
      this.parameter.yqcCategoryCode = val
      this.yqcList.forEach(item=>{
        if(val == item.code){
          this.parameter.yqcCategoryName = item.name
        }
      })
      this.$forceUpdate()
    },
    // 保存
    handleSave() {
      if (this.judgmentValue === '1') {
        this.parameter.parentCode = 1
        this.parameter.parentId = 1
      }
      // this.parameter.parentCode = this.parameter.parentCode.join(',')
      this.$refs.formInfo.validate(valid => {
        if (valid) {
          this.saveing = true
          basicData
            .bizMaterialCategorySaveCategory(this.parameter)
            .then(res => {
              this.saveing = false
              if (res.state) {
                this.$message.success(res.message)
                if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
                  this.$router.push({
                    name: 'materialManagement',
                    params: {
                      tabsActiveName: 'materialClassification',
                      pageBean: this.$route.query.pageBean
                    }
                  })
                } else {
                  this.$router.push({
                    name: 'materialManagement',
                    params: {
                      tabsActiveName: 'materialClassification'
                    }
                  })
                }
              }
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/.el-row {
  display: flex;
  justify-content: center;
}
.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>
