<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        class="main-search"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="货币" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输货币名称/符号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['store:add']"
            >新增货币</el-button
          >
        </el-form-item>
      </el-form>
  
      <el-table
        ref="tables"
        v-loading="loading"
        :data="list"
        @selection-change="handleSelectionChange"
        :default-sort="defaultSort"
        @sort-change="handleSortChange"
      >
        <el-table-column label="ID" prop="id" width="55" />
        <el-table-column label="货币名称" align="center" prop="name" />
        <el-table-column label="货币符号" align="center" prop="symbol" />
        
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column
          label="操作"
          align="center"
          width="180"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              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.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        class="common-dialog"
        width="800px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="货币名称" prop="name">
                <el-input
                  v-model="form.name"
                  placeholder="请输入货币名称"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="货币符号" prop="name">
                <el-input
                  v-model="form.symbol"
                  placeholder="请输入货币符号"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
          </el-row>
          
          
        </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>
      <FuintQrCode :showDialog="openQrCode" :qr="qr" @closeDialog="closeDialog" />
    </div>
  </template>
  
  <script>
  import FuintQrCode from "@/components/Fuint/QrCode";
  import { getToken } from "@/utils/auth";
  import {
    getStoreList,
    updateStoreStatus,
    getStoreInfo,
    saveStore,
  } from "@/api/store";
  import {saveCurrency,getCurrencyList} from '@/api/currency'
  export default {
    name: "StoreList",
    components: {
      FuintQrCode,
    },
    data() {
      return {
        // 二维码
        qr: null,
        // 二维码对话框
        openQrCode: false,
        // 遮罩层
        loading: true,
        // 标题
        title: "",
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 默认排序
        defaultSort: { prop: "operTime", order: "descending" },
        // 表单参数
        form: {
          name: "",
          symbol:''
        },
        // 微信支付证书
        wxCertPath: "",
        // 上传地址
        uploadAction: process.env.VUE_APP_SERVER_URL + "/backendApi/file/upload",
        // 隐藏上传
        hideUpload: false,
        // 上传文件列表
        uploadFiles: [],
        uploadHeader: { "Access-Token": getToken() },
        merchantOptions: [],
        // 图片根目录
        imagePath: "",
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10,
          name: "",
        },
        // 表单校验
        rules: {
          name: [
            { required: true, message: "货币名称不能为空", trigger: "blur" },
            
          ],
          symbol:[
            { required: true, message: "货币符号不能为空", trigger: "blur" },
          ]
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // 查询列表
      getList() {
        this.loading = true;
        getCurrencyList(this.queryParams).then(
          (response) => {
            console.log('res;',response)
            this.list = response.data
            this.total = response.total
            this.loading = false;
          }
        );
      },
      // 搜索按钮操作
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      // 重置按钮操作
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
        this.handleQuery();
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.status == "A" ? "启用" : "禁用";
        this.$modal
          .confirm("确认要" + text + '"' + row.name + '"店铺吗？')
          .then(function () {
            return updateStoreStatus(row.id, row.status);
          })
          .then(() => {
            this.$modal.msgSuccess(text + "成功");
          })
          .catch(function () {
            row.status = row.status === "N" ? "A" : "N";
          });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.operId);
        this.multiple = !selection.length;
      },
      // 排序触发事件
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      // 新增按钮操作
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增货币";
      },
      // 表单重置
      reset() {
        this.form = {
          id: "",
          name: "",
          symbol:'',
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 提交按钮
      submitForm: function () {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if(this.form.id){

            }else{
                saveCurrency(this.form).then(response => {
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                });
            }
            // if (this.form.id) {
            //   saveStore(this.form).then((response) => {
            //     this.$modal.msgSuccess("修改成功");
            //     this.open = false;
            //     this.getList();
            //   });
            // } else {
            //   saveStore(this.form).then((response) => {
            //     this.$modal.msgSuccess("新增成功");
            //     this.open = false;
            //     this.getList();
            //   });
            // }
          }
        });
      },
      // 二维码
      handleQrCode(row) {
        this.qr = { type: "store", id: row.id };
        this.openQrCode = true;
      },
      // 关闭二维码
      closeDialog() {
        this.openQrCode = false;
      },
      // 修改按钮操作
      handleUpdate(row) {
        this.reset();
      
        this.title = "编辑货币";
        this.open = true;
        const {id,name,symbol} = row
        this.form={id,name,symbol}
        
      },
     
      handleUploadSuccess(file) {
        this.form.logo = file.data.fileName;
      },
      handleUploadLicenseSuccess(file) {
        this.form.license = file.data.fileName;
      },
      handleUploadCertSuccess(file) {
        this.form.wxCertPath = file.data.fileName;
        this.wxCertPath = file.data.fileName;
      },
    },
  };
  </script>
  <style scoped>
  .common-dialog >>> .el-upload--picture-card {
    width: 60px;
    height: 50px;
    line-height: 60px;
  }
  .upload-cert >>> .el-upload {
    width: 20px;
    height: 20px;
    line-height: 20px;
  }
  .upload-cert .file-name {
    width: 700px;
    text-align: left;
  }
  </style>
  