<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="供应商名称" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商简称" prop="vendorNick">
        <el-input
          v-model="queryParams.vendorNick"
          placeholder="请输入供应商简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商英文名称" prop="vendorEn">
        <el-input
          v-model="queryParams.vendorEn"
          placeholder="请输入供应商英文名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商LOGO地址" prop="vendorLogo">
        <el-input
          v-model="queryParams.vendorLogo"
          placeholder="请输入供应商LOGO地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商评分" prop="vendorScore">
        <el-input
          v-model="queryParams.vendorScore"
          placeholder="请输入供应商评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商官网地址" prop="website">
        <el-input
          v-model="queryParams.website"
          placeholder="请输入供应商官网地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商邮箱地址" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入供应商邮箱地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入供应商电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人1" prop="contact1">
        <el-input
          v-model="queryParams.contact1"
          placeholder="请输入联系人1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人1-电话" prop="contact1Tel">
        <el-input
          v-model="queryParams.contact1Tel"
          placeholder="请输入联系人1-电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人1-邮箱" prop="contact1Email">
        <el-input
          v-model="queryParams.contact1Email"
          placeholder="请输入联系人1-邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人2" prop="contact2">
        <el-input
          v-model="queryParams.contact2"
          placeholder="请输入联系人2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人2-电话" prop="contact2Tel">
        <el-input
          v-model="queryParams.contact2Tel"
          placeholder="请输入联系人2-电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人2-邮箱" prop="contact2Email">
        <el-input
          v-model="queryParams.contact2Email"
          placeholder="请输入联系人2-邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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"
          v-hasPermi="['md:vendor:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:vendor:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:vendor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:vendor:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-link"
          size="mini"
          :disabled="single"
          @click="handleSycLog"
        >数据流向</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vendorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商编码" align="center" prop="vendorCode" width="100"/>
      <el-table-column label="供应商名称" align="center" prop="vendorName" width="100"/>
      <el-table-column label="供应商简称" align="center" prop="vendorNick" width="100"/>
      <el-table-column label="供应商英文名称" align="center" prop="vendorEn" width="140"/>
      <el-table-column label="供应商简介" align="center" prop="vendorDes" width="100"/>
      <el-table-column label="供应商LOGO地址" align="center" prop="vendorLogo" width="140"/>
      <el-table-column label="供应商等级" align="center" prop="vendorLevel" width="100"/>
      <el-table-column label="供应商评分" align="center" prop="vendorScore" width="100"/>
      <el-table-column label="供应商地址" align="center" prop="address" width="100"/>
      <el-table-column label="供应商官网地址" align="center" prop="website" width="140"/>
      <el-table-column label="供应商邮箱地址" align="center" prop="email" width="140"/>
      <el-table-column label="供应商电话" align="center" prop="tel" width="100"/>
      <el-table-column label="联系人1" align="center" prop="contact1" width="100"/>
      <el-table-column label="联系人1-电话" align="center" prop="contact1Tel" width="140"/>
      <el-table-column label="联系人1-邮箱" align="center" prop="contact1Email" width="140"/>
      <el-table-column label="联系人2" align="center" prop="contact2" width="100"/>
      <el-table-column label="联系人2-电话" align="center" prop="contact2Tel" width="140"/>
      <el-table-column label="联系人2-邮箱" align="center" prop="contact2Email" width="140"/>
      <el-table-column label="统一社会信用代码" align="center" prop="creditCode" width="160"/>
      <el-table-column label="是否启用" align="center" prop="enableFlag" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:vendor:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:vendor:remove']"
          >删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="供应商编码" prop="vendorCode">
          <el-input v-model="form.vendorCode" placeholder="请输入供应商编码" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="vendorName">
          <el-input v-model="form.vendorName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="供应商简称" prop="vendorNick">
          <el-input v-model="form.vendorNick" placeholder="请输入供应商简称" />
        </el-form-item>
        <el-form-item label="供应商英文名称" prop="vendorEn">
          <el-input v-model="form.vendorEn" placeholder="请输入供应商英文名称" />
        </el-form-item>
        <el-form-item label="供应商简介" prop="vendorDes">
          <el-input v-model="form.vendorDes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="供应商LOGO地址" prop="vendorLogo">
          <el-input v-model="form.vendorLogo" placeholder="请输入供应商LOGO地址" />
        </el-form-item>
        <el-form-item label="供应商评分" prop="vendorScore">
          <el-input v-model="form.vendorScore" placeholder="请输入供应商评分" />
        </el-form-item>
        <el-form-item label="供应商地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="供应商官网地址" prop="website">
          <el-input v-model="form.website" placeholder="请输入供应商官网地址" />
        </el-form-item>
        <el-form-item label="供应商邮箱地址" prop="email">
          <el-input v-model="form.email" placeholder="请输入供应商邮箱地址" />
        </el-form-item>
        <el-form-item label="供应商电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入供应商电话" />
        </el-form-item>
        <el-form-item label="联系人1" prop="contact1">
          <el-input v-model="form.contact1" placeholder="请输入联系人1" />
        </el-form-item>
        <el-form-item label="联系人1-电话" prop="contact1Tel">
          <el-input v-model="form.contact1Tel" placeholder="请输入联系人1-电话" />
        </el-form-item>
        <el-form-item label="联系人1-邮箱" prop="contact1Email">
          <el-input v-model="form.contact1Email" placeholder="请输入联系人1-邮箱" />
        </el-form-item>
        <el-form-item label="联系人2" prop="contact2">
          <el-input v-model="form.contact2" placeholder="请输入联系人2" />
        </el-form-item>
        <el-form-item label="联系人2-电话" prop="contact2Tel">
          <el-input v-model="form.contact2Tel" placeholder="请输入联系人2-电话" />
        </el-form-item>
        <el-form-item label="联系人2-邮箱" prop="contact2Email">
          <el-input v-model="form.contact2Email" placeholder="请输入联系人2-邮箱" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </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>

    <!-- 数据流向对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-table v-loading="loading" :data="logList" >
        <el-table-column label="目标系统" align="center" prop="targetSys">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.related_system" :value="scope.row.targetSys"/>
          </template>
        </el-table-column>
        <el-table-column label="操作类型" align="center" prop="dataOpr">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.md_syc_type" :value="scope.row.dataOpr"/>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="operTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.operTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="logParams.pageNum"
        :limit.sync="logParams.pageSize"
        @pagination="handleSycLog"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel2">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVendor, getVendor, delVendor, addVendor, updateVendor } from "@/api/md/vendor";
import { listLog } from "@/api/md/log";

export default {
  name: "Vendor",
  dicts: ['related_system', 'md_syc_type', 'md_data_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商表格数据
      vendorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 主数据流向表格数据
      logList: [],
      // 弹出层标题
      title2: "",
      // 是否显示弹出层
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        vendorEn: null,
        vendorDes: null,
        vendorLogo: null,
        vendorLevel: null,
        vendorScore: null,
        address: null,
        website: null,
        email: null,
        tel: null,
        contact1: null,
        contact1Tel: null,
        contact1Email: null,
        contact2: null,
        contact2Tel: null,
        contact2Email: null,
        creditCode: null,
        enableFlag: null,
      },
      logParams: {
        pageNum: 1,
        pageSize: 10,
        dataId:null,
        dataType: null,
        dataOpr: null,
        targetSys: null,
        operTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vendorCode: [
          { required: true, message: "供应商编码不能为空", trigger: "change" }
        ],
        vendorName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        vendorLevel: [
          { required: true, message: "供应商等级不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "供应商地址不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "供应商邮箱地址不能为空", trigger: "blur" }
        ],
        tel: [
          { required: true, message: "供应商电话不能为空", trigger: "blur" }
        ],
        contact1: [
          { required: true, message: "联系人1不能为空", trigger: "blur" }
        ],
        contact1Tel: [
          { required: true, message: "联系人1-电话不能为空", trigger: "blur" }
        ],
        creditCode: [
          { required: true, message: "统一社会信用代码不能为空", trigger: "blur" }
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商列表 */
    getList() {
      this.loading = true;
      listVendor(this.queryParams).then(response => {
        this.vendorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2() {
      this.open2 = false;
    },
    // 表单重置
    reset() {
      this.form = {
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        vendorEn: null,
        vendorDes: null,
        vendorLogo: null,
        vendorLevel: null,
        vendorScore: null,
        address: null,
        website: null,
        email: null,
        tel: null,
        contact1: null,
        contact1Tel: null,
        contact1Email: null,
        contact2: null,
        contact2Tel: null,
        contact2Email: null,
        creditCode: null,
        enableFlag: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vendorId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vendorId = row.vendorId || this.ids
      getVendor(vendorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商";
      });
    },
    handleSycLog(row) {
      // 总条数
      total: 0;
      this.loading = true;
      const vendorId = row.vendorId || this.ids[0]
      this.logParams.dataId = vendorId;
      listLog(this.logParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.open2 = true;
        this.title2 = "数据流向";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vendorId != null) {
            updateVendor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVendor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vendorIds = row.vendorId || this.ids;
      this.$modal.confirm('是否确认删除供应商编号为"' + vendorIds + '"的数据项？').then(function() {
        return delVendor(vendorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/vendor/export', {
        ...this.queryParams
      }, `vendor_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
