<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    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 class="add" style="margin-top: -25px">
      <el-row>
        <el-col :span="18">
          <el-form ref="ruleForm" size="small" label-width="100px">
            <el-form-item size="small" label="开票信息:">
              <el-select
                v-model="code"
                multiple
                filterable
                size="small"
                remote
                :multiple-limit="1"
                reserve-keyword
                style="width: 100%"
                placeholder="开票信息"
                @change="selectChange"
              >
                <el-option
                  v-for="item in activeOptions"
                  :key="item.companyNo"
                  :label="item.companyNo + '--------' + item.company_name"
                  :value="item.companyNo"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            class="fl"
            size="small"
            style="margin: 0 0 0 10px"
            @click="batchSet"
            >批量设置</el-button
          >
          <el-button type="primary" class="fr" size="small" @click="saveSet"
            >保存设置</el-button
          >
        </el-col>
        <el-col :span="24" v-if="res === null"></el-col>
        <el-col :span="24" v-else>
          <el-form ref="ruleForm3" size="mini" label-width="100px">
            <el-row>
              <el-col :span="7">
                <el-form-item size="mini" label="纳税人识别号:">
                  <p>{{ res.company_license }}</p>
                </el-form-item>
              </el-col>

              <el-col :span="7">
                <el-form-item size="mini" label="银行卡号:">
                  <p>{{ res.bankNo }}</p>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item size="mini" label="银行名称:">
                  <p>{{ res.bank_name }}</p>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
      <div style="heigth: 80px"></div>
      <el-table
        :data="list"
        stripe
        border
        size="mini"
        max-height="450px"
        style="width: 100%"
        v-on="$listeners"
        ref="multipleTable"
        row-key="cgdNO"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
        ><el-table-column
          type="selection"
          width="50"
          fixed="left"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="cgdNO"
          show-overflow-tooltip
          label="采购单编号"
          width="150"
        />
        <el-table-column
          prop="name"
          show-overflow-tooltip
          label="供货商"
          width="180"
        />
        <el-table-column
          prop="contector"
          show-overflow-tooltip
          label="联系人"
          width="70"
        />
        <!-- <el-table-column
          prop="purchasePrice"
          show-overflow-tooltip
          label="货款"
          width="110"
        /> -->
        <el-table-column label="开票信息">
          <el-table-column
            prop="companyNo"
            show-overflow-tooltip
            label="编码"
            width="170"
          />
          <el-table-column
            prop="company_name"
            show-overflow-tooltip
            label="公司名称"
            width="170"
          />

          <el-table-column
            prop="company_license"
            show-overflow-tooltip
            label="纳税人识别号"
            width="155"
          />
          <el-table-column
            prop="bank_name"
            show-overflow-tooltip
            label="银行名称"
            width="170"
          />
          <el-table-column
            prop="bankNo"
            show-overflow-tooltip
            label="银行卡号"
            width="145"
          />
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="50">
          <template slot-scope="scope">
            <el-tooltip
               
              effect="dark"
              content="删除"
              placement="top"
            >
              <i
                class="el-icon-delete tb-icon"
                @click="deleteRow(scope.$index, list)"
              ></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/purchase/porder";
// import countTo from "vue-count-to";
import resToken from "@/mixins/resToken";
export default {
  name: "supplierPay",
  props: ["showModel", "tableData"],
  // components: { countTo },
  mixins: [resToken],
  data() {
    return {
      res: null,
      loading: false,
      title: "批量设置开票信息",
      selectLoading: false,
      activeOptions: [],
      code: [],
      list: [],
      multipleSelection: [],
      showModelThis: this.showModel,
    };
  },
  watch: {
    showModel: function (val) {
      if (val) {
        this.initForm();
      }
      this.showModelThis = val;
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    async remoteMethod(query) {
      this.selectLoading = true;
      this.activeOptions = [];
      const res = await asyncRequest.companylist({});
      if (res && res.code === 0 && res.data) {
        this.activeOptions = res.data;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.activeOptions = [];
      }
      this.selectLoading = false;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    selectChange(val) {
      console.log(val);
      if (val && val.length > 0) {
        let key = val[0];
        let index = this.activeOptions.findIndex((v1) => v1.companyNo === key);
        if (index !== -1) {
          this.res = this.activeOptions[index];
        } else {
          this.res = null;
        }
      } else {
        this.res = null;
      }
    },
    async initForm() {
      this.loading = true;
      this.multipleSelection = [];
      this.activeOptions = [];
      this.code = [];
      this.res = null;

      await this.remoteMethod("");
      this.$nextTick(() => {
        // setTimeout(() => {
        console.log(this.tableData);
        if (this.tableData && this.tableData.length > 0) {
          this.list = JSON.parse(JSON.stringify(this.tableData));
          this.list.map((v1) => {
            v1.companyNo = "";
            v1.company_name = "";
            v1.company_license = "";
            v1.bankNo = "";
            v1.bank_name = "";
            return v1;
          });
        }
        // }, 10000);
      });
      this.loading = false;
    },
    batchSet() {
      if (this.multipleSelection && this.multipleSelection.length === 0) {
        this.$message.warning("请选择采购单！");
        return;
      } else {
        if (this.code && this.code.length === 0) {
          this.$message.warning("请选择开票信息！");
          return;
        } else {
          console.log(this.multipleSelection);
          console.log(this.list);
          this.multipleSelection.forEach((v1) => {
            let index = this.list.findIndex(
              (v2) => v2.cgdNO === v1.cgdNO
            );
            console.log(index);
            if (index !== -1) {
              this.list[index].companyNo = this.res.companyNo;
              this.list[index].company_name = this.res.company_name;
              this.list[index].company_license = this.res.company_license;
              this.list[index].bankNo = this.res.bankNo;
              this.list[index].bank_name = this.res.bank_name;
               this.$set(this.list,index,this.list[index]) 
            }
          });
        }
      }
    },
    async saveSet() {
      let isok = true;
      let model = {
        cgd: [],
      };
      this.list.forEach((v1) => {
        let items = {
          cgdNo: v1.sequenceNo,
          companyNo: v1.companyNo,
        };
        if (!(v1.companyNo && v1.companyNo !== "")) {
          isok = false;
        }
        model.cgd.push(items);
      });
      if (!isok) {
        this.$message.warning("部分采购单未设置开票信息！");
        return;
      } else {
        this.loading = true;
        const res = await asyncRequest.inv(model);
        if (res && res.code === 0 && res.data) {
          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);
        }
        this.loading = false;
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.supplierPay {
  .add {
    .survey-title {
      line-height: 35px;
      font-size: 18px;
      font-weight: bold;
    }
    .el-row {
      margin-bottom: 20px;
      &:last-child {
        margin-bottom: 0;
      }
    }
    .lh40 {
      line-height: 40px;
      font-size: 16px;
    }
    .el-col {
      border-radius: 4px;
    }
    .bg-purple-dark {
      background: #99a9bf;
    }
    .bg-purple {
      background: #d3dce6;
      padding: 30px;
    }
    .bg-purple-light {
      background: #e5e9f2;
    }
    .grid-content {
      border-radius: 4px;
      min-height: 36px;
    }
    .row-bg {
      padding: 10px 0;
      background-color: #f9fafc;
    }
  }
  .setWidth {
    width: 100% !important;
    // display: flex;
    // display: block !important;
    // .el-input__inner {
    //   display: flex;
    //   width: 1000px !important;
    //   display: block !important;
    // }
  }
}
</style>
