<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.keyWord" placeholder="关键字" @keyup.enter.native="searchData" style="width: 200px;" class="filter-item" />
      <el-button 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;"
        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="getSupplierInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column
        prop="supplierName"
        label="供应商名称">
      </el-table-column>
     <el-table-column
        label="供应商编号"
        prop="supplierCode"
      >
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="region"-->
<!--        label="地区">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="abbreviation"-->
<!--        label="简称">-->
<!--      </el-table-column>-->

      <el-table-column
        label="联系人"
        prop="linkMan"
      >
      </el-table-column>
      <el-table-column
        prop="phone"
        label="联系电话">
      </el-table-column>
      <el-table-column
        prop="factoryAddress"
        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>
      <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="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="getSupplierInfo" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px">
      <el-form label-width="100px" label-position="right">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称">
              <el-input v-model="form.supplierName"></el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="简称">-->
<!--              <el-input v-model="form.abbreviation"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="供应商编号">-->
<!--              <el-input v-model="form.supplierCode"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="供应商分类">
              <el-select v-model="form.supplierKind">
                <el-option
                key="1"
                label="辅料供应商"
                :value="0"></el-option>
                <el-option
                  key="2"
                  label="纸张供应商"
                  :value="1"></el-option>
                <el-option
                  key="3"
                  label="辅料与纸张供应商"
                  :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商类别">
              <el-input v-model="form.supplierType"></el-input>
            </el-form-item>
          </el-col>

<!--          <el-col :span="8">-->
<!--            <el-form-item label="供应商拼音">-->
<!--              <el-input v-model="form.shortPinyin"></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="8">
            <el-form-item label="供货内容">
              <el-input v-model="form.supplierContent"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="结算期限">
          <el-input v-model="form.settleDay"></el-input>
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结算方式">
              <el-input v-model="form.settleKind"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人">
              <el-input v-model="form.principal"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="	联系人">
              <el-input v-model="form.linkMan"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话">
              <el-input v-model="form.linkPhone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
        <el-form-item label="地址">
          <el-input v-model="form.factoryAddress"></el-input>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input type="textarea" :rows="2" v-model="form.remark"></el-input>
        </el-form-item>
      </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>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { getSupplierInfoList, addSupplierInfo, getSupplierInfoById, updateSupplierInfo } from '@/api/baseInfo/supplierInfo.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination/index'

const form = {
  supplierCode: '',
  supplierName: '',
  supplierKind: 0,
  supplierType: '',
  supplierContent: '',
  factoryAddress: '',
  settleDay: '',
  settleKind: '',
  remark: '',
  principal: '',
  linkMan: '',
  linkPhone: ''
  // shortPinyin: ''

}
export default {
  components: {
    Cancledialog, Pagination
  },
  data() {
    return {
      webType: true,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: '',
        voidFlag: -1
      },
      form: Object.assign({}, form),
      options: [
        { id: 1, title: '供应商企业' },
        { id: 2, title: '送货员' },
        { id: 3, title: '快递' }
      ],

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

      confirmdialog: false,
      listLoading: false,
      tableData: []
    }
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  mounted() {
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
    this.getSupplierInfo()
  },
  methods: {
    // 获取列表
    getSupplierInfo() {
      const _that = this
      _that.listLoading = true
      var params = _that.search
      if (_that.isChoose || !_that.webType) {
        _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);
      getSupplierInfoList(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
      })
    },
    // 增加供应商档案
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form),
      _that.dialogTitle = '增加供应商档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getSupplierInfo()
    },
    // 增加档案
    addBtn() {
      const _that = this
      var info = JSON.stringify(_that.form)
      var data = {
        'supplierInfo': info
      }
      // //console.log(data);
      // return

      addSupplierInfo(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.dialogTitle = '修改供应商档案'
      _that.dialogVisible = true
      _that.dialogStatus = '2'

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

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

        _that.form = {
          id: data.id,
          supplierCode: data.supplierCode,
          supplierName: data.supplierName,
          supplierKind: data.supplierKind,
          supplierType: data.supplierType,
          supplierContent: data.supplierContent,
          factoryAddress: data.factoryAddress,
          settleDay: data.settleDay,
          settleKind: data.settleKind,
          remark: data.remark,
          principal: data.principal,
          linkMan: data.linkMan,
          linkPhone: data.linkPhone

        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改档案
    editBtn() {
      const _that = this
      var info = JSON.stringify(_that.form)
      var data = {
        'supplierInfo': info
      }
      // console.log(data);

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

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

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getSupplierInfo()
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.supplierName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

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