<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="单位名称" prop="contractorName">
        <el-input
          v-model="queryParams.contractorName"
          placeholder="请输入承包商单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承包商评定" prop="contractorEvaluate">
        <el-select v-model="queryParams.contractorEvaluate" placeholder="请选择承包商评定" clearable size="small">
          <el-option
            v-for="dict in evaluateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="承包商状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择承包商状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractorInfoList">
      <el-table-column label="单位名称" align="center" prop="contractorName" />
      <el-table-column label="主要负责人" align="center" prop="keyPerson" >
        <template slot-scope="scope">
          <span v-if="scope.row.keyPerson != null && scope.row.keyPerson != ''">{{scope.row.keyPerson}}</span>
          <span v-else>
            -
          </span>
        </template>
      </el-table-column>
      <el-table-column label="主要负责人手机" align="center" prop="keyPersonPhone" >
        <template slot-scope="scope">
          <span v-if="scope.row.keyPersonPhone != null && scope.row.keyPersonPhone != ''">{{scope.row.keyPersonPhone}}</span>
          <span v-else>
            -
          </span>
        </template>
      </el-table-column>
      <el-table-column label="单位资质" align="center" prop="certificateUrl" style="text-align:center;">
        <template slot-scope="scope">
          <span v-if="scope.row.certificateUrl != null && scope.row.certificateUrl != ''">
            <img :src="scope.row.certificateUrl" style="width: 20%;vertical-align:middle;cursor:pointer;" @click="showPicture(scope.row)"/>
            <el-image :ref="'a'+scope.row.id" :src="scope.row.certificateUrl" v-show="false" :preview-src-list="[scope.row.certificateUrl]" v-if="scope.row.certificateUrl != '' && scope.row.certificateUrl != null"></el-image>
          </span>
          <span v-else>
            -
          </span>
        </template>
      </el-table-column>
      <el-table-column label="承包商评定" align="center" prop="contractorEvaluate" :formatter="evaluateFormat" />
      <el-table-column label="承包商状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleApproval(scope.row)"
          >审核</el-button>
          <el-button
            v-if="scope.row.status != '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBlacklist(scope.row)"
          >加入黑名单</el-button>
          <el-button
            v-if="scope.row.status == '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBlacklist(scope.row)"
          >移出黑名单</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改承包商信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="23">
            <el-form-item label="单位名称" prop="contractorName">
              <el-input v-model="form.contractorName" placeholder="请输入承包商单位名称" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="法定代表人" prop="legalPerson">
              <el-input v-model="form.legalPerson" placeholder="请输入法定代表人" :disabled="readOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法定代表人手机" prop="legalPersonPhone">
              <el-input v-model="form.legalPersonPhone" placeholder="请输入法定代表人手机" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="主要负责人" prop="keyPerson">
              <el-input v-model="form.keyPerson" placeholder="请输入主要负责人" :disabled="readOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主要负责人手机" prop="keyPersonPhone">
              <el-input v-model="form.keyPersonPhone" placeholder="请输入主要负责人手机" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="安全负责人" prop="safetyPerson">
              <el-input v-model="form.safetyPerson" placeholder="请输入安全负责人" :disabled="readOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安全负责人手机" prop="safetyPersonPhone">
              <el-input v-model="form.safetyPersonPhone" placeholder="请输入安全负责人手机" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="单位资质名称" prop="certificateName">
              <el-input v-model="form.certificateName" placeholder="请输入单位资质证照名称" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="单位资质证照" v-if="!readOnly" prop="certificateUrl">
              <MyFileUpload
                listType="picture-card"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="fileList"
              />
              <el-input v-show="false" disabled v-model="form.certificateUrl"></el-input>
            </el-form-item>
            <el-form-item label="单位资质证照" v-if="readOnly" prop="certificateUrl">
              <img :src="form.certificateUrl" style="width: 20%;vertical-align:middle;cursor:pointer;" @click="showPicture(form)"/>
              <el-image :zIndex="9999" :ref="'a'+form.id" :src="form.certificateUrl" v-show="false" :preview-src-list="[form.certificateUrl]" v-if="form.certificateUrl != '' && form.certificateUrl != null"></el-image>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="团队信息记录" prop="teamInformation">
              <el-input type="textarea" v-model="form.teamInformation" placeholder="请输入团队信息记录" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="违章记录" prop="violationRecords">
              <el-input v-model="form.violationRecords" type="textarea" placeholder="请输入违章记录" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="承包商评定" prop="contractorEvaluate">
              <el-radio-group v-model="form.contractorEvaluate" :disabled="readOnly">
                <el-radio label="1">优</el-radio>
                <el-radio label="2">中</el-radio>
                <el-radio label="3">差</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item v-show="operate" label="审核" prop="status">
              <el-radio v-model="form.status" label="1">通过</el-radio>
              <el-radio v-model="form.status" label="0">驳回</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import MyFileUpload from '@/components/MyFileUpload';
import { listContractorInfo, getContractorInfo, delContractorInfo, addContractorInfo, updateContractorInfo, exportContractorInfo } from "@/api/contractor/contractorInfo";

export default {
  name: "ContractorInfo",
  components: {
    MyFileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承包商信息表格数据
      contractorInfoList: [],
      evaluateOptions: [],
      statusOptions: [],
      // 文件列表
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      operate: false,
      readOnly: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractorName: null,
        certificateName: null,
        certificateUrl: null,
        teamInformation: null,
        contractorEvaluate: null,
        violationRecords: null,
        legalPerson: null,
        legalPersonPhone: null,
        keyPerson: null,
        keyPersonPhone: null,
        safetyPerson: null,
        safetyPersonPhone: null,
        status: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractorName: [
          { required: true, message: "请输入单位名称", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_contractor_evaluate").then(response => {
      this.evaluateOptions = response.data;
    });
    this.getDicts("t_contractor_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    // 承包商评定
    evaluateFormat(row, column) {
      return this.selectDictLabel(this.evaluateOptions, row.contractorEvaluate);
    },
    // 承包商状态
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询承包商信息列表 */
    getList() {
      this.loading = true;
      listContractorInfo(this.queryParams).then(response => {
        this.contractorInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractorName: null,
        certificateName: null,
        certificateUrl: null,
        teamInformation: null,
        contractorEvaluate: null,
        violationRecords: null,
        legalPerson: null,
        legalPersonPhone: null,
        keyPerson: null,
        keyPersonPhone: null,
        safetyPerson: null,
        safetyPersonPhone: null,
        status: null,
        isDel: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
      this.fileList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.readOnly=false;
      this.reset();
      this.open = true;
      this.title = "添加承包商信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.readOnly=false;
      this.reset();
      const id = row.id || this.ids
      getContractorInfo(id).then(response => {
        this.form = response.data;
        if (this.form.certificateUrl) {
          this.fileList.push({
            url: this.form.certificateUrl,
          });
        }
        this.open = true;
        this.title = "修改承包商信息";
      });
    },
    /** 审核按钮操作 */
    handleApproval(row) {
      this.readOnly = true;
      this.reset();
      getContractorInfo(row.id).then(response => {
        this.form = response.data;
        this.form.status = '1';
        this.operate = true;
        this.open = true;
        this.title = "审批法律法规信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if(this.operate){
              updateContractorInfo(this.form).then(response => {
                this.msgSuccess("审核成功");
                this.open = false;
                this.operate = false;
                this.readOnly = false;
                this.getList();
              });
            } else {
              updateContractorInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addContractorInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 加入黑名单按钮操作 */
    handleBlacklist(row) {
      // const ids = row.id || this.ids;
      var confirm = '';
      if(row.status != '2'){
        confirm = '是否确认将"' + row.contractorName + '"加入黑名单?';
      } else {
        confirm = '是否确认将"' + row.contractorName + '"移出黑名单?';
      }
      this.$confirm(confirm, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        if(row.status != '2'){
          row.status = '2';
        } else {
          row.status = '0';
        }
          return updateContractorInfo(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids;
      this.$confirm('是否确认删除"' + row.certificateName + '"的承包商信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          row.isDel = "1";
          return updateContractorInfo(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    getFileInfo(res){
      this.form.certificateUrl = res.url;
      //this.$set(this.feedBookForm,'iconUrl',res.url)
    },
    listRemove(e) {
      this.form.certificateUrl = "";
      this.fileList = [];
    },
    showPicture(row){
      this.$refs['a'+row.id].showViewer = true;
      console.log("===",row.id);
    },
  }
};
</script>