<template>
  <div class="app-container">
    <el-card >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="" prop="deptId">
<!--          <el-input-->
<!--            v-model="queryParams.deptId"-->
<!--            placeholder="请选择机构名称"-->
<!--            clearable-->
<!--            style="width: 240px;"-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
          <el-select v-model="queryParams.deptId" placeholder="请选择机构名称" clearable>
            <el-option
              v-for="item in deptListSelect"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="deptType">
          <el-select v-model="queryParams.deptType" placeholder="请选择机构类型" clearable>
            <el-option label="代理商" value="1"></el-option>
            <el-option label="连锁店" value="2"></el-option>
            <el-option label="基本门店" value="3"></el-option>
            <el-option label="三方机构" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="paymentChannel">
          <el-select v-model="queryParams.paymentChannel" placeholder="请选择支付通道" clearable>
            <el-option
              v-for="dict in dict.type.payment_channel"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择支付通道状态" clearable style="width: 200px;margin-right: 15px;">
            <el-option
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['setting:pay:add']"
          >新增支付参数</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!--    列表信息-->
    <el-card style="margin-top: 20px" >
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="deptName" label="机构名称"></el-table-column>
        <el-table-column prop="deptType" label="机构类型">
          <template slot-scope="scope">
            <div v-if="scope.row.deptType==1">代理商</div>
            <div v-if="scope.row.deptType==2">连锁店</div>
            <div v-if="scope.row.deptType==3">基本门店</div>
            <div v-if="scope.row.deptType==4">三方机构</div>
          </template>
        </el-table-column>
        <el-table-column prop="paymentChannel" label="支付通道"></el-table-column>
        <el-table-column prop="appKey" label="密钥">
          <el-table-column prop="insCd" label="机构号"> </el-table-column>
          <el-table-column prop="publicKey" label="公钥">
            <template slot-scope="scope">
              <div class="single-row">{{scope.row.publicKey}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="privateKey" label="私钥">
            <template slot-scope="scope">
              <div class="single-row">{{scope.row.privateKey}}</div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column prop="contact" label="联系人"></el-table-column>
        <el-table-column prop="information" label="联系方式"></el-table-column>
        <el-table-column prop="paymentChannelType" label="支付通道状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="qy"
              inactive-value="jy"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>

          </template>
        </el-table-column>
        <el-table-column prop="realName" label="创建人"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="edit(scope.row)"
              v-hasPermi="['setting:pay:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="del(scope.row)"
              v-hasPermi="['setting:pay:delete']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </el-card>

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="50%" append-to-body>
        <el-form :model="deviceInfo" ref="deviceForm" label-width="120px"  :rules="rules">

          <el-form-item label="机构名称" prop="deptId">
            <el-select v-model="deviceInfo.deptId" placeholder="请选择机构" @change="chooseDept(deviceInfo.deptId)">
              <el-option
                v-for="item in deptListSelect"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机构类型" prop="deptType">
            <el-select v-model="deviceInfo.deptType" placeholder="请选择机构类型" clearable disabled>
              <el-option label="代理商" value="1"></el-option>
              <el-option label="连锁店" value="2"></el-option>
              <el-option label="基本门店" value="3"></el-option>
              <el-option label="三方机构" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="支付通道" prop="paymentChannel">
            <el-select v-model="deviceInfo.paymentChannel" placeholder="请选择支付通道" clearable>
              <el-option
                v-for="dict in dict.type.payment_channel"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="机构号" prop="insCd">
            <el-input v-model="deviceInfo.insCd"></el-input>
          </el-form-item>
          <el-form-item label="公钥" prop="publicKey">
            <el-input v-model="deviceInfo.publicKey"></el-input>
          </el-form-item>
          <el-form-item label="私钥" prop="privateKey">
            <el-input v-model="deviceInfo.privateKey"></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="deviceInfo.contact"></el-input>
          </el-form-item>
          <el-form-item label="联系方式" prop="information">
            <el-input v-model="deviceInfo.information"></el-input>
          </el-form-item>
          <el-form-item label="支付状态" prop="appStatus">
            <el-switch
              v-model="deviceInfo.status"
              active-value="qy"
              inactive-value="jy"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('deviceForm')">提交</el-button>
          </el-form-item>
        </el-form>
    </el-dialog>

  </div>
</template>

<script>


import {deleteAppApi, getAppListApi, saveAppApi, updateAppApi} from "@/api/sys/app";
import {selectChildByDeptId} from "@/api/system/Site/site";
import {addMerchantApi, editMerchantRemark, getMerchant, listMerchantPage} from "@/api/payConfig/merchantconfig";
import {
  addPaymentParameter, deletePaymentParameter,
  editPaymentParameter,
  getPaymentParameter,
  listPaymentParameterPage
} from "@/api/payConfig/paymentParameter";

export default {
  name: "printIndex",

  dicts: ['zhzt','payment_channel'],
  data() {
    return {
      tableData: [],
      deviceInfo: {
        deptId:'',
        deptType:'',
        paymentChannel:'',
        publicKey:'',
        contact:'',
        information:'',
        paymentChannelType:'',
        status:"qy"
      },
      deptListSelect:[],

      // 是否显示弹出层
      open: false,
      title:"",
      total:0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
      },
      rules: {
        deptId: [
          {required: true, message: '请选择机构名称', trigger: 'blur'},
        ],
        deptType: [
          {required: true, message: '请选择机构类型', trigger: 'blur'},
        ],
        paymentChannel: [
          {required: true, message: '请选择支付通道', trigger: 'blur'},
        ],
        insCd: [
          {required: true, message: '请填写机构号', trigger: 'blur'},
        ],
        publicKey: [
          {required: true, message: '请填写公钥信息', trigger: 'blur'},
        ],
        privateKey: [
          {required: true, message: '请填写私钥信息', trigger: 'blur'},
        ],
        contact: [
          {required: true, message: '请填写联系人信息', trigger: 'blur'},
        ],
        information: [
          {required: true, message: '请填写联系人方式', trigger: 'blur'},
        ],
      }
    };
  },
  created() {
    this.getList()
    this.selectChildByDeptIdApi()
  },
  methods: {
    chooseDept(deptId){
      this.deptListSelect.forEach(item => {
        if (item.deptId == deptId){
          this.deviceInfo.deptType = item.deptType
        }
      })
    },
    getList(){
      listPaymentParameterPage(this.queryParams).then(res=>{
        this.tableData = res.data.records;
        this.total = res.data.total
      })
    },
    getsSubmitDeviceInfo(){},

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.deviceInfo.merchantName = this.deviceInfo.paymentChannel
          // 表单验证通过，可以提交数据
          // 示例：调用 API 提交数据
          this.submitDeviceInfo();
        } else {
          // 表单验证失败，不执行任何操作
          return false;
        }
      });
    },
    submitDeviceInfo() {
      // 在这里调用接口提交支付参数信息数据
      if (this.deviceInfo.id) {
        editPaymentParameter(this.deviceInfo).then(res => {
          if (res.data === 1) {
            this.$modal.msgSuccess("修改成功")
            this.getList()
            this.open = false
          } else {
            this.$modal.msgError("修改失败，当前机构的支付通道已存在！")
          }
        })
      }else {
        addPaymentParameter(this.deviceInfo).then(res => {
          if (res.data == 1) {
            this.$modal.msgSuccess("添加成功");
            this.getList()
            this.open = false
          } else {
            this.$modal.msgError("添加失败，当前机构的支付通道已存在！")
          }
        })
      }
    },
    handleQuery(){
      this.getList()
    },
    resetQuery(){
      this.queryParams = {
        page: 1,
        pageSize: 10,
        deviceName:'',
      },
      this.getList()
    },
    handleAdd(e) {
      this.clean()
      this.open = true;
      this.title = "增加支付参数"
      // this.selectChildByDeptIdApi();
    },
    edit(e) {
      this.clean()
      // this.open = true;
      // this.title = "编辑支付参数"
      // this.deviceInfo = e
      getPaymentParameter(e.id).then(res => {
        this.open = true;
        this.title = "编辑支付参数"
        this.deviceInfo = res.data
      })
    },
    selectChildByDeptIdApi() {
      selectChildByDeptId().then(res=>{
        this.deptListSelect = res.data
      })
    },

    del(e) {

      this.$confirm('此操作将永久删除该支付参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePaymentParameter(e.id).then(res=>{
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });



    },
    clean() {
      this.deviceInfo= {
        id: null,            // 自增id
        deptId:'',
        deptType:'',
        paymentChannel:'',
        publicKey:'',
        contact:'',
        information:'',
        status:'qy'
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
.app-container{
  height: 100%;
}
.single-row {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>

