<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="8vh"
    width="900px"
    :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="showModelThis = false"
  >
    <el-card style="margin-top: -20px" class="account">
      <el-row :gutter="10">
        <el-col :span="24" style="margin:-10px 0 0 0">
          <el-divider>选择收货地址</el-divider>
        </el-col>
        <el-col :span="24">
          <el-table
            ref="multipleTable"
            :data="tableData1"
            border
            :size="'mini'"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange($event, 1)"
          >
            <el-table-column type="selection" width="40" align="center" />
            <el-table-column
              prop="receipt_quantity"
              label="总数量"
              width="100"
            />
            <el-table-column prop="send_num" label="已发货数量" width="100" />
            <el-table-column prop="wsend_num" label="未发货数量" width="100" />
            <el-table-column
              prop="arrive_time"
              label="最晚到货时间"
              width="145"
            />
            <el-table-column prop="contactor" label="收件人" width="60" />
            <el-table-column prop="mobile" label="联系电话" width="105" />
            <el-table-column
              prop="contactor"
              label="收件地址"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                {{ scope.row.addr_info }}{{ scope.row.addr }}
              </template>
            </el-table-column>
            <!-- <el-table-column prop="post_fee" label="物流费" width="100" /> -->
          </el-table>
        </el-col>
        <el-form
        style="padding:15px 0 0 0"
          ref="ruleForm"
          :model="ruleForm"
          status-icon
          :rules="rulesThis"
          label-width="90px"
          class="demo-ruleForm"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="发货数量" prop="send_num">
                <el-input v-model="ruleForm.send_num" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物流费用" prop="post_fee">
                <el-input v-model="ruleForm.post_fee" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物流公司" prop="post_name">
                <search-express
                  :value="ruleForm.post_name"
                  :placeholder="'请输入物流公司'"
                  :names="''"
                  :disabled="false"
                  :is-detail="false"
                  @searchChange="handleCompany"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物流单号" prop="post_code">
                <el-input v-model="ruleForm.post_code" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="24" style="text-align: right">
            <el-button type="primary" @click="submitForm">保 存 </el-button>
            <el-button @click="showModelThis = false">关 闭</el-button>
          </el-col>
        </el-form>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/zxOrder/detail";
import resToken from "@/mixins/resToken";
import { isnumber, isnumber2, isAlphanumeric } from "@/utils/validate";
export default {
  name: "Account",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    const validatePrice = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空！"));
      } else {
        if (isnumber2(value)) {
          callback();
        } else {
          callback(new Error("仅支持整数或两位小数!"));
        }
      }
    };
    const validateWeight = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("仅支持整数!"));
        } else {
          callback();
        }
      }
    };
    const validateCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("仅支持字母和数字!"));
        } else {
          callback();
        }
      }
    };
    return {
      roleList: [],
      tableData: [],
      tableData1: [],
      loading: false,
      title: "新建发货申请单",
      list1: [],
      list0: [],
      showModelThis: this.showModel,
      rulesThis: {},
      rules: {
        send_num: {
          required: true,
          validator: validateWeight,
          trigger: "blur",
        },
        post_name: {
          type: "array",
          required: true,
          trigger: "change",
          message: "请输入物流公司",
        },
        post_code: {
          required: true,
          validator: validateCode,
          trigger: "blur",
        },
        post_fee: {
          required: true,
          validator: validatePrice,
          trigger: "blur",
        },
      },
      supplierName: "",
      good_price: "0",
      lasttime: "",
      ruleForm: {
        post_name: [], //裸价
        post_code: "", //仓库
        post_fee: "", //供应商
        send_num: "", //采购供应商编码
        addrid: "",
        orderCode: "",
        wsm_code: "",
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    

    handleSelectionChange(e, type) {
      if (type === 0) {
        this.list0 = e;
        this.ruleForm.wsm_code = e && e.length > 0 ? e[0].wsm_code : "";
      } else {
        this.list1 = e;
        this.ruleForm.addrid = e && e.length > 0 ? e[0].id : "";
      }
    },
    async initForm() {
      this.loading = true;
      this.list0 = [];
      this.list1 = [];
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    //初始化表单
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { addrs, info, orderCode } = this.sitem;
          this.tableData = info;
          this.tableData1 = addrs;
          this.ruleForm = {
            post_name: [],
            post_code: "",
            post_fee: "",
            send_num: "",
            addrid: "",
            orderCode: orderCode || "",
            wsm_code: "",
          };
        }
      });
    },
    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const item = JSON.parse(JSON.stringify(this.ruleForm));
          if (this.list0.length === 0) {
            this.$message.warning("请选择发货仓库!");
            return;
          }
          if (this.list1.length === 0) {
            this.$message.warning("请选择收货地址！");
            return;
          }
          if (this.list0.length !== 1) {
            this.$message.warning("只能选择一个发货仓库!");
            return;
          }
          if (this.list1.length !== 1) {
            this.$message.warning("只能选择一个收货地址！");
            return;
          }
          let cNum = parseInt(this.list0[0].wsend_num);
          let aNum = parseInt(this.list1[0].wsend_num);
          let num = parseInt(this.ruleForm.send_num);
          if (num > cNum) {
            this.$message.warning("发货数量不能大于仓库未发货数量!");
            return;
          }
          if (num > aNum) {
            this.$message.warning("发货数量不能大于收货地址未发货数量!");
            return;
          }
          this.loading = true;
          item.post_name = item.post_name.toString();
          const res = await asyncRequest.saleout(item);
          this.loading = false;
          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;
        }
      });
    },
    handleCompany(e) {
      if (e && e.code) {
        this.ruleForm.post_name = [e.shortName];
        this.ruleForm.page = 1;
      }
      this.$refs.ruleForm.validateField("post_name");
    },
  },
};
</script>

   <style lang="scss" scoped>
.account {
  .gongshi {
    span {
      vertical-align: top;
      display: inline-block;
      color: #000;
    }
    .icon-span {
      padding: 0 5px;
      height: 40px;
      line-height: 40px;
      font-size: 20px;
      color: #606266;
      display: inline-block;
      // vertical-align: top;
      // display: inline-block;
    }
    .label {
      height: 40px;
      line-height: 40px;
    }
    .tuan {
      &.chu {
        width: 60px;
        height: 40px;
        display: inline-block;
        span {
          width: 60px;
          display: inline-block;
          line-height: 20px;
          text-align: center;
          font-size: 12px;
          height: 20px;
          &:last-child {
            border-top: 1px solid #606266;
          }
        }
      }
      &.cheng {
        .name {
          height: 40px;
          line-height: 40px;
        }
        .icon-span {
          line-height: 40px;
          font-size: 16px;
          padding: 0 1px;
        }
      }
    }
  }
}
</style>
