<template>
  <div class="app-container">
    <!-- 区域选择卡片 -->
    <el-card class="area-card" shadow="never">
      <div class="area-header">
        <span class="area-title">区域选择</span>
        <el-tabs v-model="checkStoreAreaId" @tab-click="handleQuery" type="card" class="area-tabs">
          <el-tab-pane 
            v-for="item in areaList" 
            :key="item.id"
            :label="item.name" 
            :name="item.id + ''">
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card>

    <!-- 搜索和操作区域 -->
    <el-card class="search-card" shadow="never">
      <el-row :gutter="20" type="flex" align="middle">
        <el-col :span="6">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入衣服名称"
            clearable
            prefix-icon="el-icon-search"
            @keyup.enter.native="handleQuery"
          />
        </el-col>
        <el-col :span="8">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-col>
        <el-col :span="10" style="text-align: right">
          <el-button type="primary" plain icon="el-icon-upload2" @click="handleImport">导入价格</el-button>
          <el-button type="warning" plain icon="el-icon-download" @click="handleExport">导出价格</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 价格表格 -->
    <el-card class="table-card" shadow="never">
      <el-table v-loading="loading" :data="clothItemPriceDataList" border stripe>
        <!-- 基本信息列 -->
        <el-table-column label="名称" prop="name" width="200" align="center" />

        <!-- 洗衣方式价格列组 -->
        <el-table-column label="洗衣方式" align="center">
          <el-table-column label="干洗" align="center" prop="dryCleaningModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.dryCleaningModePrice" />
            </template>
          </el-table-column>
          <el-table-column label="水洗" align="center" prop="washModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.washModePrice" />
            </template>
          </el-table-column>
          <el-table-column label="织补" align="center" prop="darningModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.darningModePrice" />
            </template>
          </el-table-column>
          <el-table-column label="单烫" align="center" prop="ironModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.ironModePrice" />
            </template>
          </el-table-column>
          <el-table-column label="保值精洗" align="center" prop="fineWashModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.fineWashModePrice" />
            </template>
          </el-table-column>
          <el-table-column label="护理" align="center" prop="nurseModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.nurseModePrice" />
            </template>
          </el-table-column>
          <el-table-column label="奢护洗" align="center" prop="shehuModePrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.shehuModePrice" />
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 衣服类型价格列组 -->
        <el-table-column label="衣服类型加价" align="center">
          <el-table-column label="加厚" align="center" prop="plusPrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.plusPrice" />
            </template>
          </el-table-column>
          <el-table-column label="羊绒" align="center" prop="sheepPrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.sheepPrice" />
            </template>
          </el-table-column>
          <el-table-column label="真丝" align="center" prop="silkPrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.silkPrice" />
            </template>
          </el-table-column>
          <el-table-column label="儿童" align="center" prop="childPrice" min-width="100">
            <template slot-scope="scope">
              <price-cell :value="scope.row.childPrice" />
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?storeAreaId=' + upload.storeAreaId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或<em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="primary" :underline="false" @click="importTemplate">下载模板</el-link>
          <span class="upload-tip-text">仅允许导入xls、xlsx格式文件</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 价格单元格组件
const PriceCell = {
  props: {
    value: [String, Number]
  },
  render(h) {
    const hasValue = this.value !== null && this.value !== undefined && this.value !== ''
    return h('div', {
      class: {
        'price-cell': true,
        'price-not-set': !hasValue
      }
    }, [
      hasValue 
        ? h('span', { class: 'price-value' }, [`¥${this.value}`])
        : h('span', { class: 'not-set-text' }, ['未设置'])
    ])
  }
}

import { listStoreArea } from '@/api/system/storeArea'
import { listClothItem, listClothItemPriceByArea } from '@/api/core/clothItem'
import { listType } from '@/api/core/clothType'
import { listWashMode } from '@/api/core/washMode'
import { getToken } from '@/utils/auth'
export default {
  name: 'ClothPrice',
  components: {
    PriceCell
  },
  dicts: ['cloth_type'],
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      // 遮罩层
      loading: true,
      areaList: [],
      checkStoreAreaId: '1',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      clothItemPriceVO: null,
      clothItemPriceDataList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null
      },
      // 表单参数
      form: {
        status: '0'
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（单价导入）
        open: false,
        // 弹出层标题（单价导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        //区域id
        storeAreaId: '',
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/mapi/cloth/price/importData'
      },
      // 表单校验
      rules: {},
      clothTypeList: [],
      washModeList: [],
      clothVO: null,
      clothType0VO: null,
      clothType0V1: null,
      clothType0V2: null,
      clothType0V3: null
    }
  },
  created() {
    this.getAreaList()
    this.getWashModeList()
    this.getClothTypeList()
    this.getList()
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      if (!this.checkStoreAreaId) {
        this.$modal.msgError('请选择区域')
        return false
      }
      this.upload.storeAreaId = this.checkStoreAreaId
      const up_name = this.areaList.find((item) => item.id === parseInt(this.checkStoreAreaId))?.name

      this.upload.title = `【${up_name}】 洗衣方式单价导入`
      this.upload.open = true
    },
    /** 导出按钮操作 */
    handleExport() {
            if (!this.checkStoreAreaId) {
                this.$modal.msgError('请选择区域')
                return
            }
            let primary = {
                storeAreaId: this.checkStoreAreaId
            }
            const up_name = this.areaList.find((item) => item.id === parseInt(this.checkStoreAreaId))?.name
            this.downloadJson(
                '/mapi/cloth/price/exportData',
                {
                    ...primary
                },
                `${up_name}衣服单价列表.xlsx`
            )
        },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/mapi/cloth/price/downloadTemplate', {}, `衣服单价导入模板.xlsx`)
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      var respMsg = response.code == '200' ? '' : '导入失败！'
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + respMsg + '<br/>' + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    getAreaList() {
      listStoreArea({ pageSize: 10000, pageNum: 1, status: '0' }).then((res) => {
        this.areaList = res.rows
      })
    },
    /** 获取洗衣方式 */
    getWashModeList() {
      listWashMode({ status: '0' }).then((response) => {
        this.washModeList = response.rows
      })
    },
    /** 获取衣服类型 */
    getClothTypeList() {
      listType({ pageSize: 9999, pageNum: 1 }).then((response) => {
        this.clothTypeList = response.rows
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeId = data.id
      this.clothItemPriceDataList = []
      this.handleQuery()
    },
    getList() {
      this.queryParams.areaId = parseInt(this.checkStoreAreaId)
      listClothItemPriceByArea(this.queryParams).then((response) => {
        this.clothItemPriceDataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        rechargeAmount: null,
        giveAmount: null,
        givePointAmount: null,
        expireMonth: null,
        status: '0',
        delFlag: null,
        createById: null,
        createBy: null,
        createTime: null,
        updateById: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .area-card {
    margin-bottom: 16px;
    
    .area-header {
      .area-title {
        font-size: 16px;
        font-weight: 500;
        margin-bottom: 26px;
        color: #303133;
      }
    }
    
    .area-tabs {
      margin-top: 16px;
      :deep(.el-tabs__nav) {
        .el-tabs__item {
          &.is-active {
            font-weight: 500;
          }
        }
      }
    }
  }
  
  .search-card {
    margin-bottom: 16px;
    
    .el-input {
      width: 240px;
    }
    
    .el-button {
      margin-left: 8px;
      
      &:first-child {
        margin-left: 0;
      }
    }
  }
  
  .table-card {
    :deep(.el-table) {
      // 表头样式
      th {
        background-color: #f5f7fa;
        color: #303133;
        font-weight: 500;
        
        &.el-table__cell {
          padding: 8px 0;
        }
      }
      
      // 单元格样式
      td {
        padding: 12px 0;
      }
      
      // 价格单元格样式
      .price-cell {
        .price-value {
          color: #606266;
          font-weight: 500;
        }
        
        &.price-not-set {
          .not-set-text {
            color: #909399;
            font-size: 13px;
            background-color: #f5f7fa;
            padding: 2px 8px;
            border-radius: 2px;
          }
        }
      }
    }
  }
}

// 上传提示文字样式
.upload-tip-text {
  margin-left: 8px;
  color: #909399;
  font-size: 13px;
}

// 分页样式
.pagination-container {
  margin-top: 16px;
  padding: 16px 0;
  text-align: right;
}
</style>
