<template>
  <div class="app-container">
    <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
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加纸张档案
      </el-button>

      <el-button
        size="mini"
        v-if="permissionFlag"
        class="filter-item"
        style="margin-left: 10px;"
        type="danger"
        @click="resetStockPrice">
        重置纸张入库均价
      </el-button>
      <el-button
        v-if="type == 1"
        class="filter-item"
        style="margin-left: 10px;"
        type="success"
        size="mini"
        @click="chooseList">选择
      </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
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      :size="!webType ? 'mini':''"
      border
      @selection-change="selectionChange"
      default-expand-all>
      <el-table-column
        type="selection"
        width="55"
        v-if="type == 1"
        :reserve-selection="true"
      ></el-table-column>
      <el-table-column
        prop="paperCode"
        width="100"
        label="纸张编码">
      </el-table-column>
      <el-table-column
        prop="paperName"
        width="260"
        label="纸张名称">
      </el-table-column>
      <template v-if="webType">
        <el-table-column
          width="80"
          prop="mtSuttle"
          label="克重">
        </el-table-column>
        <el-table-column
          prop="mtRule"
          width="95"
          label="规格">
        </el-table-column>

        <!--        <el-table-column-->
        <!--          prop="priceType"-->
        <!--          label="计价规则">-->
        <!--        </el-table-column>-->
        <el-table-column
          label="品牌"
          prop="mtModel"
        >
        </el-table-column>
        <el-table-column
          prop="paperTypeName"
          width="120"
          label="纸张类型名称">
        </el-table-column>

        <el-table-column
          label="转换参数"
          width="200px">
          <template slot-scope="{row}">
            <span>吨 = {{ row.convertUnitByToMt }} 令 = {{ row.convertUnitMtToSe }} 张</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="stockAvgPrice"
          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-if="row.voidFlag==1" type="danger">
              禁用
            </el-tag>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" v-if="type !=1" fixed="left" align="center" width="200"
                       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="sourceType == 1? chooseThisTo(row):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" :close-on-click-modal="false" :visible.sync="dialogVisible" width="70%">
      <el-form label-width="130px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item v-if="dialogStatus==2" label="纸张名称">
              <el-input v-model="form.paperName" :readonly="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格">
              <el-input v-model="form.mtRule" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="克重" v-show="false">
              <el-input v-model="form.mtSuttle" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纸张类型">
              <!--              <el-select v-model="form.idPaperType" filterable placeholder="请选择" :disabled="dialogStatus==2">-->
              <!--                <el-option-->
              <!--                  v-for="item in options"-->
              <!--                  :key="item.id"-->
              <!--                  :label="item.paperTypeName"-->
              <!--                  :value="item.id">-->
              <!--                </el-option>-->
              <!--              </el-select>-->
              <el-button @click="openChoose" :disabled="dialogStatus==2">{{ basePaperName ? basePaperName : '请选择' }}
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品牌">
              <el-button :disabled="btnDisable" @click="checkMtModel" type="text">{{ form.mtModel }}</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购单位">
              <el-input v-model="form.byUnit" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库存单位">
              <el-input v-model="form.mtUnit" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="使用单位">
              <el-input v-model="form.seUnit" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购=>库存" v-show="dialogStatus==2">
              <el-input v-model="form.convertUnitByToMt" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存=>使用" v-show="dialogStatus==2">
              <el-input v-model="form.convertUnitMtToSe" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计价方式">
              <el-select v-model="form.priceType">
                <el-option label="吨价" :value="0"></el-option>
                <el-option label="平方价" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="条形码">
              <el-input v-model="form.barCode" :disabled="dialogStatus==2"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="备注">
              <el-input type="textarea" :rows="2" v-model="form.remark"></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 :disabled="disabledBtn" type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
    <el-dialog :close-on-click-modal="false" title="选择纸张类型" :visible.sync="paperInfoVisible" width="70%">
      <BasePaperInfo :is-choose="true" @choosePaper='choosePaper'></BasePaperInfo>
    </el-dialog>
    <el-dialog width="550px" title="选择品牌" :visible.sync="dialogTableVisible">
      <el-input size="mini" v-model="search2.keyWord" placeholder="关键字" style="width: 200px;margin-bottom: 10px"
                class="filter-item" @keyup.enter.native="searchData2"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData2">
        搜索
      </el-button>
      <el-table border size="mini" :data="gridData">
        <el-table-column width="80" label="操作">
          <template slot-scope="{row}">
            <el-button @click="mtModelChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </el-table-column>
        <el-table-column property="brandName" label="品牌名称" width="150"></el-table-column>
      </el-table>
      <pagination style="margin-top: 5px" v-show="listData2.total>0" :total="listData2.total"
                  :page.sync="listData2.page" :limit.sync="listData2.pageSize" @pagination="getPaperBrandListMethods"/>
    </el-dialog>

  </div>
</template>

<script>
import {
  getPaperInfoList,
  getPaperInfoById,
  updatePaperInfo,
  addPaperInfo,
  getPaperBrandList
} from '@/api/baseInfo/paper/paperInfo.js'
import {getPaperTypeList} from '@/api/baseInfo/paper/paperType.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import BasePaperInfo from '@/views/baseInfo/paper/basePaperInfo'
import {ifPermission} from "@/api/order/orderList";

const form = {
  idPaperType: '',
  idPaperBaseInfo: '',
  paperName: '',
  mtRule: '',
  mtSuttle: '',
  paperCode: '',
  mtModel: '选择品牌',
  convertUnitByToMt: '',
  convertUnitMtToSe: '',
  byUnit: '吨',
  mtUnit: '令',
  seUnit: '张',
  barCode: '',
  remark: '',
  priceType: 0
}
export default {
  name: 'PaperInfo',
  components: {
    BasePaperInfo,
    Cancledialog
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 选择方式
     * 0 单选
     * 1 多选
     */
    type: {
      type: Number,
      default: 0
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      disableLock: false,
      btnDisable: false,
      disabledBtn: false,
      dialogTableVisible: false,
      gridData: [],
      permissionFlag: false,
      webType: true,
      paperInfoVisible: false,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: '',
        voidFlag: "",
      },
      search2: {
        keyWord: '',
        voidFlag:0
      },
      form: Object.assign({}, form),
      options: [],

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

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      basePaperName: '',
      chooseInfoList: []
    }
  },
  mounted() {
    this.checkPermission()
    if (this.$route.query.type === undefined) {
      this.webType = true
      this.search.voidFlag = -1
    } else {
      this.webType = false
      this.search.voidFlag = 0
    }
    this.getPaperInfo()
    this.getPaperTypeInfo()
    this.getPaperBrandListMethods();
  },
  methods: {
    checkDisable(val) {
      // console.log(val);
      this.listData.page = 1;
      this.getPaperInfo()
    },
    searchData2() {
      this.listData2.page = 1
      this.getPaperBrandListMethods()
    },
    mtModelChoose(row) {
      const _that = this;

      _that.form.mtModel = row.brandName;
      _that.dialogTableVisible = false
    },
    checkMtModel() {
      const _that = this;
      _that.search2.keyWord="";
      _that.dialogTableVisible = true;
    },

    getPaperBrandListMethods() {
      const _that = this;
      var params = _that.search2;
      var page = {
        pageIndex: _that.listData2.page,
        pageSize: _that.listData2.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getPaperBrandList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.gridData = data

        _that.listData2.total = response.total
        // console.log(_that.tableData)
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 获取列表
    getPaperInfo() {
      const _that = this;
      _that.listLoading = true;
      var params = _that.search;
      if(_that.disableLock){
        _that.search.voidFlag = 1;
      }else {
        _that.search.voidFlag = 0;
      }
      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
      var data = {
        'params': JSON.stringify({
          'voidFlag': 0
        })
      }
      // console.log(data);
      getPaperTypeList(data).then(response => {
        // console.log(response)
        const data = response.data
        _that.options = data
      }).catch(err => {
        _that.$message.error(err)
      })
    },

    // 增加纸张档案
    handleCreateAdd() {
      // 增加系统目录
      const _that = this
      _that.form = Object.assign({}, form)
      _that.basePaperName = '';
      _that.form.mtModel = "选择品牌";
      _that.dialogTitle = '增加纸张档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true;
      _that.btnDisable = false;
      _that.disabledBtn = false;
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 增加纸张档案
    addBtn() {
      const _that = this;
      if (!_that.form.idPaperBaseInfo) {
        _that.$message.error('请选择纸张类型')
        return
      }
      if (_that.form.mtModel == "选择品牌") {
        _that.form.mtModel = "";
      }
      // if (_that.form.mtModel=="选择品牌") {
      //   _that.$message.error('请选择纸张品牌')
      //   return
      // }
      _that.disabledBtn = true;
      _that.dialogVisible = false
      var info = JSON.stringify(_that.form)
      var data = {
        'info': info
      }
      addPaperInfo(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {

          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.disabledBtn = false;
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.disabledBtn = false;
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form)
      _that.basePaperName = ''
      _that.dialogTitle = '修改纸张档案'
      _that.dialogVisible = true
      _that.disabledBtn = false;
      _that.btnDisable = true;
      _that.dialogStatus = '2'

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

      getPaperInfoById(data).then(res => {
        const data = res.data
        _that.form = {
          id: data.id,
          paperName: data.paperName,
          idPaperBaseInfo: data.idPaperBaseInfo,
          idPaperType: data.idPaperType,
          mtRule: data.mtRule,
          mtSuttle: data.mtSuttle,
          paperCode: data.paperCode,
          mtModel: data.mtModel,
          byUnit: data.byUnit,
          mtUnit: data.mtUnit,
          seUnit: data.seUnit,
          barCode: data.barCode,
          remark: data.remark,
          convertUnitByToMt: data.convertUnitByToMt,
          convertUnitMtToSe: data.convertUnitMtToSe,
          priceType: data.priceType
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改纸张档案
    editBtn() {
      const _that = this
      if (!_that.form.idPaperBaseInfo) {
        _that.$message.error('请选择纸张类型')
        return
      }
      var info = JSON.stringify(_that.form)
      var data = {
        'info': info
      }
      _that.disabledBtn = true;
      _that.dialogVisible = false

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

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

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

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },
    openChoose() {
      var _that = this
      _that.paperInfoVisible = true
    },
    choosePaper(row) {
      console.log(row)
      var _that = this
      _that.paperInfoVisible = false
      _that.form.mtSuttle = row.mtSuttle
      _that.form.idPaperBaseInfo = row.id
      _that.form.idPaperType = row.idPaperType
      _that.basePaperName = row.paperName
    },

    // 判断权限
    checkPermission() {
      const _that = this
      var data = {
        permission: 'stock.price.admin'
      }
      ifPermission(data).then(res => {
        if (res.data == 1) {
          _that.permissionFlag = true
        }
      })
    },
    resetStockPrice() {
      var _that = this
      var params = {
        params: {idBillDefine: 6}
      }
      var url = '/inventory/resetMaterialStockPrice'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    // 多选
    chooseList() {
      this.$emit('choosePaper', this.chooseInfoList)
    },
    selectionChange(val) {
      const _that = this
      _that.chooseInfoList = val
    },
    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.paperName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

<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;
}
</style>
