<template>
  <el-dialog
    :title="'竞价单选定商品下单'"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    append-to-body
  >
    <el-card style="margin-top: -20px">
      <el-row>
        <el-col :span="24">
          <show-data-table
            style="margin: 0; padding: 0"
            :newTime="newTime"
            v-if="newTime !== '' && sitem"
            :sitem="sitem"
            border
            :columns="ShowDataTableColumns"
          >
            <template slot="good_name">
              <span>{{ sitem.good_name }}</span>
              <span v-for="(si, sii) in sitem.specinfo" :key="si.specid + sii">
                <span v-if="sii !== 0">--</span
                ><span>{{ si.spec_name }}[{{ si.spec_value_name }}]</span></span
              >
            </template>
            <template slot="is_noble">
              <span v-if="sitem.is_noble">
                {{ sitem.noble_weight }}g--{{ sitem.metal_name }}--{{
                  sitem.is_diff === "1" ? "有" : "无"
                }}工差--{{ sitem.config }}--{{ sitem.other_config }}</span
              >
              <span v-else>非贵金属商品</span>
            </template>
            <template slot="in_num">
              <span>{{ sitem.in_num }}{{ sitem.unit }}</span>
            </template>
          </show-data-table>
        </el-col>
        <el-col :span="24" style="padding: 18px 0 0 0">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            :size="'mini'"
            label-width="55px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="5">
                <el-form-item label="数量" prop="good_num">
                  <!--:min="sitem.in_num"-->
                  <digital-input
                    :values="ruleForm.good_num"
                    :placeholder="'数量'"
                    :min="1"
                    :max="100000000000"
                    :position="'right'"
                    :precision="0"
                    :size="'mini'"
                    :controls="false"
                    :disabled="false"
                    :append="sitem.unit"
                    @reschange="num_change($event, 'good_num')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="单价" prop="sale_price">
                  <digital-input
                    :values="ruleForm.sale_price"
                    :placeholder="'商品单价'"
                    :min="sitem.sale_price"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :disabled="false"
                    :append="'元'"
                    @reschange="num_change($event, 'sale_price')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="总金额" prop="good_num" label-width="70px">
                  <el-input
                    v-model="total_price"
                    placeholder="总金额"
                    disabled
                    maxlength="500"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="发货方式" prop="sendtype" label-width="90px">
                  <el-select
                    v-model="ruleForm.sendtype"
                    placeholder="发货方式"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2" style="text-align: right">
                <el-button
                  :size="'mini'"
                  type="primary"
                  :disabled="loading"
                  @click="submitForm"
                  >保 存
                </el-button>
              </el-col>
              <el-col :span="11">
                <el-form-item label="业务公司" prop="companyNo" label-width="85px">
                  <search-work-company
                    :value="ruleForm.companyNo"
                    :placeholder="'业务公司'"
                    :disabled="false"
                    :size="'mini'"
                    :isDetail="true"
                    @searchChange="company_idsearchChange"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="13">
                <el-form-item label="订单备注" prop="remark" label-width="85px">
                  <el-input
                    v-model="ruleForm.remark"
                    maxlength="500"
                    placeholder="订单备注"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-row>
                  <el-col :span="12"
                    ><el-form-item label="凭证类型" prop="proof_type" label-width="85px">
                      <el-select
                        v-model="ruleForm.proof_type"
                        @change="set_proof_url()"
                        placeholder="凭证类型"
                        style="width: 100%"
                      >
                        <el-option
                          v-for="item in proof_options"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="到货时间" prop="arrtime" label-width="85px">
                      <el-date-picker
                        v-model="ruleForm.arrtime"
                        type="date"
                        style="width: 100%"
                        :disabled="false"
                        value-format="yyyy-MM-dd"
                        :picker-options="pickerOptions"
                        placeholder="要求到货时间"
                        @change="mdDate"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>

                  <el-col :span="24">
                    <el-form-item label="凭证文件" prop="proof_url" label-width="85px">
                      <ul class="shangchuan-ul">
                        <li v-if="ruleForm.proof_type === '1'" class="shiping">
                          <div v-if="video_url" class="clearfix">
                            <el-popover placement="top" width="300" trigger="hover">
                              <video width="275" controls class="fl">
                                <source :src="video_url" type="video/mp4" />
                                <source :src="video_url" type="video/avi" />
                                您的浏览器不支持Video标签。
                              </video>
                              <i
                                class="el-icon-video-camera-solid hover"
                                style="font-size: 18px"
                                slot="reference"
                              ></i>
                            </el-popover>
                            <el-link
                              :underline="false"
                              @click="deleteUrl('1')"
                              type="warning"
                              style="margin: 0 0 0 16px"
                              >删除</el-link
                            >
                          </div>

                          <div class="activity-upload" v-else>
                            <div class="btnupload" style="position: relative">
                              <i class="el-icon-plus avatar-uploader-icon"></i>
                              <video-upload
                                class="Upload"
                                :disabled="type === 'view' || type === 'editCoin'"
                                :accept="'.mp4,.avi'"
                                :multiple="false"
                                :uploadcondition="beforeVideoUpload"
                                @UploadErrorEvent="UploadVideoEventproof_url"
                                @UploadSuccessEvent="UploadSuccessVideoproof_url"
                              />
                            </div>

                            <div class="txt-tips fl">
                              <p>建议大小：小于10MB</p>
                              <p>文件格式：.mp4,.avi</p>
                            </div>
                          </div>
                        </li>
                        <li v-if="ruleForm.proof_type === '2'" class="tupian">
                          <div v-if="img_url" class="clearfix">
                            <img
                              style="width: 50px; height: 50px"
                              :src="img_url"
                              class="avatar fl"
                            />
                            <el-link
                              :underline="false"
                              @click="deleteUrl('2')"
                              type="warning"
                              style="margin: 0 0 0 16px"
                              >删除</el-link
                            >
                          </div>

                          <div class="activity-upload" v-else>
                            <div class="btnupload" style="position: relative">
                              <i class="el-icon-plus avatar-uploader-icon"></i>
                              <file-upload
                                class="Upload"
                                :disabled="type === 'view' || type === 'editCoin'"
                                :accept="'.jpg,.png,.jpeg'"
                                :multiple="false"
                                :uploadcondition="beforeAvatarUpload"
                                @UploadErrorEvent="UploadErrorEventproof_url"
                                @UploadSuccessEvent="UploadSuccessEventproof_url"
                              ></file-upload>
                            </div>
                            <div class="txt-tips fl">
                              <p>建议大小：小于1MB</p>
                              <p>文件格式：.jpg,.png,.jpeg</p>
                            </div>
                          </div>
                        </li>
                        <li v-if="ruleForm.proof_type === '3'" class="qita">
                          <div v-if="other_url" class="clearfix">
                            <a :href="other_url" download="凭证文件">点击下载</a>

                            <el-link
                              :underline="false"
                              @click="deleteUrl('3')"
                              type="warning"
                              style="margin: 0 0 0 16px"
                              >删除</el-link
                            >
                          </div>

                          <div class="activity-upload" v-else>
                            <div class="btnupload" style="position: relative">
                              <i class="el-icon-plus avatar-uploader-icon"></i>
                              <file-upload-pdf
                                class="Upload"
                                :accept="'.xlsx,.xls,.pdf,.zip,.rar,.7z'"
                                :multiple="false"
                                :uploadcondition="beforeOtherUpload"
                                @UploadErrorEvent="UploadErrorOtherproof_url"
                                @UploadSuccessEvent="UploadSuccessOtherproof_url"
                              />
                            </div>
                            <div class="txt-tips fl">
                              <p>建议大小：小于5MB</p>
                              <p>文件格式：.xlsx,.xls,.pdf,.zip,.rar,.7z</p>
                            </div>
                          </div>
                        </li>
                      </ul>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-col>

              <el-col :span="12">
                <el-form-item label="其他单号" prop="workNo" label-width="115px">
                  <el-input
                    v-model="ruleForm.workNo"
                    maxlength="2000"
                    type="textarea"
                    :rows="2"
                    show-word-limit
                    placeholder="如: 业管单号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="平台订单号"
                  prop="platform_order"
                  label-width="115px"
                >
                  <el-input
                    v-model="ruleForm.platform_order"
                    maxlength="2000"
                    type="textarea"
                    :rows="2"
                    show-word-limit
                    placeholder="如: PO号"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" v-if="ruleForm.sendtype === '1'">
          <el-form :model="addrForm" ref="addrForm" :size="'mini'">
            <el-table
              :data="addrForm.order_addr"
              border
              :size="'mini'"
              style="width: 100%"
              max-height="300px"
              row-key="key"
            >
              <el-table-column type="index" width="50" show-overflow-tooltip />
              <el-table-column
                prop="receipt_quantity"
                label="收货总数"
                min-width="125"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-form-item
                    v-if="scope.row.edit"
                    :prop="'order_addr.' + scope.$index + '.' + 'receipt_quantity'"
                    :rules="addrRules.receipt_quantity"
                    :size="'mini'"
                    :style="{ marginBottom: scope.row.edit ? '13px' : '0' }"
                  >
                    <el-input
                      v-model="scope.row.receipt_quantity"
                      placeholder="收货总数"
                      :disabled="!scope.row.edit"
                      maxlength="10"
                    />
                  </el-form-item>
                  <span v-if="!scope.row.edit">{{ scope.row.receipt_quantity }}</span>
                </template>
              </el-table-column>

              <el-table-column
                prop="contactor"
                label="收件联系人"
                min-width="110"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-form-item
                    v-if="scope.row.edit"
                    :prop="'order_addr.' + scope.$index + '.' + 'contactor'"
                    :rules="addrRules.contactor"
                    :size="'mini'"
                    :style="{ marginBottom: scope.row.edit ? '13px' : '0' }"
                  >
                    <el-input
                      v-model="scope.row.contactor"
                      placeholder="收件联系人"
                      maxlength="20"
                      :disabled="!scope.row.edit"
                    />
                  </el-form-item>
                  <span v-if="!scope.row.edit">{{ scope.row.contactor }}</span>
                </template>
              </el-table-column>

              <el-table-column
                prop="mobile"
                label="收货联系电话"
                min-width="130"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-form-item
                    v-if="scope.row.edit"
                    :prop="'order_addr.' + scope.$index + '.' + 'mobile'"
                    :rules="addrRules.mobile"
                    :size="'mini'"
                    :style="{ marginBottom: scope.row.edit ? '13px' : '0' }"
                  >
                    <el-input
                      v-model="scope.row.mobile"
                      placeholder="收货联系电话"
                      :disabled="!scope.row.edit"
                      maxlength="11"
                    />
                  </el-form-item>
                  <span v-if="!scope.row.edit">{{ scope.row.mobile }}</span>
                </template>
              </el-table-column>

              <el-table-column
                prop="addr_code"
                label="收货省市区"
                min-width="230"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-form-item
                    v-if="scope.row.edit"
                    :prop="'order_addr.' + scope.$index + '.' + 'addr_code'"
                    :rules="addrRules.addr_code"
                    :size="'mini'"
                    :style="{ marginBottom: scope.row.edit ? '13px' : '0' }"
                  >
                    <select-area
                      :value="scope.row.addr_code"
                      :disabled="!scope.row.edit"
                      placeholder="收货省市区"
                      :size="'mini'"
                      @selectChange="select_area_change($event, scope.$index)"
                    />
                  </el-form-item>
                  <span v-if="!scope.row.edit">{{ scope.row.addr_code_name }}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="addr"
                label="详细地址"
                min-width="170"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-form-item
                    v-if="scope.row.edit"
                    :prop="'order_addr.' + scope.$index + '.' + 'addr'"
                    :rules="addrRules.addr"
                    :size="'mini'"
                    :style="{ marginBottom: scope.row.edit ? '13px' : '0' }"
                  >
                    <el-input
                      v-model="scope.row.addr"
                      placeholder="详细地址"
                      :disabled="!scope.row.edit"
                      maxlength="100"
                    />
                  </el-form-item>
                  <span v-if="!scope.row.edit">{{ scope.row.addr }}</span>
                </template>
              </el-table-column>

              <el-table-column fixed="right" width="94" show-overflow-tooltip>
                <template slot="header" slot-scope="scope">
                  <el-tooltip
                    class="fr"
                    style="margin: 3px 0 0 8px"
                    effect="dark"
                    content="手动添加地址"
                    placement="top"
                  >
                    <i
                      class="el-icon-circle-plus-outline tb-icon"
                      style="color: #6954f0"
                      @click="openHouseModal(-1)"
                    ></i>
                  </el-tooltip>
                  <el-tooltip
                    class="fr"
                    style="margin: 3px 0 0 8px"
                    effect="dark"
                    content="导入收货地址"
                    placement="top"
                  >
                    <i
                      class="el-icon-upload2 tb-icon"
                      style="color: #6954f0"
                      @click="addrmodel = true"
                    ></i>
                  </el-tooltip>
                  <el-tooltip
                    class="fr"
                    style="margin: 3px 0 0 0"
                    effect="dark"
                    content="下载收货地址模板"
                    placement="top"
                  >
                    <a class="downloadAddr" href="./static/收货地址模板.xlsx">
                      <i
                        class="el-icon-download tb-icon"
                        style="color: #6954f0; margin: 0"
                      ></i>
                    </a>
                  </el-tooltip>
                </template>
                <template slot-scope="scope">
                  <el-tooltip
                    effect="dark"
                    content="编辑"
                    v-if="!scope.row.edit"
                    placement="top"
                  >
                    <i
                      class="el-icon-edit tb-icon"
                      @click="openHouseModal(scope.$index)"
                    ></i>
                  </el-tooltip>

                  <el-tooltip
                    effect="dark"
                    content="保存"
                    v-if="scope.row.edit"
                    placement="top"
                  >
                    <i
                      class="el-icon-circle-check tb-icon"
                      @click="checkRow(scope.$index)"
                    ></i>
                  </el-tooltip>

                  <el-tooltip effect="dark" content="删除" placement="top">
                    <i
                      class="el-icon-delete tb-icon"
                      @click="deleteRow(scope.$index, addrForm.order_addr)"
                    ></i>
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </el-col>
        <el-col :span="24">
          <in-addr-model
            :show-model="addrmodel"
            @cancel="addrmodel = false"
            @refresh="addrRefresh"
          />
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/zixunOrder";
import searchStockGoodModal from "@/components/search-stock-good-modal";
import {
  isnumber,
  isMobile,
  isChinese,
  isEmoticon,
  isSpecialSymbol,
  hasSpace,
  isAddr,
} from "@/utils/validate";
import inAddrModel from "@/components/in-addr-model";
import { ShowDataTableColumns } from "./ShowDataTableColumns";
export default {
  name: "buyGoodModal",
  props: ["showModel", "sitem"],
  mixins: [resToken],
  components: { searchStockGoodModal, inAddrModel },
  data() {
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("手机号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validateWeight = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("收货总数不能为空!"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("收货总数仅支持整数!"));
        } else {
          callback();
        }
      }
    };
    const validate_num_0 = (rule, value, callback) => {
      const { required } = rule;
      if (required && value === "") {
        callback(new Error("不能为空！"));
      } else if (
        required &&
        (value === "0" ||
          value === "0." ||
          value === "0.0" ||
          value === "0.00" ||
          value === "0.000")
      ) {
        callback(new Error("不能为零！"));
      } else {
        callback();
      }
    };
    const validatecontactor = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("联系人不能为空！"));
      } else {
        if (value.length < 2 || value.length > 10) {
          callback(new Error("联系人规则为2~10位汉字！"));
        } else {
          if (!isChinese(value)) {
            callback(new Error("联系人规则为2~10位汉字！"));
          } else if (isEmoticon(value)) {
            callback(new Error("联系人规则为2~10位汉字！"));
          } else {
            callback();
          }
        }
      }
    };
    const validateAddr = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("详细地址不能为空！"));
      } else {
        if (hasSpace(value)) {
          callback(new Error("不能出现/回车/换行符！"));
        } else if (isSpecialSymbol(value)) {
          callback(new Error("不能使用英文特殊字符！"));
        } else if (isAddr(value)) {
          callback();
        } else {
          callback(new Error("详细地址填写不规范！"));
        }
      }
    };
    return {
      loading: false,
      total_price: "0",
      addrmodel: false,
      showModelThis: this.showModel,
      showGoodsModel: false,
      ShowDataTableColumns: ShowDataTableColumns,
      video_url: "",
      img_url: "",
      other_url: "",
      proof_options: [
        {
          value: "1",
          label: "视频",
        },
        {
          value: "2",
          label: "图片",
        },
        {
          value: "3",
          label: "其他",
        },
      ],
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < new Date(this.sitem.in_arrival_time).getTime();
        },
      },

      options: [
        {
          value: "1",
          label: "直接发货",
        },
        {
          value: "2",
          label: "延时发货",
        },
      ],
      stock_code: "",
      cat_id: "",
      ruleForm: {
        bidNo: "", //竞价单反馈编号
        good_num: "", //商品数量
        sale_price: "",
        sendtype: "1", // 发货方式
        arrtime: "", // 到货时间
        remark: "", //订单备注
        platform_order: "", //平台订单号
        workNo: "", //其他单号
        companyNo: "",
      },
      rulesThis: this.rules,
      rules: {
        good_num: [
          {
            required: true,
            validator: validate_num_0,
            trigger: "blur",
          },
        ],
        sale_price: [
          {
            required: true,
            validator: validate_num_0,
            trigger: "blur",
          },
        ],
        companyNo: [
          {
            required: true,
            message: "请选择业务公司",
            trigger: "change",
          },
        ],
        sendtype: [
          {
            required: true,
            message: "请选择发货方式",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: true,
            message: "输入订单备注",
            trigger: "blur",
          },
        ],
        arrtime: [
          {
            required: true,
            message: "要求到货时间",
            trigger: "change",
          },
        ],
        proof_type: [
          {
            required: false,
            message: "请选择凭证类型",
            trigger: "change",
          },
        ],
        proof_url: [
          {
            required: false,
            message: "请上传凭证文件",
            trigger: "blur,change",
          },
        ],
      },
      addrForm: {
        order_addr: [], //收货地址
      },
      addrRules: {
        receipt_quantity: [
          {
            required: true,
            validator: validateWeight,
            trigger: "blur",
          },
        ],

        contactor: [
          {
            required: true,
            trigger: "blur",
            validator: validatecontactor,
          },
        ],
        mobile: [
          {
            required: true,
            validator: validatemobile,
            trigger: "blur",
          },
        ],

        addr_code: [
          {
            type: "array",
            required: false,
            trigger: "change",
          },
        ],
        addr: [
          {
            required: true,
            validator: validateAddr,
            trigger: "blur",
          },
        ],
      },

      id: "",
    };
  },
  created() {
    // this.this.ruleForm.arrtime = this.sitem.in_arrival_time.substring(0, 10);
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        // this.rulesThis = this.rules;
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    //修改日期校验
    mdDate() {
      //用户传递的到货时间不能大于要求到货的时间  对要求到货时间进行字符截取10位抹除时分秒
      if (
        Date.parse(this.ruleForm.arrtime) <
        Date.parse(this.sitem.in_arrival_time.substring(0, 10))
      ) {
        this.$message.warning("到货时间不能小于要求到货日期");
        this.ruleForm.arrtime = "";
      }
    },

    //销售方公司选择
    company_idsearchChange(e) {
      const { code } = e;
      this.ruleForm.companyNo = code || "";
      this.$refs.ruleForm.validateField("companyNo");
    },

    closeModel() {
      console.log("closeModel!!");
      this.showModelThis = false;
    },
    num_change(e, key) {
      this.ruleForm[key] = e + "";
      this.$refs.ruleForm.validateField(key);
      this.set_total_price();
    },
    set_total_price() {
      const { good_num, sale_price } = this.ruleForm;
      let num = good_num * 1;
      let price = sale_price * 1;
      this.total_price = num * price + "";
    },
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      console.log(this.sitem);
      const { can } = this.sitem;
      this.sitem.can_name = "";
      this.sitem.is_noble = false;
      if (can && can.length > 0) {
        can.forEach((a, ai) => {
          this.sitem.is_noble = a.id === "6";
          this.sitem.can_name += `${ai !== 0 ? "_" : ""}${a.name}`;
        });
      }
      await this.resetForm();
      this.getNewTime();
      this.set_total_price();
      this.loading = false;
    },
    addrRefresh(e) {
      const { list } = e;
      this.addrForm.order_addr.push(...list);
    },

    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async resetForm() {
      //重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const {
            bidNo,
            in_num,
            in_arrival_time,
            sale_price,
            remark,
            in_companyNo,
          } = this.sitem;
          console.log(this.sitem);
          this.ruleForm = {
            bidNo: bidNo || "", //竞价单反馈编号
            good_num: in_num || "0", //商品数量
            sale_price: sale_price || "",
            sendtype: "1", // 发货方式
            arrtime: in_arrival_time || "", // 到货时间
            remark: "",
            proof_type: "1", // 通过or驳回
            proof_url: "", //驳回至
            companyNo: in_companyNo || "",
          };
        }

        if (this.$refs.addrForm) {
          this.$refs.addrForm.resetFields();
          this.$refs.addrForm.clearValidate();
          this.addrForm = {
            order_addr: [],
          };
        }
      });
    },
    openHouseModal(index) {
      let findex = this.addrForm.order_addr.findIndex((v) => v.edit === true);
      if (findex !== -1) {
        this.$message.warning("当前已有地址在编辑，请保存后再试！");
        return;
      } else {
        if (index === -1) {
          this.addrForm.order_addr.push({
            edit: true,
            receipt_quantity: "",
            contactor: "",
            mobile: "",
            addr_code: [],
            addr: "",
          });
        } else {
          this.addrForm.order_addr[index].edit = true;
        }
      }
    },
    //省市区选择
    async select_area_change(e, index) {
      this.addrForm.order_addr[index].addr_code = e;
      const { addr_code } = this.addrForm.order_addr[index];
      if (addr_code && addr_code.length === 3) {
        let arrCode = addr_code[addr_code.length - 1];
        // console.log(model);
        let { code, data, message } = await asyncRequest.addrall({
          code: arrCode,
        });
        if (code === 0) {
          const { province, city, area } = data;
          let province_name = province && province.name ? province.name : "";
          let city_name = city && city.name ? city.name : "";
          let area_name = area && area.name ? area.name : "";
          this.addrForm.order_addr[index].addr_code_name =
            province_name && city_name && area_name
              ? `${province_name}/${city_name}/${area_name}`
              : "";
        } else if (code >= 100 && code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(message);
        }
      } else {
        this.addrForm.order_addr[index].addr_code_name = "";
      }
    },
    //省市区保存某一行
    checkRow(rowIndex) {
      this.$refs.addrForm.validate((valid) => {
        if (valid) {
          this.addrForm.order_addr[rowIndex].edit = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 省市区删除行操作
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) return;
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          model.order_addr = [];
          const { sendtype, good_num } = model;
          const { order_addr } = JSON.parse(JSON.stringify(this.addrForm));
          if (sendtype === "1" && order_addr && order_addr.length === 0) {
            this.$message.warning("至少上传一个地址信息！");
            this.loading = false;
            return;
          }
          if (sendtype === "1") {
            let addrT = 0,
              isAEdit = false;
            order_addr.forEach((v2) => {
              if (v2.edit) {
                isAEdit = true;
              }
              addrT += parseInt(v2.receipt_quantity + "");
              let model2 = {
                addr_code: v2.addr_code.toString(),
                contactor: v2.contactor,
                mobile: v2.mobile,
                addr: v2.addr,
                receipt_quantity: v2.receipt_quantity,
              };
              model.order_addr.push(model2);
            });

            if (isAEdit) {
              this.$message.warning("请保存地址信息！");
              this.loading = false;
              return;
            }
            if (parseInt(good_num + "") !== addrT) {
              this.$message.warning("收货总数量与购买不一致！");
              this.loading = false;
              return;
            }
          }
          let res = await asyncRequest.consultorder(model);

          if (res && res.code === 0) {
            this.$notify.success({
              title: "下单成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
            this.loading = false;
          } else if (res && res.code >= 100 && res.code <= 104) {
            this.loading = false;
            await this.logout();
          } else {
            this.loading = false;
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //图片上传成功
    async UploadSuccessEventproof_url(data) {
      const { url } = data;
      if (url === "noToken") {
        await this.logout();
      } else {
        this.img_url = url;
        this.set_proof_url();
        this.$message.success("图片上传成功!");
      }
    },

    //图片上传失败
    UploadErrorEventproof_url(res) {
      if (res !== "break") {
        this.set_proof_url();
        this.$message.error("图片上传失败!");
      }
    },

    //判断图片规格
    beforeAvatarUpload(file) {
      let isJPG = false;
      if (
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/jpeg"
      ) {
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isJPG) {
        this.$message.error("图片格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },

    //视频上传成功
    async UploadSuccessVideoproof_url(data) {
      const { url } = data;
      if (url === "noToken") {
        await this.logout();
      } else {
        this.video_url = url;
        this.set_proof_url();
        this.$message.success("视频上传成功!");
      }
    },

    //视频上传失败
    UploadErrorVideoproof_url(res) {
      if (res !== "break") {
        this.set_proof_url();
        this.$message.error("视频上传失败!");
      }
    },

    //判断视频规格
    beforeVideoUpload(file) {
      let isJPG = false;
      if (file.type === "video/mp4" || file.type === "video/avi") {
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isJPG) {
        this.$message.error("视频格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("视频大小不能超过 10MB!");
      }
      return isJPG && isLt2M;
    },

    // 其他文件上传成功
    async UploadSuccessOtherproof_url(data) {
      const { url } = data;
      if (url === "noToken") {
        await this.logout();
      } else {
        this.other_url = url;
        this.set_proof_url();
        this.$message.success("文件成功!");
      }
    },

    //其他文件上传失败
    UploadErrorOtherproof_url(res) {
      if (res !== "break") {
        this.set_proof_url();
        this.$message.error("文件上传失败!");
      }
    },

    //判断其他文件规格
    beforeOtherUpload(file) {
      console.log(file);
      let isJPG = false;
      if (
        file.type === "application/vnd.ms-excel" ||
        file.type ===
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
        file.type === "application/pdf" ||
        file.type === "application/x-zip-compressed"
      ) {
        isJPG = true;
      }
      let name = file.name;
      let list = name.split(".");
      if (list[list.length - 1] === "rar" || list[list.length - 1] === "7z") {
        isJPG = true;
      }

      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isJPG) {
        this.$message.error("文件格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("文件大小不能超过 5MB!");
      }
      return isJPG && isLt2M;
    },

    deleteUrl(type) {
      switch (type) {
        case "1":
          this.video_url = "";
          break;
        case "2":
          this.img_url = "";
          break;
        case "3":
          this.other_url = "";
          break;
        default:
          this.video_url = "";
      }
      this.set_proof_url();
    },
    set_proof_url() {
      const { proof_type } = this.ruleForm;
      switch (proof_type) {
        case "1":
          this.ruleForm.proof_url = this.video_url;
          break;
        case "2":
          this.ruleForm.proof_url = this.img_url;
          break;
        case "3":
          this.ruleForm.proof_url = this.other_url;
          break;
        default:
          this.ruleForm.proof_url = this.video_url;
      }
      this.$refs.ruleForm.validateField("proof_url");
    },
  },
};
</script>

<style lang="scss" scoped>
// .capitalClaim {
.excelUploadBox {
  position: relative;
  width: 100%;
  height: 120px;
  line-height: 120px;
  box-sizing: border-box;
  &:hover {
    cursor: pointer;
  }
  .el-icon-receiving {
    width: 100%;
    text-align: center;
    height: 50px;
    display: block;
    font-size: 32px;
    line-height: 90px;
    color: #d3d4d6;
  }
  .boxM {
    width: 100%;
    display: block;
    text-align: center;
    line-height: 65px;
    height: 60px;
    color: #909399;
  }
}
.excelUpload {
  top: 0;
  left: 0;
  position: absolute;
  z-index: 2;
  width: 100%;
  height: 120px;
  line-height: 120px;
  box-sizing: border-box;
}
.excelUploadRes {
  width: 100%;
  height: 120px;
  line-height: 120px;
  box-sizing: border-box;
  i {
    width: 55px;
    height: 120px;
    line-height: 120px;
    text-align: center;
    font-size: 20px;
    &.fl {
      padding-left: 16px;
    }
    &.fr {
      padding-right: 16px;
      &:hover {
        cursor: pointer;
      }
    }
  }
  span {
    width: 386px;
    line-height: 16px;
    margin: 52px 0 0 0;
    font-size: 16px;
  }
}
// }
</style>
