<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? '编辑成交状态' : '更新成交状态'"
      :fullscreen="false"
      width="330px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="50px">
        <div slot="otherItem" class="scoped-select">
          <div :class="`${curDes === item.key ? 'cur' : ''} ss-op`" v-for="(item, index) in tsdArr" @click="desHandle(item)" :key="index">{{ item.key }}</div>
        </div>
        <div slot="footer" style="padding-top: 20px;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object,
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      curEstateObj: {},
      curDes: '',
    }
  },
  computed: {
    tsdArr () {
      return this.$dictData.trade_state_des || []
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDef()
      }
    },
  },
  methods: {
    desHandle (item) {
      this.curDes = item.key
      this.getDef('trade_state_des', item.key)
    },
    getDef(str, key) {
      let params = { ...this.curObj }
      if (str === 'trade_state_des') {
        params.trade_state_des = key
      }
      this.formData = [
        { label: '状态', key: 'trade_state', type: 'select', options: this.$dictData.trade_state },
        { label: '备注', key: 'trade_state_des', type: 'textarea' },
      ]
      this.setDefaultValue(params)
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            let apiStr = 'admtradestateedit'
            if (this.curObj.id) {
              newForm.id = this.curObj.id
            }
            newForm.record_des = `维护成:${arrToObj(this.$dictData.trade_state)[newForm.trade_state]}，原因:${newForm.trade_state_des}`
            this.$api.cust[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.scoped-select {
  .ss-op {
    color: #666;
    background: #ccc;
    border-radius: 6px;
    padding: 3px 10px;
    margin-right: 10px;
    margin-bottom: 10px;
    display: inline-block;
    cursor: pointer;
    &.cur {
      color: #fff;
      background: #2d8cf0;
    }
  }
}
</style>
