<template>
  <div id="element-main">
    <Form ref="formModel" :model="formModel" :rules="formRule" label-position="top">
      <FormItem label="请输入订单反取消原因" prop="reason">
        <Input v-model="formModel.reason" placeholder="请输入..." type="textarea" :rows="3"></Input>
      </FormItem>
    </Form>
    <footer>
      <Button type="fcdefault" @click="closeDialog()">取消</Button>
      <Button type="posdefault" @click="submit()" style="margin-left: 8px;">确定</Button>
    </footer>
  </div>
</template>

<script lang="js">
  import { showModalFcSuccess, showModalFcError } from '@yunque/service/modules/utils';
  import { common } from '@yunque/service/index';

  export default {
    name: 'OMSOnlineOrderCancelOrderRollback',
    props: {
      idArray: { // 获取ID用于多选
        type: Array,
        default: () => []
      },
    },
    data() {
      return {
        formModel: {
          reason: '' // 反取消原因
        },
        formRule: {
          reason: [
            { required: true, message: '请输入订单反取消原因', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      /**
       * 关闭当前弹窗
       * @param refreshGrid 是否刷新列表
       */
      closeDialog(refreshGrid = false) {
        this.$emit('closeActionDialog', refreshGrid);
      },
      /**
       * 确定
       */
      submit() {
        this.$refs.formModel.validate(async (valid) => {
          if (!valid) {
            return;
          }

          try {
            const param = {
              ids: this.idArray,
              param: {
                T_OMSONLINEORDER: [{
                  REASON: this.formModel.reason
                }]
              },
              tableName: this.$route.params.tableName,
              actionName: 'T_OMSONLINEORDER_订单反取消_SP',
              isContainsParam: true
            };
            const res = await common.exeAction(param);
            if (res === null) {
              return;
            }

            if (res.code !== 0) {
              showModalFcError('友情提示', res.message);
            } else {
              this.closeDialog(true);
              showModalFcSuccess('友情提示', res.message);
            }
          } catch (e) {
            console.error(e);
            showModalFcError('错误', `系统出现异常，请联系管理员（${e}）`);
          }
        });
      }
    }
  };
</script>

<style scoped>
  #element-main {
    width: 400px;
  }

  footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
  }
</style>
