<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-bill"
        @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">
              <el-form-item label="供应商名称" prop="supplierName">
                <el-input
                  :disabled="this.breadcrumbOpt[1].breadcrumbName === '编辑'"
                  v-model="parameter.supplierName"
                  placeholder="请选择供应商"
                >
                  <el-button
                    slot="append"
                    v-if="this.breadcrumbOpt[1].breadcrumbName != '编辑'"
                    icon="el-icon-search"
                    @click="handleInput"
                  ></el-button>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="评分时间" prop="scoreTime">
                <el-date-picker
                  v-model="parameter.scoreTime"
                  type="datetime"
                  value-format="yyyy-MM-dd hh:mm:ss"
                  placeholder="请选择评分时间"
                  style="width: 100%"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="供应商分类" prop="categoryName">
                <dict-select
                  dictCode="gysfl"
                  :hasDefaultOption="true"
                  v-model="parameter.categoryName"
                  style="width: 100%"
                  clearable
                  disabled
                  placeholder="请选择供应商分类"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="总得分" prop="score">
                <el-input-number
                  style="width: 100%;"
                  v-model="parameter.score"
                  placeholder="由打分自动计算"
                  disabled
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="供应商分级" prop="level">
                <dict-select
                  dictCode="gysfj"
                  :hasDefaultOption="true"
                  v-model="parameter.level"
                  style="width: 100%"
                  clearable
                  placeholder="请选择供应商分级"
                />
              </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>
        <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
                label-width="0px"
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationDimension`"
                :rules="[{required: true, message: '请输入'}]"
              >
                <el-select
                  v-model="scope.row.evaluationDimension"
                  placeholder="请选择评分方"
                  disabled
                >
                  <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
                label-width="0px"
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationItem`"
                :rules="[{required: true, message: '请输入评分项目'}]"
              >
                <el-input
                  disabled
                  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="evaluationStandard"
          >
            <template slot-scope="scope">
              <el-form-item
                label-width="0px"
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationStandard`"
                :rules="[{required: true, message: '请输入评分标准'}]"
              >
                <el-input
                disabled
                  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
                label-width="0px"
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationType`"
                :rules="[{required: true, message: '请选择评分类型'}]"
              >
                <dict-select
                  dictCode="pflx"
                  :hasDefaultOption="true"
                  v-model.number="scope.row.evaluationType"
                  style="width: 100%"
                  disabled
                  placeholder="请选择评分类型"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="分值"
            min-width="170px"
            prop="evaluationScore"
          >
            <template slot-scope="scope">
              <el-form-item
                label-width="0px"
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.evaluationScore`"
                :rules="[{required: true, message: '请输入分值'}]"
              >
                <el-input-number
                  v-model="scope.row.evaluationScore"
                  controls-position="right"
                  :min="1"
                  disabled
                  :precision="0"
                  :max="100"
                  step-strictly
                ></el-input-number>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="评分"
            min-width="170px"
            prop="score"
          >
            <template slot-scope="scope">
              <el-form-item
                label-width="0px"
                :prop="`bizSupplierTypeEvaluationLists.${scope.$index}.score`"
                :rules="[{required: true, message: '请输入评分'}]"
              >
                <el-input-number
                  v-model="scope.row.score"
                  controls-position="right"
                  :min="1"
                  @change="changeScore"
                  :max="10"
                  step-strictly
                ></el-input-number>
              </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>
      </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>
    <supplierUser
      @addDetail="addDetail"
      :useOrgList="supplierList"
      ref="supplier"
    ></supplierUser>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import supplierManger from '@/api/supplierManger.js'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import supplierUser from './supplierUser.vue'
export default {
  name: 'evaluationAddDialog',
  components: {
    Breadcrumb,
    supplierUser,
  },
  props: {
    editData: Object,
  },
  data() {
    return {
      saveing: false,
      breadcrumbOpt: [{breadcrumbName: '企业评价'}, {breadcrumbName: '新增'}],

      rules: {
        evaluationItem: [
          {required: true, message: '请输入评分项目', trigger: 'blur'},
        ],
        evaluationScore: [
          {required: true, message: '请输入分值', trigger: 'blur'},
        ],
        evaluationStandard: [
          {required: true, message: '请输入评分标准', trigger: 'blur'},
        ],
        evaluationType: [
          {required: true, message: '请选择评分类型', trigger: 'blur'},
        ],
        score: [{required: true, message: '请输入评分', trigger: 'blur'}],
        scoreOrgName: [
          {required: true, message: '请选择评分企业名称', trigger: 'blur'},
        ],
        categoryName:[
        {required: true, message: '请选择供应商名称', trigger: 'blur'}
        ],
        supplierName:[
        {required: true, message: '请选择供应商名称', trigger: 'blur'}
        ],
        scoreTime:[
        {required: true, message: '请选择评分时间', trigger: 'blur'}
        ],
        level:[
        {required: true, message: '请选择供应商分级', trigger: 'blur'}
        ],
      },
      dataList: [],
      parameter: {
        bizSupplierTypeEvaluationLists: [],
        evaluationDimension:1,
        supplierCode:'',
        supplierId:'',
        supplierName:'',
        admittedSupplierId:'',
        scoreOrgId:'',
        scoreOrgCode:'',
        scoreOrgName:'',
        scoreUserId:'',
        scoreUserAccount:'',
        scoreUserName:'',
        score:null,
        level:1,
        scoreTime:'',
        categoryName:'',
        categoryCode:'',
      },
      loading: false,
      scoreOrgList: [], //评分企业
      supplierList: [], //供应商
    }
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
  },
  created() {
    this.addModifyBtn()
    let moment = require('moment')
    this.parameter.scoreTime = moment().format('YYYY-MM-DD HH:mm:ss')
    let currentUser = JSON.parse(sessionStorage.getItem('currentUser'))
    this.parameter.scoreUserId = currentUser.userId
    this.parameter.scoreUserName = currentUser.username
    this.parameter.scoreUserAccount = currentUser.account
  },
  methods: {
    handleInput() {
      this.$refs['supplier'].add()
    },
    addDetail(list) {
      this.parameter.scoreOrgId = list[0].enterpriseId
      this.parameter.scoreOrgName = list[0].enterpriseName
      this.parameter.scoreOrgCode = list[0].enterpriseCode
      this.parameter.supplierCode = list[0].supplierCode
      this.parameter.supplierId = list[0].supplierId
      this.parameter.supplierName = list[0].supplierName
      this.parameter.admittedSupplierId = list[0].id
      this.parameter.categoryName = list[0].categoryName
      this.parameter.categoryCode = list[0].categoryCode
      let data = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'supplierTypeId',
            relation: 'AND',
            value: list[0].supplierTypeId,
          },
        ],
      }
      this.$http
        .post('${portal}/supplier/bizSupplierTypeEvaluation/v1/query', data)
        .then((res) => {
          this.parameter.bizSupplierTypeEvaluationLists = []
          this.parameter.bizSupplierTypeEvaluationLists = res.data.rows
        })
    },
    getByIdData(id) {
      this.loading = true
      this.parameter.bizSupplierTypeEvaluationLists =[]
      supplierManger.getByIdEvaluationlist(id).then((res) => {
        this.loading = false
        this.parameter = res.data
        this.parameter.bizSupplierTypeEvaluationLists = this.parameter.detailList
      })
    },
    //编辑
    addModifyBtn() {
      let {id} = this.$route.params
      if (id) {
        this.getByIdData(id)
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    changeScore(val){
      let sum = 0 
      this.parameter.bizSupplierTypeEvaluationLists.forEach(item=>{
        sum += item.score
        this.parameter.score = sum
      })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'enterpriseEvaluation',
          params: {
            pageBean: this.$route.query.pageBean,
          },
        })
      } else {
        this.$router.push({
          name: 'enterpriseEvaluation',
        })
      }

      // this.$emit('closeComponent')
      this.parameter = {}
      // this.$refs.formInfo.clearValidate()
    },
    // 保存
    handleSave() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          let params = this.parameter
          params.detailList = this.parameter.bizSupplierTypeEvaluationLists
          params.detailList.forEach(item=>{
            if(item.id){
              delete item.id
            }
          })
          supplierManger.saveEvaluationlist(params).then((res) => {
            this.saveing = false
            this.loading = false
            if (res.data.state) {
              this.$confirm('保存成功，是否返回列表页?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'success',
              }).then(() => {
                this.$router.push({
                  name: 'enterpriseEvaluation',
                })
              })
            }
          })
        }
      })
    },
  },
}
</script>

<style lang="scss"
  scoped>
// .row-box {
//   padding-left: 25%;
// }

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

.input-select-item {
  .el-form-item__content {
    position: relative;

    .input-select-val {
      height: 100%;
      position: relative;
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      z-index: 1;
      padding: 2px 30px 2px 9px;
      margin-right: 56px;
      min-height: 40px;

      ::v-deep .el-tag--info {
        margin: 2px 0 2px 6px;
        display: flex;
        max-width: 100%;
        align-items: center;

        .el-tag__close {
          background-color: #c0c4cc;
        }
      }
    }

    .el-input {
      position: absolute;
      height: 100%;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;

      ::v-deep .el-input__inner {
        height: 100%;
      }
    }
  }
}
</style>
