<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    width="750px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="closeModel"
  >
    <el-card style="margin-top: -25px">
      <el-row :gutter="10">
        <!-- <el-col :span="24" v-if="status && isDetail !== '003'">
          <step-bar :dstatus="status"></step-bar>
        </el-col> -->
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="24">
                <el-form-item label="原发票编号" prop="relaNo">
                  <el-input
                  @click="openInvoicDialog"
                    v-model="ruleForm.relaNo"
                    placeholder="如INV0000000000000000"
                    :disabled="isDetail !== '003'"
                  /> </el-form-item
              ></el-col>
              <el-col :span="24">
                <el-form-item label="退票类型" prop="backType">
                  <el-radio-group
                    v-model="ruleForm.backType"
                    :disabled="isDetail !== '003'"
                  >
                    <el-radio :label="'1'">原发票</el-radio>
                    <el-radio :label="'2'">红冲发票</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row v-if="isDetail !== '003'">
              <el-col :span="24">
                <el-form-item size="mini" label="原认领企业编号">
                  <el-input v-model="ruleForm.customerNo" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item size="mini" label="原认领企业">
                  <el-input v-model="ruleForm.companyName" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item size="mini" label="退票总金额">
                  <el-input v-model="ruleForm.refund_fee" disabled />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" v-if="isDetail !== '003'">
          <el-form
            ref="examForm"
            :model="examForm"
            status-icon
            :rules="examThis"
            label-width="110px"
            class="demo-examForm"
          >
            <el-form-item label="审核状态" prop="backType">
              <el-radio-group v-model="examForm.backType">
                <el-radio :label="'1'" disabled>通过</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="isDetail === '003'"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button v-else type="primary" @click="submitExamForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">关 闭</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/InvoiceSales/returnTicket";
import resToken from "@/mixins/resToken";
export default {
  name: "returnTicket",
  props: ["showModel", "id", "isDetail", "dstatus"],
  mixins: [resToken],
  // components: {
  //   "step-bar": () => import("./stepBar"),
  // },

  data() {
    return {
      roleList: [],
      loading: false,
      status: "",
      title: "添加退票申请",
      showModelThis: this.showModel,
      ruleForm: {
        relaNo: "", // 退票申请
        backType: "1",
        type: "2",
      },
      rulesThis: this.rules,
      rules: {
        relaNo: [
          {
            required: true,
            message: "请输入原发票编号",
            trigger: "blur",
          },
        ],
        backType: [
          {
            required: true,
            message: "请选择退款类型",
            trigger: "change",
          },
        ],
      },
      examForm: {
        relaNo: "", // 退票ID
        backType: "1",
      },
      examThis: this.examRules,
      examRules: {
        backType: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.status = "";
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      console.log(this.dstatus);
      this.loading = true;
      if (this.isDetail === "003") {
        this.title = "添加退票申请";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        this.rulesThis = {};
        this.examThis = this.examRules;
        // console.log(this.examThis);
        if (this.isDetail === "028") {
          this.title = "退票业务审核";
        } else if (this.isDetail === "029") {
          this.title = "退票财务审核";
        } else {
          this.title = "退票财务核销发票";
        }
        await this.resetForm();
        await this.initData();
      }
      this.status = this.dstatus;
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ refundNo: this.id });
      if (res && res.code === 0 && res.data) {
        this.ruleForm = res.data;
        this.ruleForm.backType = this.ruleForm.back_type;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            relaNo: "", // 退票申请
            backType: "1",
            type: "2",
          };
        }
        if (this.$refs.examForm) {
          this.$refs.examForm.resetFields();
          this.$refs.examForm.clearValidate();
          this.examForm = {
            backType: "1",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const obj = JSON.parse(JSON.stringify(this.ruleForm));
          const model = {
            relaNo: obj.relaNo, // 退票申请
            type: "2",
            backType: obj.backType,
          };
          let res = await asyncRequest.add(model);

          this.loading = false;
          if (res && res.code === 0) {
            const title = "添加成功！";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async submitExamForm() {
      await this.$refs.examForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const obj = JSON.parse(JSON.stringify(this.examForm));
          const model = {
            refundNo: this.id,
            status:
              this.isDetail === "028"
                ? "2"
                : this.isDetail === "029"
                ? "3"
                : "4",
            remark: "",
          };
          let res = await asyncRequest.status(model);
          this.loading = false;
          if (res && res.code === 0) {
            const title = "审核成功！";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.returnTicket {
}
</style>
