<template>
  <div style="height: auto;padding-top: 20px;padding-left: 10px" class="">
    <div class="filter-container">
      <el-input size="mini" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item"
                @keyup.enter.native="searchData"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        size="mini"
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加基础纸张档案
      </el-button>
      <el-checkbox v-if="!isChoose" size="mini" @change="checkDisable" v-model="disableLock" label="已禁用" border></el-checkbox>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getPaperInfo"/>
        </el-tooltip>
      </div>
    </div>
    <el-table
      size="mini"
      :data="tableData"
      v-loading="listLoading"
      class="tb-edit"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      highlight-current-row
      @row-click="handleCurrentChange"
      default-expand-all>
      <el-table-column
        prop="paperName"
        width="130px"
        label="档案名称">
      </el-table-column>
      <el-table-column
        prop="paperTypeName"
        label="纸张类型名称">
      </el-table-column>

      <el-table-column
        prop="mtSuttle"
        label="克重">
      </el-table-column>
      <el-table-column
        prop="priceType"
        label="计价方式">
        <template slot-scope="{row}">
        <span v-if="row.priceType==0" type="success">
          吨价
        </span>
          <span v-else-if="row.priceType==1" type="danger">
          平方单价
        </span>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        v-if="webType && isChoose == false"-->
      <!--      prop="priceType"-->
      <!--      label="单价">-->
      <!--      <template slot-scope="scope">-->
      <!--        <el-input size="small" v-model="scope.row.paperPrice" placeholder="请输入内容" @change="handleEdit(scope.$index, scope.row)"></el-input>-->
      <!--        <span>{{scope.row.paperPrice}}</span>-->
      <!--      </template>-->
      <!--    </el-table-column>-->
      <el-table-column align="center" label="状态" class-name="status-col">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            启用
          </el-tag>
          <el-tag v-else-if="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed='left' align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="webType">
            <template v-if="!isChoose">
              <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-if="row.voidFlag==1" size="mini">
                启用
              </el-button>
            </template>
            <template v-else>
              <el-button @click="chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
          <template v-else>
            <el-button @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </template>
      </el-table-column>

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

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px">
      <el-form label-width="130px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item v-if="dialogStatus==2" label="纸张名称">
              <el-input v-model="form.paperName" :disabled="true" style="width: 240px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纸张类型">
              <el-select v-model="form.idPaperType" filterable placeholder="请选择" style="width: 240px"
                         :disabled="dialogStatus==2">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.id"
                  :label="item.paperTypeName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计价方式">
              <el-select v-model="form.priceType" filterable placeholder="请选择" style="width: 240px">
                <el-option
                  v-for="item in priceOptions"
                  :key="item.value"
                  :label="item.label"
                  :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.mtSuttle" :disabled="dialogStatus==2" style="width: 240px"
                        type="number"></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 {
  addPaperInfo,
  getPaperInfoById,
  getPaperInfoList,
  savePaperInfoPrice,
  updatePaperInfo
} from '@/api/baseInfo/paper/basePaperInfo.js'
import {getPaperTypeList} from '@/api/baseInfo/paper/paperType.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'

const form = {
  paperName: '',
  idPaperType: '',
  paperTypeName: '',
  priceType: 0,
  paperPrice: '',
  mtSuttle: ''
}
export default {
  name: 'BasePaperInfo',
  components: {
    Cancledialog, Pagination
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    partId: {
      type: Number || String
    },
    productId: {
      type: Number || String
    }
  },
  data() {
    return {
      disableLock: false,
      webType: true,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: '',
        voidFlag:'',
        partInfo: {
          id: '',
          idProduct: ''
        }
      },
      form: Object.assign({}, form),
      typeOptions: [],
      priceOptions: [
        {
          label: '吨价',
          value: 0
        },
        {
          label: '平方单价',
          value: 1
        }
      ],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      menuId: ''
    }
  },
  mounted() {
    this.getPaperInfo()
    this.getPaperTypeInfo()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {
    checkDisable(val) {
      // console.log(val);
      this.listData.page = 1;
      this.getPaperInfo()
    },
    // 获取列表
    getPaperInfo() {
      const _that = this
      _that.listLoading = true
      _that.search.partInfo.id = _that.partId
      _that.search.partInfo.idProduct = _that.productId
      if(_that.disableLock){
        _that.search.voidFlag = 1;
      }else {
        _that.search.voidFlag = 0;
      }
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getPaperInfoList(data).then(response => {
        // console.log('基础纸张档案列表',response)
        const data = response.data;
        // //  组合表单数据
        _that.tableData = data;
        _that.listData.total = response.total;
        // console.log(_that.tableData)
        _that.listLoading = false;
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    // 获取纸张类型列表
    getPaperTypeInfo() {
      const _that = this
      _that.listLoading = true
      const page = {
        pageIndex: _that.listData.page,
        pageSize: 10000
      }

      var data = {
        'params': JSON.stringify({
          'voidFlag': 0
        }),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getPaperTypeList(data).then(response => {
        // console.log(response)
        const data = response.data
        _that.typeOptions = data
      }).catch(err => {
      })
    },
    // 增加机型档案
    handleCreateAdd() {
      // 增加系统目录
      const _that = this
      _that.form = Object.assign({}, form)
      _that.dialogTitle = '增加基础纸张档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 增加机型档案
    addBtn() {
      const _that = this
      var info = JSON.stringify(_that.form)
      var data = {
        'info': info
      }
      // console.log(data);
      // return

      addPaperInfo(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
      })
    },

    getPaper(id) {
      var _that = this
      var data = {
        id: id
      }

      getPaperInfoById(data).then(res => {
        console.log('获取信息', res)
        const data = res.data
        _that.form = {
          id: data.id,
          paperName: data.paperName,
          idPaperType: data.idPaperType,
          paperTypeName: data.paperTypeName,
          paperInfoCode: data.paperInfoCode,
          priceType: data.priceType,
          paperPrice: data.paperPrice,
          mtSuttle: data.mtSuttle
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form)

      _that.dialogTitle = '修改基础纸张档案'
      _that.dialogVisible = true
      _that.dialogStatus = '2'

      var idPrint = row.id
      _that.getPaper(idPrint)
    },
    // 修改机型档案
    editBtn() {
      const _that = this
      var info = JSON.stringify(_that.form)
      var data = {
        'info': info
      }

      updatePaperInfo(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 = {
        'info': JSON.stringify(printerTypeInfo)
      }

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

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },

    // 行内编辑
    handleCurrentChange(row, event, column) {
      // console.log(row, event, column, event.currentTarget);
    },
    handleEdit(index, row) {
      // console.log('修改行内');
      // console.log(index, row);
      var _that = this
      // _that.getPaper(row.id);
      var data = {
        'id': row.id,
        'paperPrice': row.paperPrice
      }
      // console.log('修改后form',_that.form);return;
      savePaperInfoPrice(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }

  }
}
</script>
<style>
/*处理input type = number的上下箭头*/
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

input[type="number"] {
  -moz-appearance: textfield;
}
</style>
<style lang="scss" scoped>
::v-deep .el-checkbox.is-bordered.is-checked {
  border-color: #F56C6C;
}
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner,
::v-deep .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  border-color: #F56C6C !important;
  background-color: #F56C6C !important;
}

::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #F56C6C !important;
}

::v-deep .el-checkbox__inner:hover {
  border-color: #F56C6C !important;
}

::v-deep .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #F56C6C !important;
}
.filter-container {
  margin-bottom: 15px;
}
.tb-edit .el-input {
  display: none
}
.tb-edit .current-row .el-input {
  display: block
}
.tb-edit .current-row .el-input + span {
  display: none
}

</style>
