<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="orgName">
          <el-input
            v-model="queryParams.orgName"
            placeholder="请输入机构名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="legalRepresentativeContact">
          <el-input
            v-model="queryParams.legalRepresentativeContact"
            placeholder="请输入法人联系方式"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="appStatus">
          <el-input
            v-model="queryParams.appStatus"
            placeholder="请选择小程序状态"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </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"
          >新增小程序配置</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="机构类型"></el-table-column>
        <el-table-column prop="paymentChannel" label="支付通道"></el-table-column>
        <el-table-column prop="appKey" label="密钥">
          <el-table-column prop="publicKey" label="公钥"></el-table-column>
          <el-table-column prop="privateKey" label="私钥"></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="支付通道状态"></el-table-column>
        <el-table-column prop="createByName" 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)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="del(scope.row)"
            >删除</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 ref="form" :model="deviceInfo" label-width="80px">
        <el-form :model="deviceInfo" ref="deviceForm" label-width="120px">
          <el-form-item label="机构名称" prop="orgName">
            <el-input v-model="deviceInfo.orgName" style="width: 300px"></el-input>
          </el-form-item>


          <el-form-item label="机构名称" prop="unifiedSocialCreditCode">
            <el-select v-model="deviceInfo.deptId" multiple placeholder="请选择机构">
              <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="appId">
            <el-input v-model="deviceInfo.appId"></el-input>
          </el-form-item>
          <el-form-item label="支付通道" prop="appKey">
            <el-input v-model="deviceInfo.appKey"></el-input>
          </el-form-item>
          <el-form-item label="公钥" prop="legalRepresentativeName">
            <el-input v-model="deviceInfo.legalRepresentativeName"></el-input>
          </el-form-item>
          <el-form-item label="私钥" prop="legalRepresentativeWeChat">
            <el-input v-model="deviceInfo.legalRepresentativeWeChat"></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="legalRepresentativeContact">
            <el-input v-model="deviceInfo.legalRepresentativeContact"></el-input>
          </el-form-item>
          <el-form-item label="支付状态" prop="appStatus">
            <el-switch
              v-model="deviceInfo.appStatus"
              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-form>
    </el-dialog>

  </div>
</template>

<script>


import {deleteAppApi, getAppListApi, saveAppApi, updateAppApi} from "@/api/sys/app";
import {selectChildByDeptId} from "@/api/system/Site/site";

export default {
  name: "printIndex",

  data() {
    return {
      tableData: [],
      deviceInfo: {
        deptName:'',
        deptType:'',
        paymentChannel:'',
        publicKey:'',
        contact:'',
        information:'',
        paymentChannelType:'',
        createByName:'',
        createTime:'',
      },
      deptListSelect:[],

      // 是否显示弹出层
      open: false,
      title:"",
      total:0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        orgName:'',
        legalRepresentativeContact:'',
        appStatus:'',
      },
    };
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      getAppListApi(this.queryParams).then(res=>{
        this.tableData = res.data.records;
        this.total = res.data.total
      })
    },
    getsSubmitDeviceInfo(){},

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 表单验证通过，可以提交数据
          // 示例：调用 API 提交数据
          this.submitDeviceInfo();
        } else {
          // 表单验证失败，不执行任何操作
          return false;
        }
      });
    },
    submitDeviceInfo() {
      // 在这里调用接口提交设备信息数据
      console.log('提交设备信息数据:', this.deviceInfo);
      if (this.deviceInfo.id) {
        updateAppApi(this.deviceInfo).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }

        })
      }else {
        saveAppApi(this.deviceInfo).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }
        })
      }
    },
    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
      console.log('提交设备信息数据:', this.deviceInfo);

    },
    selectChildByDeptIdApi() {
      selectChildByDeptId().then(res=>{
        this.deptListSelect = res.data
      })
    },

    del(e) {

      this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAppApi(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
        deptName:'',
        deptType:'',
        paymentChannel:'',
        publicKey:'',
        contact:'',
        information:'',
        paymentChannelType:'',
        createByName:'',
        createTime:'',
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
</style>

