<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-apartment"
        @breadcrumb-click="handleClose"
      />
    </div>
    <div class="form-content">
      <el-form
        ref="formInfo"
        :model="parameter"
        label-width="auto"
        label-position="right"
        :rules="rules"
        v-loading="loading"
      >
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">供应商分类分级信息</div>
          </div>
        </div>
        <div class="row-box">
          <el-row :gutter="24">
            <el-col :span="12" v-if="breadcrumbOpt[1].breadcrumbName == '编辑'">
              <el-form-item label="分类编码" prop="code">
                <el-input
                  :readonly="breadcrumbOpt[1].breadcrumbName == '编辑'"
                  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 v-model="parameter.name" placeholder="请输入分类名称"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="分类级别" prop="level">
                <dict-select
                  v-if="breadcrumbOpt[1].breadcrumbName == '新增'"
                  dictCode="gysgl-fljb"
                  :hasDefaultOption="true"
                  v-model="parameter.level"
                  style="width: 100%"
                  clearable
                  readonly
                  placeholder="请输入分类级别"
                />
                <el-input
                  readonly
                  v-else
                  clearable
                  :value="getDictName('gysgl-fljb', parameter.level)"
                ></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item label="备注" prop="remarks">
                <el-input
                  :readonly="breadcrumbOpt[1].breadcrumbName == '编辑'"
                  clearable
                  type="textarea"
                  :autosize="{minRows: 2, maxRows: 4}"
                  v-model="parameter.remarks"
                  placeholder="请输入备注"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">评分表</div>
          </div>
        </div>
        <div>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="addTable"
            >添加</el-button
          >
        </div>
        <el-table
          ref="singleTable"
          :data="parameter.bizSupplierTypeEvaluationLists"
          style="width: 100%"
          highlight-current-row
          :cell-style="{'text-align': 'center'}"
          :header-cell-style="{'text-align': 'center'}"
        >
          <el-table-column
            label="评分方"
            min-width="150"
            prop="evaluationDimension"
          >
            <template slot-scope="scope">
              <el-form-item
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationDimension`"
                :rules="[{required: true, message: '请输入'}]"
              >
                <el-select
                  v-model="scope.row.evaluationDimension"
                  placeholder="请选择评分方"
                >
                  <el-option label="企业方" :value="1"> </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column
            label="评分项目"
            min-width="170"
            prop="evaluationItem"
          >
            <template slot-scope="scope">
              <el-form-item
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationItem`"
                :rules="[{required: true, message: '请输入评分项目'}]"
              >
                <el-input
                  clearable
                  type="textarea"
                  v-model="scope.row.evaluationItem"
                  placeholder="请输入评分项目"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="分值" min-width="170" prop="evaluationScore">
            <template slot-scope="scope">
              <el-form-item
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationScore`"
                :rules="[{required: true, message: '请输入分值'}]"
              >
                <el-input-number
                  v-model="scope.row.evaluationScore"
                  controls-position="right"
                  :min="1"
                  :max="100"
                  :precision="0"
                  step-strictly
                ></el-input-number>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="评分标准"
            min-width="170"
            prop="evaluationStandard"
          >
            <template slot-scope="scope">
              <el-form-item
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationStandard`"
                :rules="[{required: true, message: '请输入评分标准'}]"
              >
                <el-input
                  clearable
                  type="textarea"
                  v-model="scope.row.evaluationStandard"
                  placeholder="请输入评分标准"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="评分类型"
            min-width="170"
            prop="evaluationType"
          >
            <template slot-scope="scope">
              <el-form-item
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationType`"
                :rules="[{required: true, message: '请选择评分类型'}]"
              >
                <dict-select
                  dictCode="pflx"
                  :hasDefaultOption="true"
                  v-model.number="scope.row.evaluationType"
                  style="width: 100%"
                  clearable
                  placeholder="请选择评分类型"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="备注" min-width="170" prop="remarks">
            <template slot-scope="scope">
              <el-input
                clearable
                type="textarea"
                v-model="scope.row.remarks"
                placeholder="请输入备注"
              ></el-input>
            </template>
          </el-table-column>
          <!-- <el-table-column label="排序号" min-width="170" prop="sn">
                        <template slot-scope="scope">
                            <el-form-item
                                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.sn`"
                                :rules="[
                                    {required: true, message: '请输入排序号'},
                                ]"
                            >
                                <el-input-number
                                    v-model="scope.row.sn"
                                    controls-position="right"
                                    :min="1"
                                    :max="10"
                                    step-strictly
                                ></el-input-number>
                            </el-form-item>
                        </template>
                    </el-table-column> -->
          <el-table-column
            fixed="right"
            align="right"
            width="90px"
            :index="999"
          >
            <template slot-scope="scope">
              <el-button
                v-if="!scope.row.id"
                size="mini"
                type="danger"
                circle
                icon="el-icon-close"
                @click="delTable(scope.$index, scope.row)"
              />
            </template>
          </el-table-column>
        </el-table>
      </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 supplierManger from '@/api/supplierManger.js'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import utils from '@/utils.js'
export default {
  name: 'domesticDialog',
  components: {Breadcrumb},
  props: {
    editData: Object,
  },
  data() {
    return {
      breadcrumbOpt: [
        {breadcrumbName: '供应商分类分级'},
        {breadcrumbName: '新增'},
      ],
      saveing: false,
      rules: {
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur',
          },
        ],
      },
      parameter: {
        bizSupplierTypeEvaluationLists: [],
      },
      loading: false,
    }
  },
  created() {
    this.addModifyBtn()
  },
  methods: {
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    getByIdData(id) {
      let formData = new FormData()
      formData.append('id', id)
      supplierManger.getByIdClassification(formData).then((res) => {
        this.parameter = res.data.value || {}
      })
    },
    //编辑
    addModifyBtn() {
      let {id} = this.$route.params
      if (id) {
        this.getByIdData(id)
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'classificationSupplier',
          params: {
            pageBean: this.$route.query.pageBean,
          },
        })
      } else {
        this.$router.push({
          name: 'classificationSupplier',
        })
      }
      this.parameter = {}
    },
    //列表添加行
    addTable() {
      this.parameter.bizSupplierTypeEvaluationLists.push({
        evaluationDimension:1
      })
    },
    //列表删除行
    delTable(index, row) {
      this.parameter.bizSupplierTypeEvaluationLists.splice(index, 1)
      if (row.id) {
        this.parameter.deleteDetailsId.push(row.id)
      }
    },
    // 保存
    handleSave() {
      this.parameter.bizSupplierTypeEvaluationLists.forEach((item) => {
        delete item.isDele
        delete item.pkVal
        delete item.tenantId
      })
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          supplierManger.saveClassification(this.parameter).then((res) => {
            this.saveing = false
            if (res.data.state) {
              this.$confirm('保存成功，是否返回列表页?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'success',
              }).then(() => {
                this.$router.push({
                  name: 'classificationSupplier',
                })
              })
            } else {
              this.loading = false
              this.$message.error('保存失败！')
            }
          })
        }
      })
    },
  },
}
</script>

<style lang="scss"
  scoped>
.row-box {
  margin: 0 10%;
}

// /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;
    }
  }
}

::v-deep .el-table {
  .el-form-item:not(.is-error) {
    margin-bottom: 0;
  }

  .el-form-item__content {
    margin-left: 0 !important;

    .el-input-number,
    .el-select {
      width: 100%;
    }
  }
}
</style>
