<template>
  <div>
    <!---生产企业资质----->
    <el-card>
      <el-form :model="filterQuery" class="query-form" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col span="6">
            <el-form-item label="企业名称:">
              <el-input v-model="filterQuery.companyName" style="width: 90%" placeholder="请输入企业名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="社会信用号:">
              <el-input v-model="filterQuery.creditCode" style="width: 90%" placeholder="请输入社会信用号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="处理状态:">
              <el-select v-model="filterQuery.auditStatus" style="width: 90%" placeholder="请输入处理状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="草稿" value="0"></el-option>
                <el-option label="未审核" value="6"></el-option>
                <el-option label="变更未审核" value="3"></el-option>
                <el-option label="已审核" value="1"></el-option>
                <el-option label="已拒绝" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addInfoDialog()">添加</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>


      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row
                @current-change="supManufacturerClick">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="企业名称" prop="companyName" width="250"></el-table-column>
        <el-table-column label="社会信用号" prop="creditCode"></el-table-column>
        <el-table-column label="企业类型" prop="bussinessStatus" width="120">
          <template slot-scope="scope">
            <span>{{ companyTypeMap[scope.row.companyType] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所在地区" prop="placeArea"></el-table-column>
        <el-table-column label="所在地址" prop="placeAddress" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="所属供应商" prop="supName" show-overflow-tooltip></el-table-column>
        <el-table-column label="审核状态" prop="auditStatus" width="120">
          <template slot-scope="scope">
            <el-tag :type="(scope.row.auditStatus) | statusFilterType">
              {{ checkFlag[scope.row.auditStatus] }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="addInfoDialog(scope.row)">编辑</el-button>
            <el-button type="text" size="small" :disabled="scope.row.auditStatus === 1"
                       @click.native.stop="deleteDialog(scope.row)">删除
            </el-button>
            <el-button type="text" size="small" v-if="scope.row.auditStatus === 2"
                       @click.native.stop="rejectInfo(scope.row)">说明
            </el-button>
          </template>
        </el-table-column>
      </el-table>


      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="handleCurrentChange"
      ></pagination>

      <el-dialog
        :title="formMap[formName]"
        :visible.sync="addInfoVisible"
        width="80%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="addInfoVisible"
        @close='closeDialog'
      >
        <suppliersEnterpriseBasicAdd
          :addCloseDialog="closeDialog"
          :inputQuery="inputQuery"
          :editType="editType"
          :fromType="fromType"
        ></suppliersEnterpriseBasicAdd>
      </el-dialog>


    </el-card>


    <!---配送企业资质----->
    <el-card class="el-card">
      <el-form :model="registrationQuery" class="query-form" style="margin-top: 5px" v-show="showSearch2"
               label-width="100px">
        <el-row>
          <el-col span="6">
            <el-form-item label="物资名称:">
              <el-input v-model="registrationQuery.recordProductName" style="width: 90%" clearable
                        placeholder="物资名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="注册备案号:">
              <el-input v-model="registrationQuery.recordCode" style="width: 90%" clearable
                        placeholder="注册备案号"></el-input>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="注册备案人:">
              <el-input v-model="registrationQuery.recordPeopleName" style="width: 90%" clearable
                        placeholder="注册备案人"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch2">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset2">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit2">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="registrationDialog()">添加</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>


      <el-table v-loading="registrationLoading" :data="registrationList" style="width: 100%" border
                highlight-current-row>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="注册/备案产品名称" prop="recordProductName" show-overflow-tooltip></el-table-column>
        <el-table-column
          label="规格型号"
          prop="ggxh"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="注册/备案号" prop="recordCode" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案人名称" prop="recordPeopleName" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产企业" prop="manufacturerName" show-overflow-tooltip></el-table-column>
        <el-table-column label="所属供应商" prop="supName" show-overflow-tooltip></el-table-column>
        <el-table-column label="审核状态" prop="auditStatus" width="120">
          <template slot-scope="scope">
            <el-tag :type="(scope.row.auditStatus) | statusFilterType">
              {{ checkFlag[scope.row.auditStatus] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="registrationDialog(scope.row)">编辑</el-button>
            <el-button type="text" size="small" :disabled="scope.row.auditStatus === 1"
                       @click.native="deleteRegistration(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>


      <pagination
        v-show="certTotal>0"
        :total="certTotal"
        :page.sync="registrationQuery.page"
        :limit.sync="registrationQuery.limit"
        @pagination="certHandleCurrentChange"
      ></pagination>


      <el-dialog
        :title="formProductMap[formName]"
        :visible.sync="registrationVisible"
        width="80%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="registrationVisible"
        @close='closeProductDialog'
      >
        <suppliersRegistrationBasic
          :closeRegistrationDialog="closeProductDialog"
          :inputQuery="curProductQuery"
          :editType="editProductType"
          :currentManufacturer="currentManufacturer"
        ></suppliersRegistrationBasic>
      </el-dialog>
    </el-card>

  </div>
</template>

<script>
import {deleteCompany, getCompanyList, getCompanyBySup} from "../../../api/purchase/supManufacturer"
import suppliersEnterpriseBasicAdd from "@/views/purchase/manufacturer/supManufacturerEditDialog";
import {getRegistrationList, deleteRegistration} from "../../../api/purchase/suppliersRegistrationBasic";
import suppliersRegistrationBasic from "@/views/purchase/product/supProductEditDialog";

export default {
  name: "supCertificationAdd",
  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API,
      showSearch: true,
      showSearch2: true,
      /**------------------生产企业资质--------------------*/
      filterQuery: {
        customerId: null,
        companyName: "",
        auditStatus: null,
        creditCode: "",
        placeArea: "",
        page: 1,
        limit: 20,
      },
      total: 0,
      companyTypeMap: {
        1: "境内企业",
        2: "境外企业"
      },
      productManageTypeMap: {
        1: "Ⅰ类",
        2: "Ⅱ类",
        3: "Ⅲ类",
      },
      checkFlag: {
        0: "草稿",
        1: "已通过",
        2: "已拒绝",
        3: "变更未审核",
        6: "未审核",
      },
      loading: false,
      list: [],
      addInfoVisible: false,
      fileUrl: "",
      inputQuery: {

        auditStatus: 6
      },
      editType: 1,
      fromType: 1,//1:供应商，2:医院
      formMap: {
        add: "新增生产企业资质信息",
        update: "编辑生产企业资质信息",
      },
      currentManufacturer: null,


      /**------------------配送企业资质--------------------*/
      registrationVisible: false,
      enterpriseId: "",
      registrationQuery: {
        auditStatus: null,
        customerId: null,
        manufacturerIdFk: null,
        recordProductName: "",
        recordCode: "",
        recordPeopleName: "",
        page: 1,
        limit: 20
      },
      registrationList: [],
      certTotal: 0,
      registrationLoading: false,
      registrationId: "",

      pmQuery: {
        customerId: null,
        companyName: null,
        page: 1,
        limit: 10,
      },
      curProductQuery: {
        auditStatus: 6
      },
      editProductType: 0,
      formName: "add",
      formProductMap: {
        add: "新增配送产品资质信息",
        update: "编辑配送产品资质信息",
      },
    };
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        0: "warning",
        1: "success",
        2: "danger",
        3: "warning",
        6: "warning",
      };
      return statusMap[status];
    },
    statusFilterName(status) {
      const statusMap = {
        0: "禁用",
        1: "正常",
        2: "未验证",
      };
      return statusMap[status];
    },
  },
  methods: {
    /**-------------------------生产企业资质---------------------*/
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        customerId: null,
        companyName: "",
        creditCode: "",
        placeArea: "",
        auditStatus: "",
        page: 1,
        limit: 20,
      };
      this.getList();
      this.registrationList = [];
    },
    onSubmit() {
      this.filterQuery.page = 1
      this.getList();
      this.registrationList = [];
      this.currentManufacturer = null;
    },
    toViewRegistrationCert(row, num) {
      let path = num === 1 ? row.filePath : row.instructions;
      window.open(this.fileUrl + path);
    },
    getList() {
      this.loading = true;
      this.filterQuery.customerId = this.$store.getters.customerId
      getCompanyBySup(this.filterQuery)
        .then((response) => {
          this.loading = false;
          this.list = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },

    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    hideSearch2() {
      this.showSearch2 = !this.showSearch2;
    },
    addInfoDialog(row) {
      if (this.$isNotBlank(row)) {
        this.enterpriseId = row.id;
        this.editType = 1;
        this.inputQuery = row;
      } else {
        this.enterpriseId = null;
        this.editType = 0;
        this.inputQuery = {
          auditStatus: 0
        };
      }

      this.addInfoVisible = true;
    },
    closeDialog(type) {
      this.addInfoVisible = false;
      this.registrationVisible = false;
      this.enterpriseId = null;
      this.registrationId = null;
      this.getList();
      this.registrationList = [];
    },


    handleSizeChange(val) {
      this.filterQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    handleCheckedChange(val) {
      console.log(val);
      console.log(this.check);
    },

    rejectInfo(row) {
      this.$confirm(row.auditComment, "驳回说明", {
        confirmButtonText: "确定",
        type: "warning",
        showCancelButton: false,
      })
        .then(() => {
        });

    },

    deleteDialog(row) {
      this.$confirm("删除后将清空该生产企业以及所有关联信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let tQuery = {
            id: row.id,
          };
          deleteCompany(tQuery).then(() => {
            this.getList();
          });
        })
        .catch(() => {
        });
    },

    supManufacturerClick(row) {
      this.registrationQuery.customerId = row.customerId;
      this.registrationQuery.manufacturerIdFk = row.manufacturerId;
      this.getRegistrationList();
      this.currentManufacturer = row;
    },

    /**---------------------------配送产品资质----------------------*/

    onReset2() {
      //判断有没有选择配送企业
      if (this.currentManufacturer != null) {
        this.registrationQuery = {
          customerId: this.registrationQuery.customerId,
          manufacturerIdFk: this.registrationQuery.manufacturerIdFk,
          recordProductName: "",
          recordCode: "",
          recordPeopleName: "",
          auditStatus: null,
          page: 1,
          limit: 20
        };
        this.getRegistrationList();
      }
    },
    onSubmit2() {
      if (this.currentManufacturer == null) {
        this.$message.error("请先选中生产企业！");
        return
      }
      this.registrationQuery.page = 1;
      this.getRegistrationList();
    },
    registrationDialog(row) {
      if (this.currentManufacturer == null) {
        this.$message.error("请先选中生产企业！");
        return
      }

      if (this.$isNotBlank(row)) {
        this.registrationId = row.id;
        this.curProductQuery = row;
        this.curProductQuery.cuStatus = this.currentManufacturer.auditStatus;
        this.curProductQuery.manufacturerId = this.registrationQuery.manufacturerIdFk
        this.formName = "update";
        this.editProductType = 1;
      } else {
        this.editProductType = 0;
        this.formName = "add";
        this.curProductQuery = {
          auditStatus: 0,
          cuStatus: this.currentManufacturer.auditStatus,
          manufacturerId: this.registrationQuery.manufacturerIdFk
        };
      }
      this.registrationVisible = true;
    },
    deleteRegistration(row) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.certLoading = true;
        let tQuery = {
          id: row.id,
          filePath: row.filePath,
          filePath2: row.instructions,
        };
        deleteRegistration(tQuery)
          .then((response) => {
            this.certLoading = false;
            if (response.code === 20000) {
              this.getRegistrationList();
              this.$message({
                type: "success",
                message: "删除成功！",
              });
            } else {
              this.$message.error(response.message);
            }
          })
          .catch(() => {
            this.certLoading = false;
          });
      }).catch(() => {
      });
    },
    closeProductDialog() {
      this.registrationVisible = false;
      this.enterpriseId = null;
      this.registrationId = null;
      this.getRegistrationList();
    },
    getRegistrationList() {
      this.registrationLoading = true;
      getRegistrationList(this.registrationQuery)
        .then((response) => {
          this.registrationLoading = false;
          this.registrationList = response.data.list || [];
          this.certTotal = response.data.total;
        })
        .catch(() => {
          this.registrationLoading = false;
          this.registrationList = [];
        });
    },
    supChange(val) {
      this.pmQuery.customerId = val;
      this.findPMMethod();
    },
    certHandleCurrentChange(val) {
      this.registrationQuery.page = val.page;
      this.getRegistrationList();
    }

  },
  components: {
    suppliersEnterpriseBasicAdd, suppliersRegistrationBasic
  },
  mounted() {
  },
  created() {
    this.fileUrl = this.BASE_URL + "/udiwms/image/register/file/getImage?type=image4&name=";
    this.getList();
    // this.getRegistrationList();
  },
}
</script>

<style scoped>

</style>
