<template>
  <el-row :gutter="10" v-loading="loading">
    <el-col :span="24" v-if="step === 1">
      <el-form
        ref="ruleForm1"
        :model="ruleForm1"
        status-icon
        :rules="rulesThis"
        label-width="130px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="14">
            <el-form-item label="申请企业" prop="companyNo">
              <el-select
                v-model="ruleForm1.companyNo"
                multiple
                filterable
                remote
                :disabled="disableds"
                :multiple-limit="1"
                reserve-keyword
                :size="searchSize"
                style="width: 100%"
                placeholder="申请企业"
                :remote-method="remoteMethod($event, 1)"
                :loading="selectLoading"
                @change="selectChange($event, 1)"
              >
                <el-option
                  v-for="item in activeOptions"
                  :key="item.companyNo"
                  :label="item.companyName"
                  :value="item.companyNo"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="开票企业类型" prop="radio">
              <el-radio-group
                @change="radioGroupChange"
                v-model="ruleForm1.radio"
              >
                <el-radio :label="'1'">使用申请企业</el-radio>
                <el-radio :label="'2'">其他企业</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="financeModel">
            <el-form-item label="开票购买方企业" prop="companyNo2">
              <el-select
                v-model="ruleForm.companyNo2"
                multiple
                filterable
                remote
                :multiple-limit="1"
                reserve-keyword
                :size="searchSize"
                style="width: 100%"
                placeholder="开票购买方企业"
                :remote-method="remoteMethod($event, 2)"
                :loading="selectLoading"
                @change="selectChange($event, 2)"
              >
                <el-option
                  v-for="item in activeOptions2"
                  :key="item.companyNo"
                  :label="item.companyName"
                  :value="item.companyNo"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="发票抬头" prop="invoice_title">
                <el-input
                  :disabled="disableds"
                  placeholder="请输入发票抬头"
                  v-model="ruleForm1.invoice_title"
                >
                </el-input
              ></el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纳税登记证号" prop="invoice_code">
                <el-input
                  :disabled="disableds"
                  placeholder="请输入纳税登记证号"
                  v-model="ruleForm1.invoice_code"
                >
                </el-input
              ></el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="开户银行名称" prop="invoice_bank">
                <el-input
                  :disabled="disableds"
                  placeholder="请输入开户银行名称"
                  v-model="ruleForm1.invoice_bank"
                >
                </el-input
              ></el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开户银行账号" prop="invoice_bankNo">
                <el-input
                  :disabled="disableds"
                  placeholder="请输入开户银行账号"
                  v-model="ruleForm1.invoice_bankNo"
                >
                </el-input
              ></el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纳税登记地址" prop="invoice_addr">
                <el-input
                  :disabled="disableds"
                  placeholder="请输入纳税登记地址"
                  v-model="ruleForm1.invoice_addr"
                >
                </el-input
              ></el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="invoice_mobile">
                <el-input
                  :disabled="disableds"
                  placeholder="请输入联系电话"
                  v-model="ruleForm1.invoice_mobile"
                >
                </el-input
              ></el-form-item>
            </el-col>
          </el-col>

          <el-col :span="12">
            <el-form-item label="发票类型" prop="inv_type">
              <el-select
                v-model="ruleForm1.inv_type"
                clearable
                :disabled="disableds"
                placeholder="请选择发票类型"
                style="width: 100%"
              >
                <el-option
                  v-for="item in invioceType"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票类型" prop="type">
              <el-select
                v-model="ruleForm1.type"
                :disabled="isDetail !== '003'"
                placeholder="请选择开票类型"
                style="width: 100%"
                @change="changetype"
              >
                <el-option
                  v-for="item in billType"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="ruleForm1.type === 2">
            <el-form-item label="开票总金额(元)" prop="total">
              <el-input-number
                step-strictly
                controls-position="right"
                style="width: 100%"
                :disabled="disableds"
                v-model="ruleForm1.total"
                :precision="2"
                :step="0.01"
                :min="0"
                :max="1000000000000"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="ruleForm1.type === 2">
            <el-form-item label="备注" prop="remark">
              <el-input
                :disabled="disableds"
                type="textarea"
                maxlength="250"
                show-word-limit
                :autosize="{ minRows: 6, maxRows: 6 }"
                placeholder="请输入商品明细要求： 商品分类，型号等信息"
                v-model="ruleForm1.remark"
              >
              </el-input></el-form-item
          ></el-col>

          <el-col :span="24">
            <el-row>
              <el-col :span="20" v-if="status === '2'">
                <el-alert
                  title="财务驳回理由："
                  type="error"
                  :closable="false"
                  :description="exam_remark"
                  show-icon
                >
                </el-alert>
              </el-col>
              <el-col :span="status === '2' ? 4 : 24" style="text-align: right">
                <el-button
                  type="primary"
                  v-if="ruleForm1.type !== 2"
                  @click="submitForm1"
                  >下一步<i class="el-icon-arrow-right el-icon--right"></i
                ></el-button>
                <el-button
                  v-if="!disableds && ruleForm1.type === 2"
                  type="primary"
                  @click="submitForm1"
                  >保 存
                </el-button>
              </el-col></el-row
            >
          </el-col>
        </el-row>
      </el-form>
    </el-col>
    <el-col :span="24" v-else>
      <el-row>
        <el-col
          :span="isDetail !== '003' ? 24 : 21"
          style="padding: 0 12px 8px 0"
        >
          <el-alert
            title="特别提示：请注意商品未核销数量/发票未核销金额。若此次核销的数量等于未核销数量，不论此次总金额核销后，发票未核销金额是否为0，都不能再使用该商品核销！发票核销总金额同理。"
            type="error"
            :closable="false"
            show-icon
          >
          </el-alert>
        </el-col>
        <el-col
          v-if="isDetail === '003'"
          :span="3"
          class="tr"
          style="padding: 11px 0 8px 0"
          ><el-button type="primary" class="fr" size="small" @click="openModal"
            >添加产品确认单</el-button
          ></el-col
        >
        <el-col :span="24">
          <el-table
            style="width: 100%"
            :size="'mini'"
            :data="tableData"
            stripe
            highlight-current-row
            max-height="380"
            border
          >
            <el-table-column
              label="产品编号"
              prop="goodNo"
              show-overflow-tooltip
              width="125"
            >
            </el-table-column>
            <el-table-column
              label="商品名称"
              prop="goodname"
              show-overflow-tooltip
              min-width="120"
            >
            </el-table-column>
            <el-table-column
              label="开票商品名"
              width="160"
              prop="remark"
              show-overflow-tooltip
            />
            <el-table-column
              label="品牌"
              prop="brand"
              width="80"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="单位"
              prop="unit"
              width="80"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column label="单价" width="90" prop="price" />
            <el-table-column label="数量" align="center">
              <el-table-column label="总计" width="70" prop="num" />
              <el-table-column label="已核销" width="70" prop="inv_num" />
              <el-table-column label="未核销" width="70" prop="over_num" />
              <el-table-column label="此次核销" width="70" prop="good_num" />
            </el-table-column>

            <el-table-column
              label="总价"
              width="105"
              prop="total"
              show-overflow-tooltip
            />
            <el-table-column label="关联发票信息" align="center">
              <el-table-column
                label="已核销金额"
                width="100"
                prop="ainv_fee"
                show-overflow-tooltip
              />
              <el-table-column
                label="未核销金额"
                width="100"
                prop="winv_fee"
                show-overflow-tooltip
              />
              <el-table-column
                label="此次核销金额"
                width="100"
                prop="total_fee"
              />
            </el-table-column>

            <el-table-column
              fixed="right"
              label="操作"
              width="80"
              v-if="isDetail === '003' || isDetail === '005'"
            >
              <template slot-scope="scope">
                <el-tooltip
                   
                  effect="dark"
                  v-if="isDetail === '003' || isDetail === '005'"
                  content="编辑"
                  placement="top"
                >
                  <i
                    v-if="isDetail === '003' || isDetail === '005'"
                    class="el-icon-edit tb-icon"
                    @click="editRow(scope.$index)"
                  ></i>
                </el-tooltip>
                <el-tooltip
                   
                  effect="dark"
                  content="删除"
                  v-if="isDetail === '003'"
                  placement="top"
                >
                  <i
                    v-if="isDetail === '003'"
                    class="el-icon-delete tb-icon"
                    @click="deleteRow(scope.$index, tableData)"
                  ></i>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="24" style="padding: 10px 0 0 0">
          <el-input
            :disabled="disableds"
            type="textarea"
            maxlength="250"
            show-word-limit
            :autosize="{ minRows: 3, maxRows: 3 }"
            placeholder="请输入其他要求"
            v-model="remark"
          />
        </el-col>
        <el-col :span="24" style="padding: 10px 0 0 0">
          <el-row>
            <el-col :span="19" v-if="status === '2'">
              <el-alert
                title="财务驳回理由："
                type="error"
                :closable="false"
                :description="exam_remark"
                show-icon
              >
              </el-alert>
            </el-col>
            <el-col :span="status === '2' ? 5 : 24" class="tr">
              <el-button type="primary" @click="step = 1"
                >上一步<i class="el-icon-arrow-right el-icon--right"></i
              ></el-button>
              <el-button v-if="!disableds" type="primary" @click="submitForm"
                >保 存
              </el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <order-change-dialog
        :id="orderModelId"
        :trade_status="''"
        :inv_status="'4'"
        :show-model="showOrderModel"
        @refresh="orderRefresh"
        @cancel="showOrderModel = false"
      />
      <good-change-dialog
        :show-model="showGoodChangeModel"
        :item="activeItem"
        :is-detail="isDetail"
        @refresh="goodChangeEnd"
        @cancel="showGoodChangeModel = false"
      />
    </el-col>
  </el-row>
</template>
<script>
import asyncRequest from "@/apis/service/InvoiceSales/invoiceApply";
import orderChangeDialog from "@/components/changeDialog/order";
import resToken from "@/mixins/resToken";
import goodChangeDialog from "./goodChangeDialog.vue";
import { isMobile, isLicense, isPhone } from "@/utils/validate";
export default {
  name: "invoiceApply",
  props: ["showModel", "id", "isDetail", "newTime", "invNo", "status"],
  components: {
    orderChangeDialog,
    goodChangeDialog,
  },
  mixins: [resToken],
  data() {
    const validateLicense = (rule, value, callback) => {
      if (value !== "") {
        if (!isLicense(value)) {
          callback(new Error("纳税登记证号不正确！"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入纳税登记证号！"));
      }
    };

    const validatemobile = (rule, value, callback) => {
      if (value !== "") {
        if (isPhone(value) || isMobile(value)) {
          callback();
        } else {
          callback(new Error("联系电话格式不正确！"));
        }
      } else {
        callback(new Error("请输入联系电话！"));
      }
    };
    return {
      disableds: false,
      financeModel: false,
      orderModelId: "",
      showOrderModel: false,
      cwRes: null,
      tableData: [],
      activeIndex: 0,
      activeItem: null,
      showGoodChangeModel: false,
      remark: "",
      exam_remark: "",
      step: 1,
      selectLoading: false,
      loading: false,
      title: "添加发票申请",
      invioceType: [
        {
          id: 1,
          name: "增值税专用发票",
        },
        {
          id: 2,
          name: "增值税普通发票",
        },
        {
          id: 3,
          name: "增值税专用发票-电子发票",
        },
        {
          id: 4,
          name: "增值税普通发票-电子发票",
        },
      ],
      billType: [
        {
          id: 1,
          name: "订单开票",
        },
        {
          id: 2,
          name: "资金开票",
        },
      ],
      formValue: {
        company: "", // 账户
        page: 1, // 页码
        size: 100, // 每页显示条数
      },
      showModelThis: this.showModel,
      select: "1",
      activeOptions: [],
      actionList: [],
      ruleForm1: {
        companyNo: [],
        companyNo2: [],
        inv_type: "",
        radio: "1", //开票企业类型
        invoice_title: "",
        invoice_code: "",
        invoice_bank: "",
        invoice_bankNo: "",
        invoice_addr: "",
        invoice_mobile: "",
        type: "",
        total: 0,
        remark: "",
      },

      rulesThis: this.rules1,
      rules1: {
        companyNo: [
          { required: true, message: "请选择申请企业", trigger: "change" },
        ],
        companyNo2: [
          { required: true, message: "请选择申请企业", trigger: "change" },
        ], 
        radio: [
          { required: true, message: "请选择开票企业类型", trigger: "change" },
        ],
        userid: [
          { required: true, message: "请选择数据源用户", trigger: "change" },
        ],
        touserid: [
          { required: true, message: "请选择数据共享用户", trigger: "change" },
        ],
        inv_type: [
          { required: true, message: "请选择发票类型", trigger: "change" },
        ],
        type: [
          { required: true, message: "请选择开票类型", trigger: "change" },
        ],
        invoice_title: [
          { required: true, message: "请输入发票抬头", trigger: "blur" },
        ],
        invoice_code: [
          { required: true, validator: validateLicense, trigger: "blur" },
        ],
        invoice_bank: [
          { required: true, message: "请输入开户银行名称", trigger: "blur" },
        ],
        invoice_bankNo: [
          { required: true, message: "请输入开户银行账号", trigger: "blur" },
        ],
        invoice_addr: [
          { required: true, message: "请输入纳税登记地址", trigger: "blur" },
        ],
        invoice_mobile: [
          { required: true, validator: validatemobile, trigger: "blur" },
        ],
        total: [
          { required: false, message: "请输入发票总额", trigger: "blur" },
        ],
        remark: [{ required: false, message: "请输入备注", trigger: "blur" }],
      },

      dataForm: {
        name: "", // 业务员名字
        // username: "", //账号
        status: "", //
        // page: 1, // 页码
        // size: 5, // 每页显示条数
      },
      activeOptions: [],
      groupallList: [],
      multipleSelection: [],
    };
  },
  watch: {
    newTime: function (old, val) {
      if (old !== val) {
        this.initForm();
      }
    },
  },
  mounted() {
    this.initForm();
  },
  methods: {
    editRow(index) {
      this.activeIndex = index;
      this.activeItem = this.tableData[index];
      this.showGoodChangeModel = true;
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    radioGroupChange(val) {
      if (val === "2") {
        console.log(val);
        this.financeModel = true;
      } else {
        this.financeModel = false;
      }
    },
    goodChangeEnd(e) {
      console.log(e);
      console.log(this.activeIndex);

      if (e) {
        const { remark, total_fee, good_num } = e;
        this.tableData[this.activeIndex].remark = remark;
        this.tableData[this.activeIndex].total_fee = total_fee;
        this.tableData[this.activeIndex].good_num = good_num;
      }
    },
    text_fun(e, index, key) {
      let p = e.target.parentElement;
      // console.log(index, key);
      this.tableData.map((v2) => {
        v2.remark_type = "text";
        v2.good_num_type = "text";
        v2.total_fee_type = "text";
        return v2;
      });
      this.tableData[index][key] = "input";
      this.$nextTick((v) => {
        if (p && p.firstElementChild && p.firstElementChild.firstElementChild) {
          p.firstElementChild.firstElementChild.focus();
        }
      });
    },
    input_fun(index, key) {
      // console.log(index, key);
      this.tableData.map((v2) => {
        v2.remark_type = "text";
        v2.good_num_type = "text";
        v2.total_fee_type = "text";
        return v2;
      });
      this.tableData[index][key] = "text";
    },
    orderRefresh(e) {
      let arr = JSON.parse(JSON.stringify(this.tableData));
      e.forEach((v1) => {
        let fi = arr.findIndex(
          (v2) => v2.sequenceNo === v1.sequenceNo && v2.orderNo === v1.orderNo
        );
        if (fi > -1) {
          arr.splice(fi, 1);
        }
      });
      this.tableData = [].concat(arr).concat(e);
    },
    // 新建/编辑/详情
    openModal() {
      let arr = JSON.parse(JSON.stringify(this.ruleForm1.companyNo));
      this.orderModelId = arr.join(",");
      this.showOrderModel = true;
    },
    closeModel() {
      console.log("closeModel!!");
    },
    // handleSelectionChange(index, val) {
    //   console.log(index);
    //   console.log(val);
    //   this.multipleSelection.push(...val)
    //   console.log(this.multipleSelection);
    // },
    async initForm() {
      this.step = 1;
      this.remark = "";
      this.exam_remark = "";
      this.loading = true;
      this.cwRes = null;
      console.log(this.status);
      this.disableds = this.isDetail !== "003" && this.isDetail !== "005";
      this.tableData = [];
      if (this.isDetail === "003") {
        this.title = "添加发票申请";
        this.rulesThis = this.rules1;
        await this.resetForm();
      } else {
        if (this.isDetail == "005") {
          this.title = "发票申请详情";
          this.rulesThis = {};
        } else if (this.isDetail === "007") {
          this.title = "修改发票申请";
          this.rulesThis = this.rules1;
        }
        await this.resetForm();
        await this.initData();
      }
      this.loading = false;
    },
    async selectChange(e, type) {
      let arr = JSON.parse(
        JSON.stringify(
          type === 1 ? this.ruleForm1.companyNo : this.ruleForm1.companyNo2
        )
      );
      let code = arr.join(",");
      if (code === "") {
        this.cwRes = null;
      } else {
        await this.getCaiwu(code, type);
      }
    },
    async getCaiwu(code, type) {
      const res = await asyncRequest.cwlist({ companyNo: code });
      if (res && res.code === 0 && res.data) {
        let data = res.data;
        this.cwRes = {
          id: "",
          companyName: "",
          invoice_title: "",
          invoice_code: "",
          invoice_bank: "",
          invoice_bankNo: "",
          invoice_addr: "",
          invoice_mobile: "",
        };
        for (let k in this.cwRes) {
          this.cwRes[k] = data[k] ? data[k] + "" : "";
          this.ruleForm1[k] = data[k] ? data[k] + "" : "";
        }
        console.log(data);
        //this.cwRes =
        // for (let k in this.cwRes) {
        //   this.cwRes[k] = this.cwRes[k] ? "" : this.cwRes[k] + "";
        // }
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.cwRes = null;
        this.$message.warning(res.message);
      }
    },

    async remoteMethod(query, type) {
      this.selectLoading = true;
      if (query !== "") {
        this.formValue.company = query;
        const res = await asyncRequest.clist(this.formValue);
        if (res && res.code === 0 && res.data && res.data.list) {
          if (type === 1) {
            this.activeOptions = [];
            this.activeOptions = res.data.list;
          } else {
            this.activeOptions2 = [];
            this.activeOptions2 = res.data.list;
          }
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        }else{
          this.$message.warning(res.message);
        }
      } else {
        if (type === 1) {
          this.activeOptions = [];
        } else {
          this.activeOptions2 = [];
        }
      }
      this.selectLoading = false;
    },
    async initData() {
      // this.loading = true;
      const res = await asyncRequest.detail({ id: this.id });
      // this.loading = false;
      if (
        res &&
        res.code === 0 &&
        res.data &&
        res.data.customer &&
        res.data.customer.companyNo
      ) {
        // companyNo: "KH202104271201"
        const data = res.data;
        const customer = res.data.customer;
        this.ruleForm1 = {
          companyNo: [customer.companyNo],
          inv_type: data.inv_type ? parseInt(data.inv_type) : "",
          type: data.type_check ? parseInt(data.type_check) : "",
          total: data.inv_value,
          remark: data.remark,
          invoice_title: "",
          invoice_code: "",
          invoice_bank: "",
          invoice_bankNo: "",
          invoice_addr: "",
          invoice_mobile: "",
        };
        this.remark = data.remark;
        this.exam_remark = data.exam_remark;
        this.cwRes = {
          id: "",
          companyName: "",
          invoice_title: "",
          invoice_code: "",
          invoice_bank: "",
          invoice_bankNo: "",
          invoice_addr: "",
          invoice_mobile: "",
        };
        for (let k in this.cwRes) {
          this.cwRes[k] = customer[k] ? customer[k] + "" : "";
          this.ruleForm1[k] = customer[k] ? customer[k] + "" : "";
        }
        console.log(this.ruleForm1);
        if (!this.disableds) {
          this.changetype(this.ruleForm1.type);
        }
        console.log(res.data.order.good);
        let good =
          res.data.order &&
          res.data.order.good &&
          res.data.order.good.length > 0
            ? JSON.parse(JSON.stringify(res.data.order.good))
            : [];
        good.map((v1) => {
          v1.sequenceNo = v1.goodNo;
          v1.goodname = v1.good_name;
          v1.over_num = parseInt(v1.num) - parseInt(v1.inv_num);
          return v1;
        });
        this.tableData = good;

        await this.remoteMethod(data.customer.companyName);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }else{
        this.$message.warning(res.message);
      }
      console.log(this.ruleForm1);
    },
    changetype(val) {
      console.log(val, "1234");
      if (this.rulesThis && this.rulesThis.total && this.rulesThis.remark) {
        if (val === 1) {
          this.rulesThis.total[0].required = false;
          this.rulesThis.remark[0].required = false;
        } else {
          this.rulesThis.total[0].required = true;
          this.rulesThis.remark[0].required = true;
        }
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm1) {
          this.$refs.ruleForm1.resetFields();
          this.$refs.ruleForm1.clearValidate();
          this.cwRes = null;
          this.ruleForm1 = {
            companyNo: [],
            inv_type: "",
            type: "",
            total: 0,
            remark: "",
            radio: "1", //开票企业类型
            invoice_title: "",
            invoice_code: "",
            invoice_bank: "",
            invoice_bankNo: "",
            invoice_addr: "",
            invoice_mobile: "",
          };
        }
      });
    },
    async submitForm1() {
      await this.$refs.ruleForm1.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm1.type === 2) {
            await this.mAddOrUpdate();
          } else {
            this.step = 2;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    async mAddOrUpdate() {
      if (!this.loading) {
        this.loading = true;
        const obj = JSON.parse(JSON.stringify(this.ruleForm1));
        const model = {
          invNo: this.invNo,
          companyNo: obj.companyNo.join(), //企业
          inv_type: obj.inv_type + "", //发票类型
          total: obj.total + "", //金额
          remark: obj.remark + "",
          invoice_title: obj.invoice_title,
          invoice_code: obj.invoice_code,
          invoice_bank: obj.invoice_bank,
          invoice_bankNo: obj.invoice_bankNo,
          invoice_addr: obj.invoice_addr,
          invoice_mobile: obj.invoice_mobile,
        };
        let res = {};
        if (this.id === "add") {
          delete model["invNo"];
          res = await asyncRequest.madd(model);
        } else {
          res = await asyncRequest.mupdate(model);
        }
        this.loading = false;
        if (res && res.code === 0) {
          const title = this.id === "add" ? "添加成功" : "修改成功";
          this.$notify.success({
            title,
            message: "",
          });
          this.$emit("refreshAll");
          //   this.showModelThis = false;
          // 刷新
          //   this.$emit("refresh");
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        }else{
          this.$message.warning(res.message);
        }
      }
    },

    async submitForm() {
      if (!this.loading) {
        // remark;

        if (this.tableData.length === 0) {
          this.$message.warning("请选择确认单商品");
          return;
        }
        // if (this.ruleForm.togroupid.length === 0 && this.select !== "1") {
        //   this.$message.warning("请选择数据共享用户组");
        //   return;
        // }
        // if (
        //   this.select === "1" &&
        //   this.ruleForm.userid[0] === this.ruleForm.touserid[0]
        // ) {
        //   this.$message.warning("源用户不能与分享用户一致！");
        //   return;
        // }

        const obj = JSON.parse(JSON.stringify(this.ruleForm1));
        const list = JSON.parse(JSON.stringify(this.tableData));
        let isok = true;
        let remark = true;
        const model = {
          invNo: this.invNo,
          companyNo: obj.companyNo.join(), //企业
          inv_type: obj.inv_type + "", //发票类型
          invoice_title: obj.invoice_title,
          invoice_code: obj.invoice_code,
          invoice_bank: obj.invoice_bank,
          invoice_bankNo: obj.invoice_bankNo,
          invoice_addr: obj.invoice_addr,
          invoice_mobile: obj.invoice_mobile,
          good: [],
          remark: this.remark,
        };
        list.forEach((v1) => {
          if (v1.good_num === 0) {
            isok = false;
          }
          if (v1.total_fee === 0) {
            isok = false;
          }
          let str = v1.remark.replace(/\s*/g, "");
          if (str === "") {
            remark = false;
          }
          let item = {
            sequenceNo: v1.sequenceNo,
            remark: str,
            total_fee: v1.total_fee,
            num: v1.good_num,
          };
          model.good.push(item);
        });
        if (!isok) {
          this.$message.error("商品数量/商品总额不能为0！");
          return;
        }
        if (!remark) {
          this.$message.error("开票商品名不能为空！");
          return;
        }
        this.loading = true;
        let res = {};
        if (this.id === "add") {
          delete model["invNo"];
          res = await asyncRequest.oadd(model);
        } else {
          res = await asyncRequest.oupdate(model);
        }
        this.loading = false;
        if (res && res.code === 0) {
          const title = this.id === "add" ? "添加成功" : "修改成功";
          this.$notify.success({
            title,
            message: "",
          });
          this.$emit("refreshAll");
          //   this.showModelThis = false;
          //   // 刷新
          //   this.$emit("refresh");
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        }else{
          this.$message.warning(res.message);
        }
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.invoiceApply {
  .fp-title {
    float: left;
    width: 100px;
    text-align: right;
    line-height: 46px;
    box-sizing: border-box;
    color: #99a9bf;
    padding: 0 10px 0 0;

    font-size: 14px;
  }
  .fp-name {
    width: calc(100% - 125px);
    font-size: 14px;
    float: left;
    padding: 10px 0;
    line-height: 26px;
  }
}
</style>
