<template>
  <el-dialog
    v-loading="loading"
    title="财务开票"
    :center="true"
    align="left"
    top="8vh"
    width="1024px"
    :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="height: 700px; margin-top: -25px">
      <appProcess :status="status"></appProcess>

      <el-collapse v-model="activeNames" accordion>
        <el-collapse-item title="发票申请详情" name="1">
          <addview
            :id="id"
            :newTime="newTime"
            :show-model="showModel"
            :is-detail="isDetail"
            @refreshAll="refreshAll"
          ></addview>
        </el-collapse-item>
        <el-collapse-item title="财务开票" name="2">
          <el-row>
            <el-col :span="24" style="padding-right: 10px">
              <el-form
                ref="ruleForm"
                :model="ruleForm"
                status-icon
                :rules="rules"
                label-width="110px"
                class="demo-ruleForm"
              >
                <el-form-item label="发票回执" prop="inv_info">
                  <el-input
                    type="textarea"
                    maxlength="2000"
                    show-word-limit
                    v-model="ruleForm.inv_info"
                    :autosize="{ minRows: 10, maxRows: 10 }"
                    placeholder="请将鼠标放入输入框，等待扫码枪出结果"
                  ></el-input>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="24" style="padding-right: 10px; text-align: right">
              <el-button type="primary" @click="submitForm">保 存 </el-button>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-card>
  </el-dialog>
</template>
<script>
import addview from "./addview";
import appProcess from "./appProcess";
import asyncRequest from "@/apis/service/InvoiceSales/invoiceApply";
import resToken from "@/mixins/resToken";
export default {
  name: "invoiceApply",
  props: ["showModel", "id", "isDetail", "status", "invNo"],
  mixins: [resToken],
  components: {
    addview,
    appProcess,
  },
  data() {
    return {
      options: [
        {
          value: "3",
          label: "通过",
        },
        {
          value: "2",
          label: "驳回",
        },
      ],
      activeNames: "2",
      loading: false,
      newTime: 0,
      showModelThis: this.showModel,
      ruleForm: {
        status: "4", // 真实姓名
        remark: "",
        inv_info: "",
      },
      rules: {
        inv_info: [
          {
            required: true,
            message: "请录入发票回执信息",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      this.loading = true;
      this.activeNames = "2";
      this.changea();
      this.loading = false;
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
      this.$refs.ruleForm.validateField("inv_info");
    },
    refreshAll() {
      this.showModelThis = false;
      this.$emit("refresh");
    },
    seChange() {
      if (this.ruleForm.status === "2") {
        this.rules.remark[0].required = true;
      } else {
        this.rules.remark[0].required = false;
      }
    },
    changea() {
      this.newTime = new Date().valueOf();
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { status, remark, inv_info } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            invNo: this.invNo,
            status: status,
            remark: remark,
            inv_info: inv_info,
          };
          const res = await asyncRequest.status(model);
          if (res && res.code === 0) {
            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>
.invoiceApply {
}
</style>
