<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-show="status && isDetail !== '003'">
          <step-bar :dstatus="status" :time="time"></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-form-item label="原资金编号" prop="relaNo">
              <el-input
                v-model="ruleForm.relaNo"
                placeholder="如TRA000000000000-000"
                :disabled="isDetail !== '003'"
              />
            </el-form-item>
            <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="status">
              <el-radio-group v-model="examForm.status" @change="radioChange">
                <el-radio
                  :label="item.code"
                  v-for="(item, i) in statusList"
                  :key="item.code + i"
                  >{{ item.name }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="审核备注" prop="remark">
              <el-input
                type="textarea"
                placeholder="请输入审核备注"
                maxlength="250"
                v-model="examForm.remark"
                show-word-limit
              ></el-input>
            </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/refund";
import resToken from "@/mixins/resToken";
export default {
  name: "refund",
  props: ["showModel", "id", "isDetail", "dstatus"],
  mixins: [resToken],
  // components: {
  //   "step-bar": () => import("./stepBar"),
  // },
  data() {
    return {
      time: 0,
      roleList: [],
      loading: false,
      title: "添加退款申请",
      showModelThis: this.showModel,
      status: "",
      ruleForm: {
        relaNo: "", // 退款申请
        backType: "0",
      },
      resData: null,
      rulesThis: this.rules,
      statusList1: [
        { code: "2", name: "通过" },
        { code: "5", name: "驳回" },
      ],
      statusList2: [
        { code: "3", name: "通过" },
        { code: "6", name: "驳回" },
      ],
      statusList: [],
      rules: {
        relaNo: [
          {
            required: true,
            message: "请输入原资金编号",
            trigger: "blur",
          },
        ],
        backType: [
          {
            required: true,
            message: "请选择退款类型",
            trigger: "change",
          },
        ],
      },
      examForm: {
        relaNo: "", // 退款ID
        status: "1",
        remark: "",
      },
      examThis: this.examRules,
      examRules: {
        status: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: false,
            message: "请输入审核备注",
            trigger: "bulr",
          },
        ],
      },
    };
  },
  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.id);

      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 = "退款业务审核";
          this.statusList = this.statusList1;
        } else {
          this.title = "退款财务审核";
          this.statusList = this.statusList2;
        }
        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;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    radioChange() {
      if (this.examForm.status === this.statusList[0].code) {
        this.examThis.remark[0].required = false;
      } else {
        this.examThis.remark[0].required = true;
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            relaNo: "", // 退款申请
            backType: "0",
          };
        }
        if (this.$refs.examForm) {
          this.$refs.examForm.resetFields();
          this.$refs.examForm.clearValidate();
          this.examForm = {
            status: this.statusList[0].code,
            remark: "",
          };
          this.radioChange();
        }
      });
    },

    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: obj.status,
            remark: obj.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;
        }
      });
    },
    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, // 退款申请
            backType: obj.backType,
            type: "1",
          };
          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;
        }
      });
    },
  },
};
</script>

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