<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="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>
        <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.descriptionList"
          style="width: 100%"
          :span-method="objectSpanMethod"
          :cell-style="{'text-align': 'center'}"
          :header-cell-style="{'text-align': 'center'}"
        >
          <el-table-column
            label="评分大项"
            min-width="150"
            prop="evaluationItemPname"
          >
          </el-table-column>
          <el-table-column
            label="评分细项"
            min-width="150"
            prop="evaluationItemName"
          >
          </el-table-column>
          <el-table-column
            label="评分标准"
            min-width="170"
            prop="evaluationStandard"
          >
            <template slot-scope="scope">
              <el-input
                clearable
                type="textarea"
                v-model="scope.row.evaluationStandard"
                placeholder="请输入评分标准"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="分值" min-width="170" prop="evaluationScore">
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.evaluationScore"
                controls-position="right"
                :min="1"
                :precision="0"
                :max="100"
              ></el-input-number>
            </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="loading" @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 {
      treeData: [],
      loading: false,
      contentScoreData: [],
      centralizedData: [
        {name: '是', id: 1},
        {name: '否', id: 0},
      ],
      breadcrumbOpt: [{breadcrumbName: '综合评标法'}, {breadcrumbName: '新增'}],
      rules: {
        preparedOrgName: [
          {
            required: true,
            message: '请输入编制单位',
            trigger: 'blur',
          },
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur',
          },
        ],
      },
      useOrgListValue: '',
      methodUnitsList: [],
      parameter: {
        evaluatingMethods: 1,
        useOrgList: [],
        preparedOrgId: '',
        remarks: '',
        descriptionList: [],
      },
    }
  },
  created() {
    this.selectTree()
    this.addModifyBtn()
    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: {
    //使用单位
    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(',')
    },
    //评分大项选择
    factorChangeContent(val, index, list) {
      list.forEach((item) => {
        if (item.subjectCode === val) {
          this.parameter.descriptionList[index].evaluationItemId = item.id
        }
      })
    },
    //评分大项选择
    factorChange(val, index) {
      this.treeData.forEach((item) => {
        if (item.subjectCode === val) {
          // this.contentScoreData = item.children
          this.parameter.descriptionList[index].contentScoreData = item.children
        }
      })
    },
    //查询物料分类树结构
    selectTree() {
      basicData.bizEvaluationItemSelectTree().then((res) => {
        this.treeData = res.value[0].children
        this.treeData.forEach((val) => {
          if (val.children && val.children.length) {
            val.children.forEach((e) => {
              if (val.id == e.pcode) {
                this.parameter.descriptionList.push({
                  evaluationItemName: e.subjectName,
                  evaluationItemPname: val.subjectName,
                  evaluationStandard: '',
                  evaluationScore: null,
                  evaluationItemPid: val.id,
                  evaluationItemCode: e.subjectCode,
                  evaluationItemId: e.id,
                })
              }
            })
          }
        })
      })
    },
    //编辑
    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) => {
        console.log(res, 'resss')
        this.parameter = res.value
        this.parameter.orgs.forEach((item) => {
          delete item.isParent
        })
        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}
        })
        this.treeData.forEach((val) => {
          val.children.forEach((e) => {
            this.parameter.descriptionList.forEach((i) => {
              if (i.evaluationItemId == e.id) {
                i.evaluationItemPname = i.scoringFactors
                i.evaluationItemName = i.scoringContent
                i.evaluationItemPid = val.id
              }
            })
          })
        })
        this.parameter.descriptionList.sort((a, b) => {
          if (a['evaluationItemPid'] != b['evaluationItemPid']) {
            return a['evaluationItemPid'].localeCompare(b['evaluationItemPid'])
          }
        })
      })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'evaluationSystem',
          params: {
            tabsActiveName: 'methodComprehensive',
            pageBean: this.$route.query.pageBean,
          },
        })
      } else {
        this.$router.push({
          name: 'evaluationSystem',
          params: {
            tabsActiveName: 'methodComprehensive',
          },
        })
      }
      this.parameter = {}
    },
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 0) {
        const rows = this.parameter.descriptionList.filter(
          (item) => item.evaluationItemPid === row.evaluationItemPid
        )
        if (
          rowIndex === 0 ||
          this.parameter.descriptionList[rowIndex - 1].evaluationItemPid !==
            row.evaluationItemPid
        ) {
          return {
            rowspan: rows.length,
            colspan: 1,
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          }
        }
      }
    },
    // 保存
    handleSave() {
      let sum = 0
      this.parameter.descriptionList.forEach((item) => {
        sum += item.evaluationScore
      })
      if (sum > 101) {
        this.$message.warning('评分表分值总数不能超过100')
        return
      }
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.loading = true
          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: 'methodComprehensive',
                    pageBean: this.$route.query.pageBean,
                  },
                })
              } else {
                this.$router.push({
                  name: 'evaluationSystem',
                  params: {
                    tabsActiveName: 'methodComprehensive',
                  },
                })
              }
            }
            this.loading = false
          })
        }
      })
    },
  },
}
</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>
