<!-- 绩效指标库 -->
<template>
  <div>
    <Row align="middle" type="flex">
      <Input
        :maxlength="20"
        @on-change="restPage"
        clearable
        placeholder="指标名称"
        style="width: 200px"
        v-model="valueIndexName"
      />
      <Button :loading="loading" @click="doInitSearch" class="mg-set" type="primary">查询</Button>
      <div class="table_data-control">
        <Button
          :disabled="loading"
          @click="InfoAdd"
          ghost
          icon="md-add"
          type="primary"
          v-permission:add
        >新增</Button>
        <Modal
          :mask-closable="false"
          :styles="{width: '800px'}"
          :title="modalTitle"
          class-name="modal-scroll-set"
          v-model="modalIndex"
        >
          <Scroll height="500">
            <Form :label-width="72" :model="formIndex" :rules="ruleValidate" ref="formIndex">
              <Row :gutter="8">
                <Col span="12">
                  <FormItem label="指标名称" prop="name">
                    <Input
                      :disabled="isDisabledName"
                      :maxlength="20"
                      placeholder="请输入"
                      v-model="formIndex.name"
                    ></Input>
                  </FormItem>
                </Col>
                <Col span="12">
                  <FormItem label="指标分类" prop="typeCode">
                    <Select
                      :disabled="isDisabled"
                      @on-change="handleTypeCode"
                      placeholder="请选择"
                      v-model="formIndex.typeCode"
                    >
                      <Option
                        :key="index + 1"
                        :value="index + 1"
                        v-for="(item,index) in indexTypeArr"
                      >{{ item }}</Option>
                    </Select>
                  </FormItem>
                </Col>
              </Row>
              <Row :gutter="8">
                <Col span="12">
                  <FormItem label="指标单位" prop="unitNmae">
                    <Input
                      :disabled="isDisabled"
                      :maxlength="20"
                      placeholder="请输入"
                      v-model="formIndex.unitNmae"
                    ></Input>
                  </FormItem>
                </Col>
                <Col span="12">
                  <FormItem label="指标归属" prop="belong">
                    <Select
                      :disabled="isDisabled"
                      @on-change="handleBelong"
                      placeholder="请选择"
                      v-model="formIndex.belong"
                    >
                      <Option
                        :key="index + 1"
                        :value="index + 1"
                        v-for="(item,index) in indexAttrArr"
                      >{{ item }}</Option>
                    </Select>
                  </FormItem>
                </Col>
              </Row>
              <Row :gutter="8">
                <Col span="12">
                  <FormItem label="小数点" prop="point">
                    <Select
                      :disabled="isDisabled"
                      @on-change="handlePoint"
                      placeholder="请选择"
                      v-model="formIndex.point"
                    >
                      <Option
                        :key="index + 1"
                        :value="index + 1"
                        v-for="(item,index) in radixPointArr"
                      >{{ item }}</Option>
                    </Select>
                  </FormItem>
                </Col>
                <Col span="12">
                  <FormItem class="long-label" label="是否用于计算" prop="iscalculate">
                    <RadioGroup @on-change="handleCalcul" v-model="formIndex.iscalculate">
                      <Radio :disabled="isDisabled" label="1">是</Radio>
                      <Radio :disabled="isDisabled" label="0">否</Radio>
                    </RadioGroup>
                  </FormItem>
                </Col>
              </Row>
              <Row :gutter="8">
                <Col span="12">
                  <FormItem label="所属中心" prop="centerSelected">
                    <Select
                      :disabled="isDisabled"
                      :label-in-value="true"
                      @on-change="handleCenter"
                      placeholder="请选择"
                      v-model="formIndex.centerSelected"
                    >
                      <Option
                        :key="index"
                        :label="item.wmName"
                        :value="item.wmSiteId"
                        v-for="(item,index) in centresArr"
                      >{{ item.wmName }}</Option>
                    </Select>
                  </FormItem>
                </Col>
                <Col span="12">
                  <FormItem label="数据类型" prop="dataTypeGruop">
                    <CheckboxGroup @on-change="handleDataType" v-model="formIndex.dataTypeGruop">
                      <Checkbox :disabled="isDisabled" label="1">日</Checkbox>
                      <Checkbox :disabled="isDisabled" label="2">周</Checkbox>
                      <Checkbox :disabled="isDisabled" label="3">月</Checkbox>
                    </CheckboxGroup>
                  </FormItem>
                </Col>
              </Row>
              <Row>
                <Col span="24">
                  <FormItem label="所属专线" prop="specialLine">
                    <Select
                      :disabled="isDisabled"
                      @on-change="handleSpecialLine"
                      multiple
                      placeholder="请选择"
                      v-model="formIndex.specialLine"
                    >
                      <Option
                        :key="index"
                        :value="item.wmSiteId+'#'+item.wmActivityId"
                        v-for="(item,index) in centerList"
                      >{{ item.wmName }}</Option>
                    </Select>
                  </FormItem>
                </Col>
              </Row>
              <Row>
                <Col span="24">
                  <FormItem label="备注" prop="remark">
                    <Input
                      :autosize="{minRows: 2,maxRows: 5}"
                      :disabled="isDisabled"
                      :maxlength="200"
                      clearable
                      placeholder="请输入"
                      type="textarea"
                      v-model="formIndex.remark"
                    ></Input>
                  </FormItem>
                </Col>
              </Row>
              <Row>
                <Col span="24">
                  <RadioGroup @on-change="setIndexWay" type="button" v-model="selectWay">
                    <Radio :disabled="isDisabledWay" label="4">导入/直接取值</Radio>
                    <Radio :disabled="isDisabledWay" label="2">使用打分标准</Radio>
                    <Radio :disabled="isDisabledWay" label="3">设置公式</Radio>
                  </RadioGroup>
                  <div class="mt-set8" v-if="setWay == 'useStd'">
                    <Row>
                      <Col span="8">
                        <Select
                          :disabled="isDisabled"
                          :label="factorRes"
                          :loading="searchTagetLoading"
                          :remote-method="handleSearchFactor"
                          @on-change="handleSelectFactor"
                          clearable
                          filterable
                          placeholder="请输入因子名称"
                          ref="factorSelect"
                          remote
                          style="width: 200px;"
                          v-model="factor"
                        >
                          <Option
                            :key="item.uuid"
                            :label="item.factorName"
                            :value="item.uuid"
                            v-for="item in factorTargets"
                          >{{item.factorName}}</Option>
                        </Select>
                      </Col>
                    </Row>
                    <br />
                    <Row align="top" class-name="dynamic_panle" type="flex" v-if="showGrade">
                      <Col span="1">
                        <Button
                          @click="handleAdd"
                          class="add-item"
                          icon="md-add"
                          shape="circle"
                          size="small"
                          type="primary"
                          v-if="showAddItem"
                        ></Button>
                      </Col>
                      <Col class-name="dynamic_item-set" span="23">
                        <Row
                          :gutter="8"
                          :key="index"
                          :rules="ruleDynamic"
                          align="top"
                          class="row_style-set row_split-set22"
                          type="flex"
                          v-for="(item, index) in formIndex.scorelist"
                        >
                          <Col span="8">
                            <FormItem
                              :prop="'scorelist.'+index+'.score'"
                              :rules="ruleDynamic.score"
                              label="分值"
                            >
                              <Input
                                :disabled="isDisabled"
                                :maxlength="10"
                                placeholder="请输入"
                                style="width:100%"
                                v-model="item.score"
                              ></Input>
                            </FormItem>
                          </Col>
                          <Col span="14">
                            <FormItem label="区间">
                              <Row>
                                <Col span="11">
                                  <FormItem
                                    :prop="'scorelist.'+index+'.lowValue'"
                                    :rules="ruleDynamic.lowValue"
                                  >
                                    <Input
                                      :disabled="isDisabled"
                                      :maxlength="10"
                                      :ref="'lowValue'+index"
                                      @on-blur="handleTestValue(index)"
                                      number
                                      placeholder="请输入"
                                      style="width:100%"
                                      v-model="item.lowValue"
                                    ></Input>
                                  </FormItem>
                                </Col>
                                <Col span="2" style="text-align:center">-</Col>
                                <Col span="11">
                                  <FormItem
                                    :prop="'scorelist.'+index+'.highValue'"
                                    :rules="ruleDynamic.highValue"
                                  >
                                    <Input
                                      :disabled="isDisabled"
                                      :maxlength="10"
                                      @on-blur="handleCompareValue(index)"
                                      number
                                      placeholder="请输入"
                                      style="width:100%"
                                      v-model="item.highValue"
                                    ></Input>
                                  </FormItem>
                                </Col>
                              </Row>
                            </FormItem>
                          </Col>
                          <Col span="2">
                            <Button
                              @click="handleRemove(index)"
                              class="close-item"
                              icon="md-close"
                              shape="circle"
                              size="small"
                              type="primary"
                              v-if="showCloseItem"
                            ></Button>
                          </Col>
                        </Row>
                      </Col>
                    </Row>
                  </div>
                  <div class="mt-set8" v-else-if="setWay == 'setExp'">
                    <set-expression-single
                      :apiOption="factorApi"
                      :arrExpressIdIn="arrExpressIdIn"
                      :arrExpressIn="arrExpressIn"
                      :selectEle="sctFactor"
                      :showPanel="editPanel"
                      @getExpress="handleSetExpress"
                      @getFactorids="handleSetFactorids"
                      ref="expressCom"
                      selectWays="sctFactor"
                    ></set-expression-single>
                  </div>
                </Col>
              </Row>
            </Form>
          </Scroll>
          <div slot="footer">
            <div v-if="showFooter">
              <Button @click="handleResetAdd" v-if="showAddReset">重置</Button>
              <Button @click="handleResetEdit" v-else>重置</Button>
              <Button
                :loading="confirmLoading"
                @click="handleSubmitAdd"
                type="primary"
                v-if="showAddSubmit"
              >提交</Button>
              <Button :loading="confirmLoading" @click="handleSubmitEdit" type="primary" v-else>提交</Button>
            </div>
          </div>
        </Modal>
      </div>
    </Row>
    <br />
    <Table :columns="columnsDaily" :data="dataLibrary" :loading="loading" border ref="selection"></Table>
    <Page
      :current.sync="searchForm.pageNum"
      :page-size="searchForm.pageSize"
      :total="total"
      @on-change="pageChange"
      class="mt-set16"
      ref="page"
      show-total
    />
  </div>
</template>

<script>
import expressJudgeTool from '@/mixin/expressJudgeTool'
import setExpressionSingle from '@/views/components/set-expression/set-expression-single'
import AchieveIndicatorLibAPI from '@api/common/achieveIndicatorLib'
import FactorInfoInfAPI from '@api/common/factorInfoInf'
import ActivitiesAPI from '@api/common/specialLines'
import SitesAPI from '@api/common/sites'
import cloneDeep from 'lodash/cloneDeep'
import {
  VALUE_SOURCE,
  INDEX_TYPE,
  INDEX_ATTR,
  RADIX_POINTER
} from '@/config/dict.js/'
export default {
  name: 'index-library',
  components: {
    setExpressionSingle
  },
  mixins: [expressJudgeTool],
  data () {
    return {
      arrExpressIn: [],
      arrExpressIdIn: [],
      modalTitle: '',
      isDisabled: false,
      isDisabledName: false,
      isDisabledWay: false,
      dataSourceArr: VALUE_SOURCE,
      indexTypeArr: INDEX_TYPE,
      indexAttrArr: INDEX_ATTR,
      radixPointArr: RADIX_POINTER,
      modalIndex: false,
      editPanel: true,
      plc: '请输入',
      valueIndexName: '',
      loading: false,
      confirmLoading: false,
      addLoading: false,
      viewLoading: false,
      editLoading: false,
      delLoading: false,
      searchTagetLoading: false,
      showGrade: false,
      showFooter: true,
      showAddItem: false,
      showCloseItem: false,
      showAddSubmit: true,
      showAddReset: true,
      showNameInput: true,
      indexName: [],
      indexTargets: [],
      indexNameTargets: [],
      factorTargets: [],
      centresArr: [],
      centerName: '',
      centerList: [],
      selectedFactorIdsArr: [],
      specialLine: '',
      setWay: '',
      factor: '',
      factorRes: '',
      factorApi: FactorInfoInfAPI,
      sctFactor: '选择因子',
      selectWay: 'Excel导入',
      count: 0,
      total: 0,
      editId: '',
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        name: ''
      },
      searchFactor: {
        factorcode: ''
      },
      columnsDaily: [
        {
          type: 'index',
          title: '序号',
          width: 65,
          align: 'center'
        },
        {
          title: '指标名称',
          key: 'name'
        },
        {
          title: '所属中心',
          key: 'center'
        },
        {
          title: '指标分类',
          key: 'typeCode'
        },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          width: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  directives: [
                    {
                      name: 'permission',
                      arg: 'view',
                      value: 'index-library'
                    }
                  ],
                  props: {
                    type: 'info',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.InfoView(params.row)
                    }
                  }
                },
                '查看'
              ),
              h(
                'Button',
                {
                  directives: [
                    {
                      name: 'permission',
                      arg: 'edit',
                      value: 'index-library'
                    }
                  ],
                  props: {
                    type: 'success',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.InfoEdit(params.row)
                    }
                  }
                },
                '编辑'
              ),
              h(
                'Poptip',
                {
                  props: {
                    confirm: true,
                    transfer: true,
                    title: '确定删除该行信息？'
                  },
                  on: {
                    'on-ok': () => {
                      this.InfoRemove(params.row)
                    }
                  }
                },
                [
                  h(
                    'Button',
                    {
                      directives: [
                        {
                          name: 'permission',
                          arg: 'delete',
                          value: 'piecework-template'
                        }
                      ],
                      props: {
                        type: 'warning',
                        size: 'small'
                      }
                    },
                    '删除'
                  )
                ]
              )
            ])
          }
        }
      ],
      dataLibrary: [],
      formIndex: {
        name: '',
        typeCode: '',
        belong: '',
        unitNmae: '',
        point: '',
        centerSelected: '',
        specialLine: [],
        iscalculate: '',
        dataTypeGruop: [],
        remark: '',
        scorelist: []
      },
      ruleDynamic: {
        score: [
          {
            required: true,
            message: '请输入'
          }
        ],
        lowValue: [
          {
            required: true,
            type: 'number',
            min: 0,
            message: '不能为空，输入数字'
          }
        ],
        highValue: [
          {
            required: true,
            type: 'number',
            min: 0,
            message: '不能为空，输入数字'
          }
        ]
      },
      ruleValidate: {
        name: [
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('不能为空'))
              } else {
                // 指标名称是否重复
                if (this.showNameInput) {
                  AchieveIndicatorLibAPI.getByName(this.formIndex.name)
                    .then(({ data }) => {
                      if (data.status === 'success') {
                        if (data.data !== null) {
                          callback(new Error('指标名称已存在'))
                          this.formIndex.name = ''
                        } else {
                          callback()
                        }
                      }
                    })
                    .catch(err => {
                      console.error('err: ', err)
                    })
                } else {
                  callback()
                }
              }
            },
            required: true,
            trigger: 'blur'
          }
        ],
        typeCode: [
          {
            required: true,
            type: 'number',
            trigger: 'change',
            message: '请选择'
          }
        ],
        belong: [
          {
            required: true,
            type: 'number',
            trigger: 'change',
            message: '请选择'
          }
        ],
        unitNmae: [
          {
            required: true,
            trigger: 'blur',
            message: '不能为空'
          }
        ],
        point: [
          {
            required: true,
            trigger: 'change',
            type: 'number',
            message: '请选择'
          }
        ],
        centerSelected: [
          {
            required: true,
            type: 'number',
            message: '请选择'
          }
        ],
        specialLine: [
          {
            required: true,
            type: 'array',
            message: '请选择',
            trigger: 'blur'
          }
        ],
        iscalculate: [
          {
            required: true,
            trigger: 'change',
            message: '请选择'
          }
        ]
      }
    }
  },
  watch: {
    modalIndex (newVal) {
      if (!newVal) {
        this.arrExpressIn = []
        this.arrExpressIdIn = []
      }
    },
    valueIndexName () {
      this.searchForm.name = this.valueIndexName
    }
  },
  created () {
    this.arrExpressIn = []
    this.arrExpressIdIn = []
  },
  mounted () {
    this.doSearch()
  },
  methods: {
    restPage () {
      this.searchForm.pageNum = 1
    },
    doInitSearch () {
      this.searchForm.pageNum = 1
      this.doSearch()
    },
    checkAreaContinue () {
      let result = ''
      if (this.formIndex.scorelist.length === 1) {
        return result
      }
      if (this.formIndex.scorelist.length > 1) {
        for (let i = 1; i < this.formIndex.scorelist.length; i++) {
          let lastTmp = this.formIndex.scorelist[i - 1]
          let tmp = this.formIndex.scorelist[i]
          if (tmp.lowValue >= tmp.highValue) {
            result += i + 1 + ','
          }
          if (lastTmp.highValue !== tmp.lowValue) {
            result += i + 1 + ','
          }
        }
      }
      return result
    },
    handleTestValue (index) {
      if (index > 0) {
        if (this.formIndex.scorelist[index - 1].highValue) {
          if (
            this.formIndex.scorelist[index].lowValue !==
            this.formIndex.scorelist[index - 1].highValue
          ) {
            this.formIndex.scorelist[index].lowValue = ''
            // this.$refs['lowValue' + index][0].$el.children[1].value = ''
            this.$Message.destroy()
            this.$Message.warning('当前区间初始值应与上层末尾值相等!')
          }
        } else {
          this.$Message.destroy()
          this.$Message.warning('上层区间末尾值不能为空!')
        }
      }
      if (this.formIndex.scorelist[index].highValue) {
        if (
          this.formIndex.scorelist[index].lowValue -
            this.formIndex.scorelist[index].highValue >=
          0
        ) {
          this.formIndex.scorelist[index].lowValue = ''
          this.$Message.destroy()
          this.$Message.warning('打分区间，起始值必须小于末尾值!')
        }
      }
    },
    handleCompareValue (index) {
      if (this.formIndex.scorelist[index - 1].highValue) {
        if (
          this.formIndex.scorelist[index].highValue <=
          this.formIndex.scorelist[index - 1].highValue
        ) {
          this.formIndex.scorelist[index].highValue = this.formIndex.scorelist[index - 1].highValue + 1
          // this.$refs['lowValue' + index][0].$el.children[1].value = ''
          this.$Message.destroy()
          this.$Message.warning('当前区间末尾值比上层末尾值至少大1!')
        }
      }
      if (
        this.formIndex.scorelist[index].lowValue -
          this.formIndex.scorelist[index].highValue >=
        0
      ) {
        this.formIndex.scorelist[index].highValue = ''
        this.$Message.destroy()
        this.$Message.warning('打分区间，起始值必须小于末尾值!')
      }
    },
    // 查询
    doSearch () {
      this.loading = true
      this.$nextTick(() => {
        AchieveIndicatorLibAPI.viewList({ ...this.searchForm })
          .then(({ data }) => {
            this.loading = false
            if (data.status === 'success') {
              const formData = cloneDeep(data.data.list)
              for (let i = 0, len = formData.length; i < len; i++) {
                formData[i].typeCode = formData[i].map.typeCode
              }

              this.dataLibrary = formData
              this.total = data.data.total
            }
          })
          .catch(err => {
            console.error('err: ', err)
            this.loading = false
          })
      })
    },
    // 查看\编辑-公共设置
    handleCommonSet (formData) {
      formData.point = parseInt(formData.point)
      formData.iscalculate = formData.iscalculate.toString()
      if (formData.dataType) {
        formData.dataTypeGruop = formData.dataType.split(',')
      }
      if (!formData.map || !formData.map.factorlist) {
        return
      }
      formData.centerSelected = parseInt(
        formData.map.factorlist.map(item => {
          this.centerName = item.usercenter
          item = item.usercenter
          return item
        })[0]
      )
      formData.specialLine = formData.map.factorlist.map(item => {
        if (item.userline) {
          item = item.usercenter + '#' + item.userline
        }
        return item
      })
    },
    // 初始化所属中心、专线
    initcCenterLine (centerId) {
      // 所属中心数据初始化
      SitesAPI.viewListOwer()
        .then(({ data }) => {
          if (data.status === 'success') {
            if (data.data.length > 0) {
              this.centresArr = data.data
              // 所属专线数据初始化
              this.centerList = []

              if (centerId) {
                ActivitiesAPI.getlistbysitesid(centerId)
                  .then(({ data }) => {
                    if (data.status === 'success') {
                      this.centerList = data.data
                    }
                  })
                  .catch(err => {
                    console.error(err)
                  })
              }
            }
          }
        })
        .catch(err => {
          console.error(err)
        })
    },
    // 表格操作-新增
    InfoAdd () {
      this.modalIndex = true
      this.modalTitle = '新增指标'
      this.isDisabled = false
      this.isDisabledName = false
      this.isDisabledWay = false
      this.showGrade = false
      this.setWay = ''
      this.selectWay = ''
      this.editPanel = true
      this.showFooter = true
      this.showAddSubmit = true
      this.showAddReset = true
      this.showAddItem = true
      this.showCloseItem = true
      this.showNameInput = true
      this.factorRes = ''
      this.formIndex.scorelist = [{ index: 0 }]
      this.formIndex.dataTypeGruop = []
      this.formIndex.dataType = ''
      this.formIndex.getValueType = ''
      this.factor = ''
      if (this.$refs.expressCom) {
        this.$refs.expressCom.expression = ''
      }
      this.initcCenterLine()
      this.$nextTick(() => {
        this.$refs['formIndex'].resetFields()
      })
    },
    // 表格操作-查看
    InfoView (row) {
      let infoId = row.uuid
      this.viewLoading = true
      this.modalTitle = '查看指标'
      AchieveIndicatorLibAPI.view(infoId)
        .then(({ data }) => {
          this.viewLoading = false
          if (data.status === 'success') {
            this.modalIndex = true
            this.isDisabled = true // 指标是否可编辑
            this.isDisabledName = true // 指标名称是否可编辑
            this.isDisabledWay = true // 打分方式是否可选择
            this.showFooter = false // 是否显示底部操作按钮
            this.showNameInput = true // 是否显示指标名称
            if (data.data.formulaname) {
              this.arrExpressIn = data.data.formulaname.split(',')
            }
            const formDataView = cloneDeep(data.data)

            // 公共设置
            this.handleCommonSet(formDataView)
            this.initcCenterLine(this.centerName)
            if (formDataView.getValueType === 4) {
              this.selectWay = '4'
              this.setWay = ''
            } else if (formDataView.getValueType === 2) {
              // 判断取值方式是打分标准还是设置公式
              this.selectWay = '2'
              this.setWay = 'useStd'
              this.showGrade = true
              this.showAddItem = false // 是否显示打分标准-添加按钮
              this.showCloseItem = false // 是否显示打分标准-关闭按钮
              if (!formDataView.map) {
                return
              }
              // 设置因子名称值
              if (formDataView.map.factorName) {
                this.factorTargets = [
                  {
                    uuid: formDataView.factorid,
                    factorName: formDataView.map.factorName
                  }
                ]
                this.factor = formDataView.factorid

                this.factorRes = formDataView.map.factorName
              }
              if (formDataView.map.scorelist) {
                formDataView.scorelist = [].concat(formDataView.map.scorelist)
              }
            } else if (formDataView.getValueType === 3) {
              this.selectWay = '3'
              this.setWay = 'setExp'
              this.editPanel = false // 是否显示 设置公式面板

              this.$nextTick(() => {
                this.$refs.expressCom.expressionChina = formDataView.formulaname
                  .split(',')
                  .join('')
              })
            }
            this.$refs['formIndex'].resetFields()
            this.formIndex = formDataView
          }
        })
        .catch(err => {
          console.error(err)
          this.viewLoading = false
        })
    },
    // 表格操作-编辑
    InfoEdit (row) {
      let infoId = row.uuid

      this.editId = row.uuid
      this.editLoading = true
      this.modalTitle = '编辑指标'

      AchieveIndicatorLibAPI.view(infoId)
        .then(({ data }) => {
          this.editLoading = false
          if (data.status === 'success') {
            this.modalIndex = true
            this.isDisabled = false
            this.isDisabledName = true
            this.isDisabledWay = true
            this.showFooter = true
            this.showAddSubmit = false
            this.showAddReset = false
            this.showNameInput = false
            // debugger
            // if (data.data.formulaname && data.data.formula) {
            //   this.arrExpressIn = data.data.formulaname.split(',')
            //   this.arrExpressIdIn = data.data.formula.split(',')
            // }
            const formDataView = cloneDeep(data.data)

            // 公共设置
            this.handleCommonSet(formDataView)
            this.initcCenterLine(this.centerName)
            if (formDataView.getValueType === 4) {
              this.selectWay = '4'
              this.setWay = ''
            } else if (formDataView.getValueType === 2) {
              this.selectWay = '2'
              this.setWay = 'useStd'
              this.showGrade = true
              this.showAddItem = true
              this.showCloseItem = true
              if (formDataView.map.factorName) {
                this.factorTargets = [
                  {
                    uuid: formDataView.factorid,
                    factorName: formDataView.map.factorName
                  }
                ]
                this.factor = formDataView.factorid
                this.factorRes = formDataView.map.factorName
              }
              if (formDataView.map.scorelist) {
                formDataView.scorelist = [].concat(formDataView.map.scorelist)
              }
            } else if (formDataView.getValueType === 3) {
              this.selectWay = '3'
              this.setWay = 'setExp'
              this.editPanel = true
              this.$nextTick(() => {
                this.arrExpressIn = data.data.formulaname.split(',')
                this.arrExpressIdIn = data.data.formula.split(',')
                // this.$refs.expressCom.expressionChina = formDataView.formulaname
                //   .split(',')
                //   .join('')
              })
            }
            this.$refs['formIndex'].resetFields()
            this.formIndex = formDataView
          }
        })
        .catch(err => {
          console.error(err)
          this.editLoading = false
        })
    },
    // 表格操作-删除
    InfoRemove (row) {
      let infoId = row.uuid
      this.delLoading = true
      AchieveIndicatorLibAPI.delete(infoId)
        .then(({ data }) => {
          this.delLoading = false
          if (data.status === 'success') {
            this.$Message.destroy()
            this.$Message.success('删除成功!')
            this.doInitSearch()
          }
        })
        .catch(err => {
          console.error('err: ', err)
          this.delLoading = false
        })
    },
    // 新增指标-提交
    handleSubmitAdd () {
      let result = this.checkAreaContinue()
      if (result) {
        this.$Message.destroy()
        this.$Message.warning('出错公式序号为：' + result)
        return
      }
      this.$refs['formIndex'].validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const formData = cloneDeep(this.formIndex)
          formData.point = formData.point.toString()
          formData.iscalculate = parseInt(formData.iscalculate)
          delete formData.centerSelected
          delete formData.specialLine
          delete formData.dataTypeGruop

          if (!formData.getValueType) {
            this.confirmLoading = false
            this.$Message.destroy()
            this.$Message.warning('请先选择一种定义指标的方式!')
            return false
          }
          if (formData.getValueType === 2) {
            if (!formData.factorid) {
              this.confirmLoading = false
              this.$Message.destroy()
              this.$Message.warning('请输入因子名称!')
              return false
            }
            for (let i = 0, len = formData.scorelist.length; i < len; i++) {
              if (
                formData.scorelist[i].lowValue -
                  formData.scorelist[i].highValue >
                0
              ) {
                this.$Message.destroy()
                this.$Message.warning('打分区间起始值不得大于末尾值!')
                this.confirmLoading = false
                return false
              }
            }
          } else if (formData.getValueType === 3) {
            if (!formData.formula) {
              this.confirmLoading = false
              this.$Message.destroy()
              this.$Message.warning('请设置公式!')
              return false
            }
            if (!this.isCorrectExpress(this.selectedFactorIdsArr)) {
              formData.formula = ''
              formData.formulaname = ''
              this.$Message.destroy()
              this.$Message.warning('输入的公式格式有误，请重新输入!')
              this.confirmLoading = false
              return false
            }
          }
          AchieveIndicatorLibAPI.create({ ...formData })
            .then(({ data }) => {
              this.confirmLoading = false
              if (data.status === 'success') {
                this.modalIndex = false
                this.$refs['formIndex'].resetFields()
                this.$Message.destroy()
                this.$Message.success('添加成功!')
                this.doInitSearch()
              }
            })
            .catch(err => {
              console.error(err)
              this.confirmLoading = false
            })
        }
      })
    },
    // 编辑指标-提交
    handleSubmitEdit () {
      let result = this.checkAreaContinue()
      if (result) {
        this.$Message.destroy()
        this.$Message.warning('出错公式序号为：' + result)
        return
      }
      this.$refs['formIndex'].validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const formDataEdit = cloneDeep(this.formIndex)
          formDataEdit.point = formDataEdit.point.toString()
          formDataEdit.iscalculate = parseInt(formDataEdit.iscalculate)
          delete formDataEdit.centerSelected
          delete formDataEdit.specialLine
          delete formDataEdit.dataTypeGruop

          if (formDataEdit.getValueType === 2) {
            if (!formDataEdit.factorid) {
              this.confirmLoading = false
              this.$Message.destroy()
              this.$Message.warning('请输入因子名称!')
              return false
            }
            for (let i = 0, len = formDataEdit.scorelist.length; i < len; i++) {
              if (
                formDataEdit.scorelist[i].lowValue -
                  formDataEdit.scorelist[i].highValue >
                0
              ) {
                this.$Message.destroy()
                this.$Message.warning('打分区间起始值不得大于末尾值!')
                this.confirmLoading = false
                return false
              }
            }
          } else if (formDataEdit.getValueType === 3) {
            if (!formDataEdit.formula) {
              this.confirmLoading = false
              this.$Message.destroy()
              this.$Message.warning('请设置公式!')
              return false
            }
            if (!this.isCorrectExpress(formDataEdit.formula.split(','))) {
              formDataEdit.formula = ''
              formDataEdit.formulaname = ''
              this.$Message.destroy()
              this.$Message.warning('输入的公式格式有误，请重新输入!')
              this.confirmLoading = false
              return false
            }
          }
          AchieveIndicatorLibAPI.edit({ ...formDataEdit })
            .then(({ data }) => {
              this.confirmLoading = false
              if (data.status === 'success') {
                this.modalIndex = false
                this.$refs['formIndex'].resetFields()
                this.$Message.destroy()
                this.$Message.success('编辑成功!')
                this.doInitSearch()
              }
            })
            .catch(err => {
              console.error(err)
              this.confirmLoading = false
            })
        }
      })
    },
    // 新增指标-重置
    handleResetAdd () {
      this.centerList = []
      this.$refs['formIndex'].resetFields()
      this.factor = ''
      if (this.$refs.expressCom) {
        this.$refs.expressCom.expressionChina = ''
      }
    },
    // 编辑指标-重置
    handleResetEdit () {
      AchieveIndicatorLibAPI.view(this.editId)
        .then(({ data }) => {
          if (data.status === 'success') {
            const formDataView = cloneDeep(data.data)
            formDataView.point = parseInt(formDataView.point)
            formDataView.iscalculate = formDataView.iscalculate.toString()
            if (formDataView.dataType) {
              formDataView.dataTypeGruop = formDataView.dataType.split(',')
            }
            // 所属中心数据处理
            formDataView.centerSelected = parseInt(
              formDataView.map.factorlist.map(item => {
                item = item.usercenter
                return item
              })[0]
            )
            // 所属专线数据处理
            formDataView.specialLine = formDataView.map.factorlist.map(item => {
              if (item.userline) {
                item = item.usercenter + '#' + item.userline
              }
              return item
            })
            if (formDataView.getValueType === 2) {
              if (formDataView.map.factorName) {
                this.factorTargets = [
                  {
                    uuid: formDataView.factorid,
                    factorName: formDataView.map.factorName
                  }
                ]
                this.factor = formDataView.factorid

                this.factorRes = formDataView.map.factorName
              }
              if (formDataView.map.scorelist) {
                formDataView.scorelist = [].concat(formDataView.map.scorelist)
              }
            } else if (formDataView.getValueType === 3) {
              this.$nextTick(() => {
                // this.$refs.expressCom.expressionChina = formDataView.formulaname
                //   .split(',')
                //   .join('')
                this.arrExpressIn = data.data.formulaname.split(',')
                this.arrExpressIdIn = data.data.formula.split(',')
              })
            }
            this.formIndex = formDataView
          }
        })
        .catch(err => {
          console.error(err)
        })
    },
    // 表单-指标分类
    handleTypeCode (value) {
      this.formIndex.typeCode = value
    },
    // 表单-小数点
    handlePoint (value) {
      this.formIndex.point = value
    },
    // 表单-所属中心
    handleCenter (site) {
      if (site) {
        this.centerList = []
        this.$nextTick(() => {
          ActivitiesAPI.getlistbysitesid(site.value)
            .then(({ data }) => {
              if (data.status === 'success') {
                this.centerList = data.data
              }
            })
            .catch(err => {
              console.error(err)
            })
          this.formIndex.center = site.label
          this.formIndex.specialLine = []
        })
      }
    },
    // 表单-所属专线
    handleSpecialLine (value) {
      this.formIndex.centerlines = [].concat(value).join(',')
    },
    // 表单-指标归属
    handleBelong (value) {
      this.formIndex.belong = value
    },
    // 表单-数据类型
    handleDataType (data) {
      this.formIndex.dataType = data.join(',')
    },
    // 表单-是否用于计算
    handleCalcul (value) {
      this.formIndex.iscalculate = value
    },
    // 表单-指标取值方式
    setIndexWay (label) {
      if (label === '4') {
        this.setWay = ''
        this.formIndex.getValueType = 4
        this.formIndex.factorids = ''
        this.formIndex.formula = ''
        this.formIndex.formulaname = ''
        this.selectedFactorIdsArr = ''
        this.factor = ''
        this.formIndex.factorid = ''
        this.formIndex.scorelist = [{ index: 0 }]
      } else if (label === '2') {
        this.setWay = 'useStd'
        this.formIndex.getValueType = 2
        this.factorTargets = []
        this.formIndex.factorids = ''
        this.formIndex.formula = ''
        this.formIndex.formulaname = ''
        this.selectedFactorIdsArr = ''
      } else if (label === '3') {
        this.setWay = 'setExp'
        this.formIndex.getValueType = 3
        this.factor = ''
        this.formIndex.factorid = ''
        this.formIndex.scorelist = [{ index: 0 }]
        this.$nextTick(() => {
          this.$refs.expressCom.expression = ''
          let scrollView = document.querySelector(
            '.modal-scroll-set .ivu-scroll-container'
          )
          scrollView.scrollTop = scrollView.scrollHeight
        })
      }
    },
    // 打分标准-因子搜索
    handleSearchFactor (value) {
      const factorName = value.trim()

      this.searchTagetLoading = true
      FactorInfoInfAPI.getlist({ factorName }).then(({ data }) => {
        this.searchTagetLoading = false
        this.factorTargets = data.data.filter(
          (item, index) => item.factorName.indexOf(factorName) > -1
        )
      })
    },
    // 打分标准-选择因子项
    handleSelectFactor (value) {
      this.showAddItem = true
      this.showGrade = true
      this.formIndex.factorid = value
      this.formIndex.scorelist = [{ index: 0 }]
      this.$nextTick(() => {
        this.$refs.factorSelect.$el.children[0].children[1].children[1].blur()
      })
    },
    // 打分标准-增加操作项
    handleAdd () {
      this.count++
      this.formIndex.scorelist.push({
        index: this.count
      })
    },
    // 打分标准-删除操作项
    handleRemove (index) {
      if (index === 0 && this.formIndex.scorelist.length === 1) {
        this.formIndex.factorid = ''
        this.showAddItem = false
      }
      this.formIndex.scorelist.splice(index, 1)
    },
    // 设置公式-选择因子项
    handleSetFactorids (factorids) {
      this.formIndex.factorids = factorids
    },
    // 设置公式-公式传参
    handleSetExpress (formula, formulaname, idArray) {
      this.formIndex.formula = formula
      this.formIndex.formulaname = formulaname
      this.selectedFactorIdsArr = idArray
    },
    // 分页
    pageChange (pageNum) {
      this.searchForm.pageNum = pageNum
      this.doSearch()
    }
  }
}
</script>

<style lang="less">
.row_split-set22 {
  margin-bottom: 4px;
}
.dynamic_panle {
  .ivu-form-item-label {
    width: 60px !important;
  }
  .ivu-form-item-content {
    margin-left: 60px !important;
  }
  .add-item {
    margin-top: 4px;
  }
  .close-item {
    margin-top: 4px;
  }
}
.mg-set {
  margin: 0 12px 0 8px;
}
.long-label {
  .ivu-form-item-label {
    width: 96px !important;
  }
}
.modal-scroll-set .ivu-modal-body {
  padding-right: 0;
}
.modal-scroll-set .ivu-scroll-container {
  overflow-x: hidden;
}
.modal-scroll-set .ivu-scroll-content {
  margin-right: 16px;
}
</style>
