<template>
  <div class="app-container">
    <div class="card-change">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="" prop="legalRepresentativeContact">
          <el-select
            v-model="queryParams.orgId"
            clearable
            placeholder="所属机构"
          >
            <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId">
            </el-option>
          </el-select>
        </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="legalRepresentativeContact">
          <el-select v-model="queryParams.appStatus" placeholder="请选择小程序状态">
            <el-option label="停用" value="0" ></el-option>
            <el-option label="启用" value="1" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="float: right;margin-right: 0px">
          <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:app:add']"
          >新增小程序配置</el-button>
        </el-form-item>
      </el-form>
      <div style="height: 73vh;overflow: auto">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="orgName" label="机构名称"></el-table-column>
        <el-table-column prop="unifiedSocialCreditCode" label="统一社会信用代码"></el-table-column>
        <el-table-column prop="appId" label="小程序appID"></el-table-column>
        <el-table-column prop="appKey" label="密钥"></el-table-column>
        <el-table-column prop="legalRepresentativeName" label="法人姓名"></el-table-column>
        <el-table-column prop="legalRepresentativeWechat" label="法人微信号"></el-table-column>
        <el-table-column prop="legalRepresentativeContact" label="法人联系方式"></el-table-column>
        <el-table-column prop="appStatus" label="小程序状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.appStatus"
              @change="changeStatus(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </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)"
              v-hasPermi="['setting:app:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="del(scope.row)"
              v-hasPermi="['setting:app:delete']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="35%" append-to-body>
      <el-form :model="deviceInfo" ref="deviceForm" label-width="140px" :rules="rule">
        <el-form-item label="机构名称" prop="orgId" style="margin-left: -20px;">
          <el-select
            v-model="deviceInfo.orgId"
            clearable
            placeholder="请输入机构名称"
            style="width: 100%"
          >
            <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName"  :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode" style="margin-left: -20px;">
          <el-input v-model="deviceInfo.unifiedSocialCreditCode" style="width: 100%;" placeholder="请输入统一社会信用代码"></el-input>
        </el-form-item>
        <el-form-item label="小程序appID" prop="appId" style="margin-left: -20px;">
          <el-input v-model="deviceInfo.appId" style="width: 100%;" placeholder="请输入小程序appID"></el-input>
        </el-form-item>
        <el-form-item label="密钥" prop="appKey" style="margin-left: -20px;">
          <el-input v-model="deviceInfo.appKey" style="width: 100%;" placeholder="请输入密钥"></el-input>
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalRepresentativeName" style="margin-left: -20px;">
          <el-input v-model="deviceInfo.legalRepresentativeName" style="width: 100%;" placeholder="请输入法人姓名"></el-input>
        </el-form-item>
        <el-form-item label="法人微信号" prop="legalRepresentativeWechat" style="margin-left: -20px;">
          <el-input v-model="deviceInfo.legalRepresentativeWechat" style="width: 100%;" placeholder="请输入法人微信号"></el-input>
        </el-form-item>
        <el-form-item label="法人联系方式" prop="legalRepresentativeContact" style="margin-left: -20px;">
          <el-input v-model="deviceInfo.legalRepresentativeContact" style="width: 100%;" placeholder="请输入法人联系方式"></el-input>
        </el-form-item>
        <el-form-item label="小程序状态" prop="appStatus" style="margin-left: -20px;">
          <el-switch
            v-model="deviceInfo.appStatus"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <!-- <el-form-item>
          <el-button @click="resetForm('deviceForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('deviceForm')">提交</el-button>
        </el-form-item> -->
      </el-form>
      <!--      </el-form>-->

      <div style="text-align: center; margin-top: 50px;">
        <el-button @click="resetForm('deviceForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('deviceForm')">保 存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>


import {deleteAppApi, getAppListApi, saveAppApi, updateAppApi} from "@/api/sys/app";
import {updateBannerApi} from "@/api/sys/banner";
import {getRunningWaterByTissueApi} from "@/api/allOrder";

export default {
  name: "printIndex",

  data() {
    return {
      tableData: [],
      deptList: [],

      deviceInfo: {
        id: null,            // 自增id
        orgName: '',   // 机构名称
        orgId: '',   // 机构名称
        unifiedSocialCreditCode: '',   // 统一社会信用代码
        appId: '',   // 小程序appID
        appKey: '',   // 密钥
        legalRepresentativeName: '',   // 法人姓名
        legalRepresentativeWechat: '',   // 法人微信
        legalRepresentativeContact: '',   // 法人联系方式
        appStatus: false,   // 小程序状态
        createByName: '',
      },
      rule: {
        orgId: [{required: true, message: '请选择机构名称', trigger: 'blur'},],
        unifiedSocialCreditCode: [{required: true, message: '请填写统一社会信用代码', trigger: 'blur'},],
        appId: [{required: true, message: '请填写小程序appID', trigger: 'blur'},],
        appKey: [{required: true, message: '请填写密钥', trigger: 'blur'},],
        legalRepresentativeName: [{required: true, message: '请填写法人姓名', trigger: 'blur'},],
        // legalRepresentativeWechat: [{required: true, message: '请填写法人微信号', trigger: 'blur'},],
        legalRepresentativeContact: [{required: true, message: '请填写法人联系方式', trigger: 'blur'},],
      },
      // 是否显示弹出层
      open: false,
      title:"",
      total:0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        orgName:'',
        orgId:'',
        legalRepresentativeContact:'',
        appStatus:'',
      },
    };
  },
  created() {
    this.getList()
    this.getDeptList()
  },
  methods: {
    getDeptList() {
      getRunningWaterByTissueApi(this.addDateRange(this.queryParams)).then(response => {
        this.deptList = response.data.records;
      })
    },
    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() {
      // 在这里调用接口提交设备信息数据

      let filter = this.deptList.filter(item => item.deptId == this.deviceInfo.orgId);

      this.deviceInfo.orgName = filter[0].deptName

      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
          }
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.clean();
      this.open=false;
    },
    changeStatus(data){
      console.log(123123123)
      updateAppApi(data).then(res=>{
        if(res.code === 200) {
          this.$message({
            type: 'success',
            message: '修改状态成功!'
          });
          this.getList()
        }

      })
    },
    handleQuery(){
      this.getList()
    },
    resetQuery(){
      this.queryParams = {
        page: 1,
        pageSize: 10,
        deviceName:'',
      },
        this.getList()
    },
    handleAdd(e) {
      this.clean()
      this.open = true;
      this.title = "新增小程序配置"
    },
    edit(e) {
      this.clean()
      this.open = true;
      this.title = "修改小程序配置"
      this.deviceInfo = e
      console.log('提交设备信息数据:', this.deviceInfo);

    },
    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
        orgName: '',   // 机构名称
        unifiedSocialCreditCode: '',   // 统一社会信用代码
        appId: '',   // 小程序appID
        appKey: '',   // 密钥
        legalRepresentativeName: '',   // 法人姓名
        legalRepresentativeWeChat: '',   // 法人微信
        legalRepresentativeContact: '',   // 法人联系方式
        appStatus: false,   // 小程序状态
        createByName: '',
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
.app-container{
  box-sizing: border-box;
  padding: 10px;
  padding-bottom: 0px;
}
</style>

