ConfigDialog.vue 9.29 KB
<template>
  <baseDialog
    :is-show.sync="dialogShow"
    :config="dialogConfig.setting"
    :dialog-config="dialogConfig.config"
    class="dialog"
    @handleClick="handleClick"
  >
    <div class="dialog-content">
      <div class="month">
        <div class="label">指定月份:</div>
        <el-date-picker
          v-model="targetDate"
          type="month"
          placeholder="选择月份"
          :picker-options="monthPickerOptions"
          size="small"
          :clearable="false"
          @change="changeDate"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </div>
      <el-table
        :data="tableData"
        border
        ref="table"
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
          align="center"
        >
        </el-table-column>
        <template v-for="(item,index) in  tableParams.tableColumns">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :width="item.width"
            :min-width="item.minWidth"
            align="center"
            :key="index"
          >
            <template slot-scope="scope">
              <div
                v-if="item.addHtml"
                :key="tableKey"
                class="input-box"
              >
                <template v-if="scope.row.isEdit">
                  <el-input
                    v-model="scope.row[item.addHtml]"
                    :maxlength="item.maxlength"
                    :placeholder="item.placeholder"
                    :ref="`${scope.row.id}`"
                    size="mini"
                    :class="scope.row.className"
                    @blur="handleInput($event, scope.row, item)"
                  ></el-input>
                </template>
                <template v-else>
                  {{ scope.row[item.addHtml] != null ?scope.row[item.addHtml].toLocaleString() : ''}}
                </template>
                <template v-if="item.unit">
                  <div class="unit">{{item.unit}}</div>
                </template>
              </div>
              <template v-else-if="item.formatter">
                <span
                  v-if="item.html"
                  v-html="item.formatter(scope.row)"
                />
                <template v-if="!item.html">
                  <span :class="item.className ? item.className(scope.row) : ''" />
                  <span v-text="item.formatter(scope.row)" />
                </template>
              </template>
              <span v-else>{{ scope.row[item.prop] || "-" }}</span>
            </template>
          </el-table-column>
        </template>
        <el-table-column
          label="操作"
          fixed="right"
          :align="tableParams.tableOption.align || 'center'"
          :width="tableParams.tableOption.width || 'auto'"
          :key="tableKey"
        >
          <template slot-scope="scope">
            <template v-if="!scope.row.isEdit">
              <el-button
                :class="[{'is-disabled':isEditStatus}]"
                type="text"
                @click="edit(scope)"
                :disabled="isEditStatus"
              >编辑</el-button>
            </template>
            <template v-if="scope.row.isEdit">
              <el-button
                type="text"
                @click="save(scope.row)"
              >保存</el-button>
              <el-button
                type="text"
                @click="cancel(scope.row)"
              >取消</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </baseDialog>
</template>

<script type='text/ecmascript-6'>
import dayjs from 'dayjs'
import { getDataConfig, saveOrUpdateDataConfig } from '@api/earlyWarningManager'
export default {
  name: '',
  props: {
    show: Boolean
  },
  data () {
    return {
      dialogShow: false,
      dialogConfig: {
        setting: {
          showClose: true,
          appendToBody: true,
          btnList: [],
          isShowFooter: true,
          confirm_name: '确定',
          cancel_name: '取消'
        },
        config: {
          title: "数据配置",
          width: "720px"
        }
      },
      targetDate: dayjs().format('YYYY-MM'),
      monthPickerOptions: {
        disabledDate: date => {
          let next = dayjs().add(1, 'month').valueOf(),
            prev = dayjs().subtract(1, 'month').valueOf(),
            now = dayjs().valueOf()
          return date != now && (date > next || date < prev)
        }
      },
      tableData: [
        {
          businessType: 4,
          outputTarget: '',
          grossProfit: '',
          grossMargin: ''
        },
        {
          businessType: 3,
          outputTarget: '',
          grossProfit: '',
          grossMargin: ''
        },
        {
          businessType: 2,
          outputTarget: '',
          grossProfit: '',
          grossMargin: ''
        }
      ],
      tableParams: {
        tableOption: {
          width: '100'
        },
        tableColumns: [
          {
            label: '业务类型',
            minWidth: 110,
            formatter: (cellValue) => {
              return {
                2: '轻改',
                3: '权益',
                4: '延保'
              }[cellValue.businessType]
            },
          },
          {
            label: '产值目标',
            addHtml: 'outputTarget',
            minWidth: 135,
            maxlength: 10,
            placeholder: '请输入',
            max: 9999999999
          },
          {
            label: '毛利润',
            addHtml: 'grossProfit',
            minWidth: 135,
            maxlength: 10,
            placeholder: '请输入',
            max: 9999999999
          },
          {
            label: '毛利润率',
            addHtml: 'grossMargin',
            unit: '%',
            minWidth: 135,
            maxlength: 5,
            placeholder: '请输入',
            max: 100,
            decimals: true
          },
        ]
      },
      tableKey: Date.now(),
      isEditStatus: false
    }
  },
  watch: {
    show (n) {
      this.dialogShow = n
      if (n) {
        this.targetDate = this.$options.data.call(this).targetDate
        this.init()
      }
    },
    dialogShow (n) {
      this.$emit('update:show', n)
    }
  },
  mounted () {
  },
  methods: {
    init () {
      this.isEditStatus = false
      this.tableData = this.$options.data.call(this).tableData
      this.getDataConfig()
    },
    async getDataConfig () {
      const { targetDate } = this
      const res = await getDataConfig({
        targetDate
      })
      if (res && res.length) {
        this.tableData = res
      }
    },
    edit (e) {
      let { row } = e
      if (this.isEditStatus) return
      row.origin_outputTarget = row.outputTarget
      row.origin_grossProfit = row.grossProfit
      row.origin_grossMargin = row.grossMargin
      row.isEdit = true
      this.isEditStatus = true
      this.tableKey = Date.now()
    },
    save (row) {
      this.isEditStatus = false
      delete row.origin_outputTarget
      delete row.origin_grossProfit
      delete row.origin_grossMargin
      delete row.isEdit
      this.tableKey = Date.now()
    },
    cancel (row) {
      this.isEditStatus = false
      row.isEdit = false
      row.outputTarget = row.origin_outputTarget
      row.grossProfit = row.origin_grossProfit
      row.grossMargin = row.origin_grossMargin
      this.tableKey = Date.now()
    },
    handleInput (event, row, item) {
      let value = event.target.value
      if (value.length == 0) return
      value = String(value).match(/\d*\.?\d+/)
      if (!value) value = 0
      value = Number(value) > item.max ? item.max : value
      if (item.decimals) {
        if (!Number.isInteger(value)) value = String(value).replace(/(\d*\.?\d{0,2})\d*/, '$1')
        value = Number(String(value).replace(/^0+/, ''))
      } else value = parseInt(value)
      row[item.addHtml] = Math.abs(Number(value))
      this.tableKey = Date.now()
    },
    async handleClick () {
      try {
        let { tableData } = this,
          { targetDate } = this
        tableData.map(e => {
          if (e.isEdit) {
            e.outputTarget = e.origin_outputTarget
            e.grossProfit = e.origin_grossProfit
            e.grossMargin = e.origin_grossMargin
          }
          e.targetDate = targetDate
        })
        const res = await saveOrUpdateDataConfig({
          targetConfigList: tableData
        })
        if (res) {
          this.$message.success('保存成功')
          this.dialogShow = false
          this.$emit('update')
        }
      } catch (error) {
        if (error) this.$message.error(error)
      }

    },
    changeDate () {
      this.init()
    }
  }
}
</script>

<style scoped lang='scss'>
.dialog {
  &-content {
    padding: 5px 0;
    .month {
      display: flex;
      align-items: center;
      margin-bottom: 32px;
      .label {
        color: #333333;
        font-size: 14px;
        width: 150px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        &::before {
          content: "*";
          color: #eb4e4e;
          display: block;
          margin-right: 6px;
        }
      }
    }
  }
  .input-box {
    padding: 4px 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>