<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">

      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商电话" prop="supplierPhone">
        <el-input
          v-model="queryParams.supplierPhone"
          placeholder="请输入供应商电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号" prop="supplierAccount">
        <el-input
          v-model="queryParams.supplierAccount"
          placeholder="请输入账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商状态" prop="supplierStatus">
        <el-select v-model="queryParams.supplierStatus" placeholder="请选择供应商状态" clearable size="small">
          <el-option label="正常" value="0" />
          <el-option label="冻结" value="1" />
        </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>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['mall:mallShopSupplier:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallShopSupplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="供应商主键ID" align="center" prop="supplierId" />-->
<!--      <el-table-column label="所属商家ID" align="center" prop="shopId" />-->
<!--      <el-table-column label="系统用户ID" align="center" prop="sysUserId" />-->
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="供应商电话" align="center" prop="supplierPhone" />
      <el-table-column label="账号" align="center" prop="supplierAccount" />
      <el-table-column label="密码" align="center" prop="supplierPassword" />
<!--      <el-table-column label="省份ID" align="center" prop="provinceId" />-->
<!--      <el-table-column label="省份名称" align="center" prop="provinceName" />-->
<!--      <el-table-column label="城市ID" align="center" prop="cityId" />-->
<!--      <el-table-column label="城市名称" align="center" prop="cityName" />-->
<!--      <el-table-column label="区域ID" align="center" prop="areaId" />-->
<!--      <el-table-column label="区域名称" align="center" prop="areaName" />-->
<!--      <el-table-column label="地址" align="center" prop="address" />-->
<!--      <el-table-column label="经度" align="center" prop="longitude" />-->
<!--      <el-table-column label="纬度" align="center" prop="latitude" />-->
      <el-table-column label="供应商状态" align="center" prop="supplierStatus" >
        <template slot-scope="scope">
          <div v-if="scope.row.supplierStatus == 0">正常</div>
          <div v-if="scope.row.supplierStatus == 1">冻结</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createAt" width="160px" />
<!--      <el-table-column label="更新时间" align="center" prop="updateAt" />-->
      <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)"
          >修改</el-button>
          <el-button
            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="100px">
<!--        <el-form-item label="所属商家ID" prop="shopId">-->
<!--          <el-input v-model="form.shopId" placeholder="请输入所属商家ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="系统用户ID" prop="sysUserId">-->
<!--          <el-input v-model="form.sysUserId" placeholder="请输入系统用户ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="供应商电话" prop="supplierPhone">
          <el-input v-model="form.supplierPhone" placeholder="请输入供应商电话" />
        </el-form-item>
        <el-form-item label="账号" prop="supplierAccount">
          <el-input v-model="form.supplierAccount" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="supplierPassword">
          <el-input v-model="form.supplierPassword" placeholder="请输入密码" />
        </el-form-item>
<!--        <el-form-item label="省份ID" prop="provinceId">-->
<!--          <el-input v-model="form.provinceId" placeholder="请输入省份ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="省份名称" prop="provinceName">-->
<!--          <el-input v-model="form.provinceName" placeholder="请输入省份名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="城市ID" prop="cityId">-->
<!--          <el-input v-model="form.cityId" placeholder="请输入城市ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="城市名称" prop="cityName">-->
<!--          <el-input v-model="form.cityName" placeholder="请输入城市名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区域ID" prop="areaId">-->
<!--          <el-input v-model="form.areaId" placeholder="请输入区域ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区域名称" prop="areaName">-->
<!--          <el-input v-model="form.areaName" placeholder="请输入区域名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="地址" prop="address">-->
<!--          <el-input v-model="form.address" placeholder="请输入地址" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="经度" prop="longitude">-->
<!--          <el-input v-model="form.longitude" placeholder="请输入经度" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="纬度" prop="latitude">-->
<!--          <el-input v-model="form.latitude" placeholder="请输入纬度" />-->
<!--        </el-form-item>-->
        <el-form-item label="供应商状态">
          <el-radio-group v-model="form.supplierStatus">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">冻结</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="创建时间" prop="createAt">-->
<!--          <el-input v-model="form.createAt" placeholder="请输入创建时间" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="更新时间" prop="updateAt">-->
<!--          <el-input v-model="form.updateAt" placeholder="请输入更新时间" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="删除标记" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" 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>
  </div>
</template>

<script>
import { listMallShopSupplier, getMallShopSupplier, delMallShopSupplier, addMallShopSupplier, updateMallShopSupplier, exportMallShopSupplier } from "@/api/mall/mallShopSupplier";

export default {
  name: "MallShopSupplier",
  props: ["shopId"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家供应商管理表格数据
      mallShopSupplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: null,
        sysUserId: null,
        supplierName: null,
        supplierPhone: null,
        supplierAccount: null,
        supplierPassword: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        longitude: null,
        latitude: null,
        supplierStatus: null,
        createAt: null,
        updateAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierName: [
          { required: true, trigger: "blur", message: "请输入供应商名称" }
        ],
        supplierPhone: [
          { required: true, trigger: "blur", message: "请输入供应商电话" }
        ],
        supplierAccount: [
          { required: true, trigger: "blur", message: "请输入账号" }
        ],
        supplierPassword: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商家供应商管理列表 */
    getList() {
      this.loading = true;
      if (this.shopId != null){
        this.queryParams.shopId = this.shopId;
      }
      listMallShopSupplier(this.queryParams).then(response => {
        this.mallShopSupplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        shopId: null,
        sysUserId: null,
        supplierName: null,
        supplierPhone: null,
        supplierAccount: null,
        supplierPassword: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        longitude: null,
        latitude: null,
        supplierStatus: 0,
        createAt: null,
        updateAt: null,
        delFlag: 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.supplierId)
      this.sNames = selection.map(item => item.supplierName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家供应商管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids
      getMallShopSupplier(supplierId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家供应商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.shopId = this.shopId;
        if (valid) {
          if (this.form.supplierId != null) {
            updateMallShopSupplier(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallShopSupplier(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierIds = row.supplierId || this.ids;
      const supplierNames = row.supplierName || this.sNames;
      this.$confirm('是否确认删除供应商"' + supplierNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMallShopSupplier(supplierIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商家供应商管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMallShopSupplier(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
