<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加产品参数档案
      </el-button>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getProductParameterInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      row-key="id"
      border
      default-expand-all>
      <el-table-column
        type="index">
      </el-table-column>
      <el-table-column
        prop="parameterName"
        label="参数名称">
      </el-table-column>
      <el-table-column
        prop="parameterFieldPart"
        label="部件字段">
      </el-table-column>
      <el-table-column
        prop="parameterFieldOrder"
        label="订单字段">
      </el-table-column>
      <el-table-column
        label="字段类型">
        <template slot-scope="{row}">
          <span v-if="row.fieldType==1" >
            字符串
          </span>
          <span v-else-if="row.fieldType==2">
            小数
          </span>
          <span v-else-if="row.fieldType==3">
            整数
          </span>
          <span v-else-if="row.fieldType==4">
            纸张档案
          </span>
          <span v-else-if="row.fieldType==5">
            辅料档案
          </span>
          <span v-else-if="row.fieldType==6">
            尺寸:长*宽
          </span>
          <span v-else-if="row.fieldType==7">
            尺寸:长*宽*高
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="parameterUnit"
        label="参数单位">
      </el-table-column>
      <el-table-column
        prop="defaultValue"
        label="默认值">
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            启用
          </el-tag>
          <el-tag v-else="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="left" label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="editStatus(row.id,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row.id,'0')" v-else="row.voidFlag==1" size="mini">
            启用
          </el-button>

        </template>
      </el-table-column>

    </el-table>
    <pagination
v-show="listData.total>0"
:total="listData.total"
:page.sync="listData.page"
                :limit.sync="listData.pageSize"
@pagination="getProductParameterInfo" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px">
      <el-form label-width="100px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="参数名称">
              <el-input v-model="form.parameterName"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">

            <el-form-item label="部件字段">
              <el-input v-model="form.parameterFieldPart"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="订单字段">
              <el-input v-model="form.parameterFieldOrder"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="字段类型">
              <el-select v-model="form.fieldType" filterable placeholder="请选择" style="width: 270px">
                <el-option
                  v-for="item in fieldTypes"
                  :key="item.value"
                  :label="item.itemName"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="选择方式">-->
<!--              <el-select v-model="form.selectKind" filterable placeholder="请选择" style="width: 270px">-->
<!--                <el-option-->
<!--                  v-for="item in selectKinds"-->
<!--                  :key="item.id"-->
<!--                  :label="item.itemName"-->
<!--                  :value="item.value">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
        <el-form-item label="参数单位">
          <el-input v-model="form.parameterUnit"></el-input>
        </el-form-item>
          </el-col>

          <el-col :span="12">
        <el-form-item label="默认值">
          <el-input v-model="form.defaultValue"></el-input>
        </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
<!--    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :menuId="menuId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>-->
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { getProductParameterList, addProductParameter, getProductParameterInfoById, updateProductParameter } from '@/api/productFile/productParameter.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'

const form = {
  parameterName: '',
  parameterFieldPart: '',
  parameterFieldOrder: '',
  fieldType: '',
  parameterUnit: '',
  defaultValue: ''
  // selectKind:0
}

export default {
  components: {
    Cancledialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      fieldTypes: [
        {
          value: 1,
          itemName: '字符串'
        },
        {
          value: 2,
          itemName: '小数'
        },
        {
          value: 3,
          itemName: '整数'
        },
        {
          value: 4,
          itemName: '纸张档案'
        },
        {
          value: 5,
          itemName: '辅料档案'
        },
        {
          value: 6,
          itemName: '尺寸:长*宽'
        },
        {
          value: 7,
          itemName: '尺寸:长*宽*高'
        },
        // {
        //   value: 8,
        //   itemName: '纸张价格档案'
        // }
      ],
      // selectKinds:[
      //   {
      //     id:1,
      //     value:0,
      //     itemName:'输入'
      //   },
      //   {
      //     id:2,
      //     value:1,
      //     itemName:'选择字典'
      //   },
      //   {
      //     id:3,
      //     value:2,
      //     itemName:'选择和自定义'
      //   },
      //   {
      //     id:4,
      //     value:3,
      //     itemName:'下拉选择'
      //   }
      // ],
      options: [],

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      menuId: ''
    }
  },
  mounted() {
    this.getProductParameterInfo()
  },
  methods: {
    // 获取列表信息
    getProductParameterInfo() {
      const _that = this
      _that.listLoading = true

      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }

      getProductParameterList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },
    // 增加产品参数档案
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form),
      _that.dialogTitle = '增加产品参数档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getProductParameterInfo()
    },
    // 增加产品参数档案
    addBtn() {
      const _that = this
      var printerTypeInfo = JSON.stringify(_that.form)
      var data = {
        'productParameter': printerTypeInfo
      }
      // console.log(data);
      // return

      addProductParameter(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form),

      _that.dialogTitle = '修改产品参数档案信息'
      _that.dialogVisible = true
      _that.dialogStatus = '2'

      var idPrint = row.id
      var data = {
        idParameter: idPrint
      }

      // console.log(data);
      getProductParameterInfoById(data).then(res => {
        console.log(res)
        const data = res.data

        _that.form = {
          id: data.id,
          parameterName: data.parameterName,
          parameterFieldPart: data.parameterFieldPart,
          parameterFieldOrder: data.parameterFieldOrder,
          fieldType: data.fieldType,
          parameterUnit: data.parameterUnit,
          defaultValue: data.defaultValue
          // selectKind:data.selectKind
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改产品参数档案
    editBtn() {
      const _that = this
      var printerTypeInfo = JSON.stringify(_that.form)
      var data = {
        'productParameter': printerTypeInfo
      }

      // console.log(data);
      // return
      updateProductParameter(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 启用禁用
    editStatus(id, status) {
      var _that = this
      var printerTypeInfo = {
        id: id,
        voidFlag: status
      }
      var params = {
        'productParameter': JSON.stringify(printerTypeInfo)
      }

      var url = '/product/voidProductParameter'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getProductParameterInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
</style>
