<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable standout="bg-primary text-white" v-model="msg.Name" label="供应商名称"
            @clear="resetSearch" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" clearable standout="bg-primary text-white" v-model="msg.Type" option-value="Id"
            option-label="Name" :options="supplierTypeList" emit-value map-options label="类别" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-column-table sticky-right-column-table" separator="none" :data="data" :columns="columns"
        row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">供应商信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增供应商" @click="EditSupplier(null)" />
          </div>
        </template>
        <template v-slot:body-cell-Name="props">
          <q-td>
            <template v-if="props.row.ContractUrl&&props.row.ContractUrl!=''">
              <a style="color:#2961FE;text-decoration:none;cursor:pointer" :href="props.row.ContractUrl" target="_blank"
                title="点击查看合同">{{props.row.Name}}</a>
            </template>
            <template v-else>
              {{props.row.Name}}
              <a style="color:red;text-decoration:none;">(未上传合同)</a>
            </template>
          </q-td>
        </template>
        <template v-slot:body-cell-LinkAddress="props">
          <q-td>
            <div style="width:300px;white-space:normal; word-break:break-all;">{{props.row.LinkAddress}}</div>
          </q-td>
        </template>
        <template v-slot:body-cell-CommissionType="props">
          <q-td>
            <template v-if="props.row.CommissionType==0">
              {{props.row.CommissionMoney}} %
            </template>
            <template v-if="props.row.CommissionType==1">
              ￥ {{props.row.CommissionMoney}} 元
            </template>
          </q-td>
        </template>
        <template v-slot:body-cell-Id="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditSupplier(props.row)" />
            <q-btn flat size="xs" icon="delete" color="negative" style="font-weight:400;" label="删除"
              @click="removeSupplier(props.row)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="PageCount"
            :input="true" @input="changePage" />
        </template>
      </q-table>
      <supplier-form v-if="isShowSupplier" :save-obj="supplierObj" @close="closeSupplierForm" @success="refreshPage">
      </supplier-form>
    </div>
  </div>
</template>

<script>
  import {
    querySupplierPage,
    deleteSupplier,
    querySupplierType
  } from '../../api/studyabroad/index'
  import supplierForm from '../../components/studyAbroad/supplier-form'

  export default {
    meta: {
      title: "供应商管理"
    },
    components: {
      supplierForm,
    },
    data() {
      return {
        msg: {
          pageIndex: 1,
          pageSize: 10,
          rowsPerPage: 10,
          Name: '', //供应商名称
          Type: 0, //供应商类别
        },
        PageCount: 0, //总页数
        data: [],
        isShowSupplier: false, //是否显示供应商弹窗
        supplierObj: {}, //供应商对象
        loading: true,
        columns: [{
            name: 'Name',
            label: '供应商名称',
            align: 'left',
            field: 'Name'
          },
          {
            name: 'TypeName',
            label: '类别',
            align: 'left',
            field: 'TypeName'
          },
          {
            name: 'LinkMan',
            label: '联系人',
            align: 'left',
            field: 'LinkMan'
          },
          {
            name: 'LinkTel',
            label: '联系电话',
            align: 'left',
            field: 'LinkTel'
          },
          {
            name: 'LinkAddress',
            label: '地址',
            align: 'left',
            field: 'LinkAddress'
          },
          {
            name: 'CommissionType',
            label: '返佣',
            align: 'left',
            field: 'CommissionType'
          },
          {
            name: 'CreateByName',
            label: '创建人',
            align: 'left',
            field: 'CreateByName'
          },
          {
            name: 'CreateTimeStr',
            label: '创建时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'Id',
            label: '操作',
            field: 'Id'
          }
        ],
        supplierTypeList: [], //供应商类型列表
      }
    },
    created() {
      this.getSupplierTypeList();
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getsupplierpage()
    },
    methods: {
      //获取供应商类型
      getSupplierTypeList() {
        querySupplierType().then(res => {
          if (res.Code == 1) {
            let obj = {
              Name: '不限',
              Id: 0
            }
            this.supplierTypeList = res.Data;
            this.supplierTypeList.unshift(obj);
          }
        })
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getsupplierpage();
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.getsupplierpage()
      },
      //刷新页面
      refreshPage() {
        this.getsupplierpage();
      },
      //获取供应商分页列表
      getsupplierpage() {
        this.loading = true;
        querySupplierPage(this.msg).then(res => {
          this.loading = false;
          this.data = res.Data.PageData;
          this.PageCount = res.Data.PageCount
        })
      },
      //新增修改供应商
      EditSupplier(obj) {
        if (obj) {
          this.supplierObj = obj
        } else {
          this.supplierObj = null
        }
        this.isShowSupplier = true;
      },
      //关闭弹窗
      closeSupplierForm() {
        this.isShowSupplier = false
      },
      //删除供应商
      removeSupplier(item) {
        let delMsg = {
          Id: item.Id,
          Status: 1
        };
        var str = '是否确定删除【' + item.Name + '】供应商?';
        this.$q.dialog({
          title: '提示信息',
          message: str,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          deleteSupplier(delMsg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '删除成功！',
                position: 'top'
              })
              this.refreshPage();
            }
          })
        }).onCancel(() => {

        });
      }
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass')

</style>
