<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="main">
      <div class="form_all">
        <el-form ref="select" :model="select" label-width="auto">
          <el-row :gutter="10">
            <el-col :span="4">
              <el-form-item label="供应商名称">
                <el-input clearable v-model="select.name"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="4">
              <el-button type="primary" @click="getDataList">
                {{ '搜索' }}</el-button
              >
              <el-button type="" @click="reset"> {{ '重置' }}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="maintop">
        <div class="mainbtn">
          <!-- v-has-buttons="['supplierApplyAdd']" -->
          <el-button
            size="small"
            @click="openDig"
            :disabled="canSub"
            class="addBtn"
            >{{ '添加' }}</el-button
          >
          <!-- v-has-buttons="['supplierApplyDelete']" -->
          <el-button
            size="small"
            @click="handleDelete"
            style="background-color: #c9171d"
            :disabled="single || canSub"
            class="thebtn1"
          >
            {{ '删除' }}</el-button
          >
          <!-- v-has-buttons="['supplierApplySubmit']" -->
          <el-button
            size="small"
            :disabled="selRow.length == 0 || canSub"
            @click="goQc"
            type="success"
            >{{ '提交' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="700px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            align="center"
            min-width="120px"
            prop="name"
            label="供应商名称"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="100px"
            prop="shortName"
            label="供应商简称"
          >
          </el-table-column>

          <el-table-column align="center" prop="legalBody" label="法人姓名">
          </el-table-column>
          <el-table-column align="center" prop="linkMan" :label="'联系人'">
          </el-table-column>
          <el-table-column
            align="center"
            prop="taxpayerid"
            label="纳税人登记号"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="provinceName"
            :label="'省'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="cityName"
            :label="'市'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="countyName"
            :label="'区/县'"
          >
          </el-table-column>
          <el-table-column align="center" prop="address" :label="'详细地址'">
          </el-table-column>
          <el-table-column
            min-width="120px"
            align="center"
            prop="validPeriod"
            label="有效时间"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleTypeVal"
            label="结算方式 "
          >
          </el-table-column>
          <el-table-column
            min-width="120px"
            align="center"
            prop="tradeScope"
            label="经营范围"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="approveStatusVal"
            label="审核状态"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude1"
            min-width="120px"
            label="资质1"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude1"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude1 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude2"
            min-width="120px"
            label="资质2"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude2"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude2 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude3"
            min-width="120px"
            label="资质3"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude3"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude3 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude4"
            min-width="120px"
            label="资质4"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude4"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude4 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude5"
            min-width="120px"
            label="资质5"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude5"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude5 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude6"
            min-width="120px"
            label="资质6"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude6"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude6 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude7"
            min-width="120px"
            label="资质7"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude7"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude7 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="aptitude8"
            min-width="120px"
            label="资质8"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.aptitude8"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.aptitude8 }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="certificate"
            min-width="120px"
            label="证书"
          >
            <template slot-scope="scope">
              <a
                :href="scope.row.certificate"
                target="_blank"
                style="cursor: pointer; color: blue; text-decoration: underline"
                >{{ scope.row.certificate }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="creationTime"
            min-width="180px"
            :label="'创建时间'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                style="color: #6962f6"
                class="button-s"
                type="text"
                size="small"
                :disabled="
                  scope.row.approveStatus == 5 || scope.row.approveStatus == 2
                "
                @click.native.prevent="updateRow(scope.row)"
              >
                {{ '修改' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />

    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-row>
          <!-- <el-col :span="12">
              <el-form-item label="客商编码" prop="custCode">
                <el-input clearable v-model="form.custCode"></el-input>
              </el-form-item>
            </el-col> -->
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="name">
              <el-input clearable v-model="form.name"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="供应商简称" prop="shortName">
              <el-input clearable v-model="form.shortName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人姓名" prop="legalBody">
              <el-input clearable v-model="form.legalBody"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'联系人'" prop="linkMan">
              <el-input clearable v-model="form.linkMan"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营范围" prop="tradeScope">
              <el-input clearable v-model="form.tradeScope"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纳税人登记号" prop="taxpayerid">
              <el-input clearable v-model="form.taxpayerid"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算方式" prop="settleType">
              <el-select
                clearable
                :placeholder="'请选择'"
                v-model="form.settleType"
                disabled
              >
                <el-option
                  v-for="item in settleTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.settleType == 1">
            <el-form-item label="会员编号" prop="memberCode">
              <el-input clearable v-model="form.memberCode"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="有效期" prop="validPeriod">
              <el-date-picker
                v-model="form.validPeriod"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="'请选择'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" class="dizhi" prop="shoppingAddress">
              <el-cascader
                v-model="form.shoppingAddress"
                :key="isResouceShow"
                :options="areaData"
                :props="regionParams"
                @change="handleChange1"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'详细地址'" class="dizhi" prop="address">
              <el-input clearable v-model="form.address"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件" prop="aptitude1">
              <el-upload
                class="upload-demo"
                :action="uploadImgUrl"
                :on-success="handleUploadSuccess"
                :on-remove="removeFile"
                multiple
                :limit="8"
                :headers="headers"
                :file-list="fileList"
              >
                <el-button size="small" type="primary">{{
                  '点击上传'
                }}</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证书" prop="certificate">
              <el-upload
                class="upload-demo"
                :action="uploadImgUrl"
                :on-success="handleUploadSuccess2"
                :on-remove="removeFile2"
                multiple
                :limit="1"
                :headers="headers"
                :file-list="fileList2"
              >
                <el-button size="small" type="primary">{{
                  '点击上传'
                }}</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
  <script>
import topBar from "@/components/topBar";
import { getToken } from "@/utils/auth";
import * as api from "@/api/supply.js";
import * as site from "@/api/site.js";
export default {
  name: "SupplierApply",
  components: {
    topBar,
  },
  filters: {
    isAgree(val) {
      if (!val) {
        return '允许';
      } else {
        return '禁止';
      }
    },
  },
  data() {
    return {
      creationTime: [],
      creationTime1: [],
      select: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      addOrEdit: "", //true添加false修改
      dialogTitle: "",
      total: 0,
      dialogVisible: false,
      tableData: [],
      loading: false,
      form: {
        shoppingAddress: ["", "", ""],
        settleType: 2,
      },
      moren: "supplierApply",
      topList: [
        {
          name: "供应商申请",
          path: "supplierApply",
        },
      ],
      single: true,
      thepkId: "",
      selRow: [],
      isResouceShow: 0,
      areaData: [],
      regionParams: {
        label: "name",
        value: "id",
        children: "children",
        expandTrigger: "hover",
      },
      settleTypeList: [],
      rules: {
        name: [{ required: true, message: '请输入', trigger: "blur" }],
        shortName: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        legalBody: [
          { required: true, message: '请输入', trigger: "blur" },
        ],

        settleType: [
          { required: true, message: '请选择收货地址', trigger: "change" },
        ],
        memberCode: [
          { required: true, message: '请输入', trigger: "blur" },
        ],

        shoppingAddress: [
          { required: true, message: '请选择收货地址', trigger: "change" },
        ],
      },
      canSub: false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/system/upload", // 上传的图片服务器地址
      headers: {
        Authorization: getToken(),
      },
      fileList: [],
      fileList2: [],
    };
  },
  mounted() {
    this.getOthers();
    // 获取列表
    this.getDataList();
  },
  methods: {
    // handleImageClick(url) {
    //   // 在这里实现查看大图的逻辑，可以使用第三方库或者自定义弹窗来展示大图
    //   // 例如使用ElementUI的Dialog组件来展示大图
    //   this.$alert(
    //     '<img src="' + url + '" style="width:100%;height:100%">',
    //     "",
    //     {
    //       dangerouslyUseHTMLString: true,
    //     }
    //   );
    // },
    handleUploadSuccess(res) {
      if (res.code == 200) {
        this.fileList.push(res.data);
      } else {
        this.$message({
          type: "error",
          message: '上传失败',
        });
      }
    },
    handleUploadSuccess2(res) {
      if (res.code == 200) {
        this.fileList2.push(res.data);
        this.form.certificate = res.data.url;
      } else {
        this.$message({
          type: "error",
          message: '上传失败',
        });
      }
    },
    removeFile(file, fileList) {
      this.fileList.forEach((item, index) => {
        if (file.uid == item.uid) {
          this.fileList.splice(index, 1);
        }
      });
    },
    removeFile2(file, fileList) {
      this.fileList2.forEach((item, index) => {
        if (file.uid == item.uid) {
          this.fileList2.splice(index, 1);
          this.form.certificate = "";
        }
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.fileList.length > 0) {
            this.form.province = this.form.shoppingAddress[0];
            this.form.city = this.form.shoppingAddress[1];
            this.form.county = this.form.shoppingAddress[2];
            let thefileList = this.fileList.map((item) => {
              return item.url;
            });
            this.form.fileList = thefileList;
            // this.$set(this.form,'fileList',this.fileList)
            let url = "";
            if (this.addOrEdit == true) {
              url = api.addThSupplierApply;
            } else {
              url = api.updateThSupplierApply;
            }
            url(this.form).then((res) => {
              if (res.code == 200) {
                this.$message({
                  message: res.msg,
                  type: "success",
                });
                this.dialogVisible = false;
                // 获取列表
                this.getDataList();
                this.$refs[formName].resetFields();
              }
            });
          } else {
            this.$message({
              message: "请上传至少一个附件",
              type: "warning",
            });
          }
        }
      });
    },
    //修改
    updateRow(row) {
      this.dialogTitle = '修改';
      this.addOrEdit = false;
      api.getOneThsupplierApply(row.pkId).then((res) => {
        this.form = res.data;
        let arr = [
          res.data.province + "",
          res.data.city + "",
          (res.data.county || "") + "",
        ];
        arr = arr.filter((item) => item != "");
        this.$set(this.form, "shoppingAddress", arr);
        this.$forceUpdate();
        this.dialogVisible = true;
      });
    },
    handleChange1(val) {
      this.form.provinceName = val[0];
      this.form.cityName = val[1];
      this.form.countyName = val[2] || "";
    },
    getOthers() {
      site.areaList().then((res) => {
        this.areaData = res.data;
      });
      api.getSettleType().then((res) => {
        this.settleTypeList = res.data;
      });
    },
    reset() {
      this.select = {};
      this.getDataList();
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        api.deleteSupplierApply(this.thepkId).then((res) => {
          this.getDataList();
        });
      });
    },
    openDig() {
      this.dialogVisible = true;
      this.addOrEdit = true;
      this.dialogTitle = '添加';
      this.$refs[formName].resetFields();
    },
    getDataList() {
      this.loading = true;
      api
        .getSupplierApplyList(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.loading = false;
        });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    handleSelectionChange(val) {
      this.single = val.length !== 1;
      this.thepkId = val[0].pkId;
      this.selRow = val;
      val.forEach((element) => {
        if (element.approveStatus == 5 || element.approveStatus == 2) {
          this.canSub = true;
        } else {
          this.canSub = false;
        }
      });
    },
    goQc() {
      this.$confirm("是否确认提交", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        let arr = this.selRow.map((item) => item.pkId);
        let pkIdListStr = arr.join(",");
        api.petitionSupplierApply({ pkIdListStr: pkIdListStr }).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getDataList();
          }
        });
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>
  <style scoped lang="scss">
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .form_all {
      padding: 0px 20px 0 20px;
      // margin-bottom: 20px;
      background: #fff;
      border-radius: 8px;
    }
    .maintop {
      display: flex;
      padding: 0px 0px;
      justify-content: space-between;
      align-items: center;

      background: #f8f8f8;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  text-align: right;
  margin-right: 10px;
  color: #3181e5;
}
.dizhi {
  ::v-deep .el-cascader {
    width: 100%;
  }
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto;
}
</style>
  