<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="客户名称" prop="appName">
        <el-input
          v-model="queryParams.appName"
          clearable
          placeholder="请输入客户名称"
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="appCode">
        <el-input
          v-model="queryParams.appCode"
          clearable
          placeholder="请输入客户编码"
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="客户状态" prop="configType">
        <el-select v-model="queryParams.status" clearable placeholder="客户状态">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="getList">搜索</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
          v-hasPermi="['system:config:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>

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

    <el-row :gutter="10">
      <el-empty description="暂无支付客户" v-if="customerList.length === 0"></el-empty>
      <el-col :span="8" v-for="customer in customerList" v-if="customerList.length !== 0">

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span style="font-size: 18px;font-weight: 900">{{ customer.appName }}</span>
            <el-switch
              style="float: right; margin: 4px 5px 0;"
              v-model="customer.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="Y"
              inactive-value="N"
              @change="handleStatusChange(customer)"
            >
            </el-switch>

            <el-popconfirm
              confirm-button-text='好的'
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="handleDelete(customer.id)"
            >
            <el-button style="float: right; padding: 5px ;margin: 0 5px" slot="reference" type="danger" icon="el-icon-delete" circle ></el-button>
            </el-popconfirm>

              <el-button style="float: right; padding: 5px ;margin: 0 5px"  type="primary" icon="el-icon-edit" @click="handleUpdate(customer.id)"circle></el-button>




          </div>
          <div>
            <el-descriptions class="margin-top" :column="1" :size="size" border>

              <el-descriptions-item>
                <template slot="label"><i class="el-icon-user"></i>客户名称</template>
                {{ customer.appName }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label"><i class="el-icon-user"></i>客户编码</template>
                {{ customer.appCode }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label"><i class="el-icon-user"></i>创建人</template>
                {{ customer.createBy }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label"><i class="el-icon-user"></i>创建日期</template>
                {{ customer.createTime }}
              </el-descriptions-item>
            </el-descriptions>
            <el-divider content-position="left">近5笔交易记录</el-divider>
            <el-table
              :data="customer.orderPaySimpleRespList"
              style="width: 100%">
              <el-table-column prop="cusOrderNumber" label="客户单号"></el-table-column>
              <el-table-column prop="price" label="支付金额"></el-table-column>
              <el-table-column prop="channelTypeName" label="渠道商"></el-table-column>
            </el-table>

          </div>


        </el-card>
      </el-col>
    </el-row>


    <el-dialog
      :title="title"
      :visible.sync="open"
      width="50%">

      <el-form ref="form" :model="form" label-width="120px" :rules="rules">

        <el-form-item label="客户编码" prop="appCode">
          <span v-if="this.form.id !=null" style="font-size: 16px">{{form.appCode}}</span>
          <el-select v-if="this.form.id == null" v-model="form.appCode" placeholder="请选择未入住的客户" style="width: 100%">
            <el-option
              v-for="item in appCodeList"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="appName">
          <el-input v-model="form.appName"></el-input>
        </el-form-item>
        <el-form-item label="是否激活开通" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="详细描述">
          <el-input v-model="form.appDesc" type="textarea"></el-input>
        </el-form-item>

        <el-form-item label="客户备注">

          <editor v-model="form.remark" :min-height="192"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="open = false">取 消</el-button>
    <el-button type="primary"  @click="submitForm">提 交</el-button>
  </span>
    </el-dialog>




  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {selectList, customerAll, update, save, get, del, enable,disable} from "@/api/pay/customer";


export default {
  name: "Customer",
  dicts: ['sys_yes_no'],
  //import引入的组件需要注入到对象中才能使用"
  components: {},
  props: {},
  data() {
    //这里存放数据"
    return {
      //未入住客户
      appCodeList:["cloud-1","cloud-2","cloud-3"],
      //模态框标题
      title:"",
      //表单内容
      form:{
        appName: "",
        appCode: "",
        appDesc: "",
        status: "Y",
        remark: ""
      },
      //修改和添加弹框
      open:false,
      switchValue:true,
      // 显示搜索条件
      showSearch: true,
      //查询条件
      queryParams: {
        appName: "",
        appCode: "",
        status: "Y"
      },

      rules: {
        appCode: [
          {required: true, message: "请选择客户编码", trigger: "change"}
        ],
        appName: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "请选择客户是否开通", trigger: "blur"}
        ]
      },
      //客户列表
      customerList:[],


    };
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    /** 客户状态更改触发 */
    handleStatusChange(row){
      console.log(row)
      if (row.status === "Y"){
        enable(row.id).then((res)=>{
          this.$modal.msgSuccess("启用成功")
          this.getList()
        })
      }else {
        disable(row.id).then((res)=>{
          this.$modal.msgSuccess("禁用成功")
          this.getList()
        })
      }
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付单客户信息";
    },

    /** 删除按钮操作*/
    handleDelete(orderCustomerId) {
      del(orderCustomerId).then((res)=>{
        this.$modal.msgSuccess("删除成功")
        this.getList()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(orderCustomerId) {
      this.reset();
      get(orderCustomerId).then((res)=>{
        this.form=res.data
        this.open = true;
        this.title = "修改支付单客户信息";
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },
    //搜索
    getList(){
      selectList(this.queryParams).then((res)=>{
        this.customerList=res.data
      })
    },
    reset() {
      this.form = {
        "appName": "",
        "appCode": "",
        "appDesc": "",
        "status": "Y",
        "remark": ""
      };
      this.resetForm("form");
      this.appCodeList = [];
      customerAll().then((res)=>{
        this.appCodeList = res.data
      })

    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            save(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.getList()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, //生命周期 - 更新之后",
  beforeDestroy() {
  }, //生命周期 - 销毁之前",
  destroyed() {
  }, //生命周期 - 销毁完成",
  activated() {
  } //如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>

.el-card{
  margin: 10px 0;
}

</style>
