<template>
  <modal v-model="modal" class-name="vertical-center-modal" width="500px">
    <div class="title" slot="header">{{ data.purchaseBillNo }}</div>
    <div class="sub-content" style="padding: 20px">
      <div class="row">
        <div class="col">
          <div class="label">选择原因：</div>
          <div class="value">
            <my-select-input v-model="remark" :list="remarkList" size="small" width="180"></my-select-input>
          </div>
        </div>
      </div>
    </div>
    <div class="footer" slot="footer">
      <Button @click="cfm" class="btn" type="error" size="small">驳回</Button>
      <Button @click="cancel" class="btn">取消</Button>
    </div>
  </modal>
</template>

<script>
import { ApprovalBillFlow } from '@/api/applyPurchase'
import { AddDataDictionary, DataDictionary } from '@/api/baseData'
import MySelectInput from '_c/my-select-input'

export default {
  name: 'pass_modal',
  data () {
    return {
      modal: false,
      remark: '',
      remarkList: [],
      sourceRemarkList: []
    }
  },
  components: {
    MySelectInput
  },
  props: {
    value: {
      type: Boolean,
      default: false,
      require: true
    },
    data: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        DataDictionary({
          pageIndex: 1,
          pageSize: 99999,
          groupCode: 3
        }).then(res => {
          if (res.code === 0) {
            this.remarkList = [ ...res.result.list ]
            this.sourceRemarkList = [ ...res.result.list ]
          }
        })
      }
    }
  },
  methods: {
    cfm () {
      if (!this.remark) return this.$Message.error('请输入驳回原因！')
      ApprovalBillFlow({
        linkCheckId: [this.data.flowCheckId],
        updateAction: 1,
        suggestions: this.remark
      }).then(res => {
        if (res.code === 0) {
          this.$Message.info(res.msg || '操作成功！')
          if (!this.sourceRemarkList.find(item => item.key === this.remark)) {
            AddDataDictionary({
              key: this.remark,
              value: this.remark,
              remark: '合同付款驳回自动生成',
              groupCode: 3
            })
          }
          this.cancel()
          this.$emit('getlist')
        }
      })
    },
    cancel () {
      this.remark = ''
      this.remarkList = []
      this.sourceRemarkList = []
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.row {
  margin-bottom: 10px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  &:last-child {
    margin-bottom: 0;
  }
  .col {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .value {
      flex: 1;
      overflow: hidden;
    }
  }
}
/deep/ .ivu-modal-mask {
  background-color: rgba(55, 55, 55, 0.2);
}
</style>
