<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-utilize"
        @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="编号" prop="code">
              <el-input
                clearable
                v-model="parameter.code"
                placeholder="请输入编号"
              ></el-input>
            </el-form-item>
          </el-col> -->
          <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="useOrgList">
              <el-input
                readonly
                disabled
                v-model="useOrgListValue"
                placeholder="请选择使用单位"
              >
              <el-button slot="append" @click="purchasingUnitsBtn" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="编制单位" prop="preparedOrgName">
              <el-input
                disabled
                v-model="parameter.preparedOrgName"
                placeholder="请输入编制单位"
              ></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
        <!-- <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="评标方式" prop="clientName">
              <el-input
                clearable
                v-model="parameter.clientName"
                placeholder="请输入评标方式"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input
                clearable
                type="textarea"
                v-model="parameter.remarks"
                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" @click="handleSave">{{
        $t('domestic.save')
      }}</el-button>
    </div>
    <!-- 使用单位 -->
    <methodUnits
      @addDetail="addDetail"
      :useOrgList="methodUnitsList"
      ref="troubleshooting"
    />
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import methodUnits from './methodUnits.vue'
import basicData from '@/api/basicData'
export default {
  name: 'domesticDialog',
  components: {Breadcrumb, methodUnits},
  props: {
    editData: Object
  },
  data() {
    return {
      breadcrumbOpt: [
        {breadcrumbName: '最低价评标法'},
        {breadcrumbName: '新增'}
      ],
      options: [],
      rules: {
        preparedOrgName: [
          {
            required: true,
            message: '请输入编制单位',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
      },
      useOrgListValue: '',
      treeData:[],
      methodUnitsList: [],
      parameter: {
        evaluatingMethods: 2,
        useOrgList: [],
        preparedOrgId: '',
        description: '',
        remarks: '',
      }
    }
  },
  created() {
    this.addModifyBtn()
    this.selectTree()
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.preparedOrgId = this.currentUser.org[0].orgId
    this.parameter.preparedOrgName = this.currentUser.org[0].orgName
    this.parameter.preparedOrgCode = this.currentUser.org[0].orgCode
  },
  mounted() {},
  methods: {
    //查询物料分类树结构
    selectTree() {
      basicData.bizEvaluationItemSelectTree().then(res => {
        this.treeData = res.value[0].children
      })
    },
    //使用单位
    purchasingUnitsBtn() {
      this.$refs.troubleshooting.add()
    },
    addDetail(list) {
      this.methodUnitsList = list
      this.parameter.useOrgList = list.map(item => {
        return {orgId: item.id}
      })
      this.useOrgListValue = list
        .map(item => {
          return item.name
        })
        .join(',')
    },
    //编辑
    addModifyBtn() {
      if (this.$route.query.editRows) {
        console.log()
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
        this.modifychoData(this.$route.query.editRows)
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    //获取修改回显数据
    modifychoData(val) {
      basicData.bizEvaluationSystemId(val.id).then(res => {
        this.parameter = res.value
        this.methodUnitsList = res.value.orgs
        this.useOrgListValue = res.value.orgs
          .map(item => {
            return item.name
          })
          .join(',')
        this.parameter.useOrgList = res.value.orgs.map(item => {
          return {orgId: item.id}
        })
      })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'evaluationSystem',
          params: {
            tabsActiveName: 'lowestMethod',
            pageBean: this.$route.query.pageBean
          }
        })
      } else {
        this.$router.push({
          name: 'evaluationSystem',
          params: {
            tabsActiveName: 'lowestMethod'
          }
        })
      }
      this.parameter = {}
    },
    // 保存
    handleSave() {
      this.$refs.formInfo.validate(valid => {
        if (valid) {
          basicData.bizEvaluationSaveOrUpdate(this.parameter).then(res => {
            if (res.state) {
              this.$message.success(res.message)
              if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
                this.$router.push({
                  name: 'evaluationSystem',
                  params: {
                    tabsActiveName: 'lowestMethod',
                    pageBean: this.$route.query.pageBean
                  }
                })
              } else {
                this.$router.push({
                  name: 'evaluationSystem',
                  params: {
                    tabsActiveName: 'lowestMethod'
                  }
                })
              }
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.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>
