<template>
  <div class="fullHeight whiteBlock">
    <div style="padding: 15px 15px 0">
      <p class="pageTitle">供应商管理</p>
      <el-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        size="medium"
      >
        <el-form-item label="助查码">
          <el-input
            v-model="searchForm.lookupCode"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="供应商名称">
          <el-input
            v-model="searchForm.vendorName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="纳税人识别号">
          <el-input
            v-model="searchForm.taxpayerIdentificationNumber"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input
            v-model="searchForm.contactNumber"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人">
          <el-input
            v-model="searchForm.contact"
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" plain @click="getList">{{
            $t('message.energyManage.waybill.query')
          }}</el-button>
          <el-button type="warning" plain @click="refresh">{{
            $t('message.deviceManage.refresh')
          }}</el-button>
          <el-button type="primary" size="medium" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List">
        <el-table-column
          prop="lookupCode"
          label="助查码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="vendorName"
          align="center"
          label="供应商名称"
        ></el-table-column>
        <el-table-column
          prop="taxpayerIdentificationNumber"
          align="center"
          label="纳税人识别号"
        ></el-table-column>
        <!-- 单位 -->
        <el-table-column
          prop="address"
          align="center"
          label="地址"
        ></el-table-column>
        <el-table-column prop="contact" label="联系人" align="center">
        </el-table-column>
        <el-table-column prop="contactNumber" label="电话" align="center">
        </el-table-column>
        <el-table-column prop="mail" label="邮件" align="center">
        </el-table-column>
        <el-table-column
          prop="legalRepresentative"
          label="法人代表"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="openedBankAccount"
          label="开户银行"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="bankAccount" label="银行账号" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div class="tableBtns">
              <div @click="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>{{ $t('message.deviceManage.edit') }}</span>
              </div>
              <div @click="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>{{ $t('message.deviceManage.delete') }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="pagInfo.pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pagInfo.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(pagInfo.total)"
        background
      ></el-pagination>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :modal-append-to-body="false"
      @close="close"
      :title="title"
      :visible.sync="dialogShow"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-width="120px"
          class="dialogFormBox"
        >
          <el-form-item label="助查码" prop="lookupCode">
            <el-input
              v-model="addEditForm.lookupCode"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="供应商名称" prop="vendorName">
            <el-input
              v-model="addEditForm.vendorName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="纳税人识别号"
            prop="taxpayerIdentificationNumber"
          >
            <el-input
              v-model="addEditForm.taxpayerIdentificationNumber"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input
              v-model="addEditForm.address"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contact">
            <el-input
              v-model="addEditForm.contact"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contactNumber">
            <el-input
              v-model="addEditForm.contactNumber"
              placeholder="请输入"
              oninput="if(value.length>11)value=value.slice(0,11)"
              type="number"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮件" prop="mail">
            <el-input
              v-model="addEditForm.mail"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="法人代表" prop="legalRepresentative">
            <el-input
              v-model="addEditForm.legalRepresentative"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="开户银行" prop="openedBankAccount">
            <el-input
              v-model="addEditForm.openedBankAccount"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="银行账号" prop="bankAccount">
            <el-input
              v-model="addEditForm.bankAccount"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              @click="dialogShow = false"
              icon="el-icon-circle-close"
              size="medium"
              >{{ $t('message.deviceManage.cancel') }}
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submit"
              size="medium"
              >{{ $t('message.deviceManage.save') }}
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addGtMaterialVendorApi,
  deletGtMaterialVendorApi,
  editGtMaterialVendorApi,
  gettGtMaterialVendorPageApi
} from '@/assets/js/api/materialManagement.js'
import {
  checkPhone,
} from "@/assets/js/util.js";
export default {
  mounted() {
    // this.getFeedUnitList()
    // this.getPurchaseOrderList()
    this.getList()
  },
  data() {
    return {
      title: "",
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      addEditForm: {
        lookupCode: '',
        vendorName: '',
        taxpayerIdentificationNumber: '',
        address: '',
        contact: '',
        contactNumber: '',
        mail: '',
        legalRepresentative: '',
        openedBankAccount: '',
        bankAccount: '',
      },
      addEditRules: {
        lookupCode: [
          { required: true, message: '必填', trigger: 'change' }

        ],
        vendorName: [
          { required: true, message: '必填', trigger: 'change' }

        ],
        taxpayerIdentificationNumber: [
          { required: true, message: '必填', trigger: 'change' }

        ],
      },
      searchForm: {
        lookupCode: '',
        vendorName: '',
        contactNumber: '',
        taxpayerIdentificationNumber: '',
        contact: '',
      },
    }
  },
  methods: {
    //查询仓库数据
    getList() {
      gettGtMaterialVendorPageApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        lookupCode: this.searchForm.lookupCode,
        vendorName: this.searchForm.vendorName,
        contactNumber: this.searchForm.contactNumber,
        taxpayerIdentificationNumber: this.searchForm.taxpayerIdentificationNumber,
        contact: this.searchForm.contact,
      }).then((result) => {
        console.log('result====', result);
        if (result.success) {
          this.List = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },

    add() {
      this.title = '新增'
      this.dialogShow = true
      this.close()
    },

    edit(obj) {
      this.title = '编辑'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
    },
    submit() {
      if (this.addEditForm.contactNumber) {
        if (!checkPhone(this.addEditForm.contactNumber)) {
          this.$message.error(
            '请输入正确的联系电话'
          );
          return;
        }
      }

      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增') {
            addGtMaterialVendorApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title == '编辑') {
            console.log('编辑信息', this.addEditForm)
            editGtMaterialVendorApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.dialogShow = false
        } else {
          return false
        }
      })
    },
    deleteDev(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletGtMaterialVendorApi({ id: obj.id }).then((res) => {
            if (res.success) {
              this.getList()
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    close() {
      this.addEditForm = {}
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getList()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getList()
    },
    refresh() {
      this.searchForm = {}
      this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getList()
    },
  }
}
</script>
<style lang="less" scoped>
.tables2 {
  min-height: auto;
}
// .tableBtns {
//     display: flex;
//     justify-content: center;
// }
</style>