<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="门店名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入门店手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属配送商" prop="dadaDspId" v-if="userType == 1 || userType == 3">
        <el-select v-model="queryParams.dadaDspId" placeholder="请选择" filterable clearable >
          <el-option v-for="item in listDadaDsp" :key="item.id" :value="item.id" :label="item.name"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" filterable clearable >
          <el-option v-for="item in statusOption" :key="item.id" :value="item.id" :label="item.name"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="shopVerifyList" @selection-change="handleSelectionChange" stripe border highlight-current-row style="width: 100%">
      <el-table-column label="门店名称" align="center" prop="name" />
      <el-table-column label="商家编号" align="center" prop="thirdShopId" />
      <el-table-column label="门店编号" align="center" prop="dadaShopNo" />
      <el-table-column label="所属配送商" align="center" prop="dadaDspName" />
      <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.status == 0">未绑定</el-tag>
            <el-tag type="success" v-if="scope.row.status == 1">绑定</el-tag>
            <el-tag v-if="scope.row.status == 2">审核中</el-tag>
            <el-tag type="danger" v-if="scope.row.status == 3">审核失败</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="余额" align="center" prop="balance"/>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
           v-if="(userType == 4 || userType == 5 || userType == 1 || userType == 3) && scope.row.status != 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="商户编号" prop="thirdShopId">
          <el-input style="width:500px" v-model="form.thirdShopId" placeholder="请输入商户编号" :disabled="this.oldThirdShopId != undefined && form.status != 3" />
        </el-form-item>
        <el-form-item label="门店编号" prop="dadaShopNo">
          <el-input style="width:500px" v-model="form.dadaShopNo" placeholder="请输入门店编号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="account">
          <el-input style="width:500px" v-model="form.account" 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 { listShopVerify, updateDadaVerify } from "@/api/module/merchant"
import { getDadaDspList } from "@/api/module/dadaDsp";
import user from '@/store/modules/user'

export default {
  name: "dadaVerify",
  components: {

  },
  data() {
    return {
      statusOption: [
          {
              id: 1,
              name: "绑定"
          },{
              id: 0,
              name: "未绑定"
          },{
              id: 2,
              name: "审核中"
          },{
              id: 3,
              name: "拒绝"
          },{
              id: 4,
              name: "待完善资料"
          }
      ],
      user: {},
      // 配送商
      listDadaDsp: [],
      // 遮罩层
      loading: true,
      oldThirdShopId: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家门店表格数据
      shopVerifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 登录用户类型
      userType: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        dadaDspId: undefined,
        status: undefined
      },
 
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        thirdShopId: [
          { required: true, message: "商家编号不能为空", trigger: "blur" }
        ],
        dadaShopNo: [
          { required: true, message: "门店编号不能为空", trigger: "blur" }
        ],
        account: [
          { required: true, message: "注册手机号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.user = user.actions.getSysUser()
    this.userType = localStorage.getItem("SYS_USER_TYPE")
    this.getList();
    getDadaDspList().then(response => {
      this.listDadaDsp = response.data
    });
  },
  methods: {
    /** 查询商家门店列表 */
    getList() {
      this.loading = true;
      listShopVerify(this.queryParams).then(response => {
        this.shopVerifyList = response.data.data;
        this.total = response.data.totalNums;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        thirdShopId: undefined,
        dadaShopNo: undefined,
        mobile: undefined,
        account: undefined,
        status: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家门店";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('达达信息', row)
      this.reset();
      this.form.id = row.id
      this.oldThirdShopId = row.thirdShopId
      this.form.thirdShopId = row.thirdShopId
      this.form.account = row.account
      this.form.status = row.status
      this.open = true;
      this.title = "修改达达门店资料";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDadaVerify(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } 
        }
      });
    },
  }
};
</script>
