<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="供应商编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 " clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商分类" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oms:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oms:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oms:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oms:supplier:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="700" border v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商名称" width="160" align="center" prop="name" />
      <el-table-column label="序号"  width="55" type="index" align="center" />
      <el-table-column label="状态" align="center" prop="status" >
          <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in statusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="供应商编码" width="160" align="center" prop="code" />
      <el-table-column label="供应商分类" width="140" align="center" prop="type" />
      <el-table-column label="供应商等级" width="130" align="center" prop="level" />
      <el-table-column label="溢装比例" width="120" align="center" prop="overfillRate" >
        <template v-slot="scope">
          {{ scope.row.overfillRate }} %
        </template>
      </el-table-column>
      <el-table-column label="送货周期" width="120" align="center" prop="cycleDay" >
        <template v-slot="scope">
          {{ scope.row.overfillRate }} 天
        </template>
      </el-table-column>
      <el-table-column label="默认开票" width="140" align="left" prop="invoiceType" :formatter="invoiceTypeFormat" />
      
      <el-table-column label="默认税率" width="140" align="center" prop="taxRate" >
        <template v-slot="scope">
          {{ scope.row.taxRate }} %
        </template>
      </el-table-column>
      <el-table-column label="默认结算方式" width="140" align="center" prop="paymentType" />
      <el-table-column label="公司全称" width="140" align="center" prop="fullName" />
      <el-table-column label="助记码" width="180" align="center" prop="mnemonic" />
      <el-table-column label="合作日期" align="center" prop="cooperationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cooperationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" width="160" align="center" prop="contactName" />
      <el-table-column label="联系人电话" width="160" align="center" prop="contractTel" />
      <el-table-column label="联系人手机" width="160" align="center" prop="contactPhone" />
      <el-table-column width="105" label="所属省市区" align="center" prop="cityIds" />
      <el-table-column label="详情地址" align="center" prop="address" />
      <el-table-column label="账户名称" width="160" align="center" prop="bankName" />
      <el-table-column label="开户银行" width="160" align="center" prop="bankDeposit" />
      <el-table-column label="银行账户" width="200" align="center" prop="bankAccount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="110" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oms:supplier:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oms:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1080px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商分类" prop="type">
              <el-input v-model="form.type" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商等级" prop="level">
              <el-input v-model="form.level" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司全称" prop="fullName">
              <el-input v-model="form.fullName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="助记码" prop="mnemonic">
              <el-input v-model="form.mnemonic" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="送货周期" prop="cycleDay">
              <el-input v-model="form.cycleDay" placeholder="请输入" >
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="溢装比例" prop="overfillRate">
              <el-input v-model="form.overfillRate" placeholder="请输入" >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>        
        <el-row>
          <el-col :span="12">
            <el-form-item label="默认开票" prop="invoiceType">
              <el-select v-model="form.invoiceType" placeholder="请选择">
                <el-option
                v-for="dict in invoiceTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="默认税率" prop="taxRate">
              <el-input v-model="form.taxRate" placeholder="请输入默认税率" >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="默认结算方式" prop="paymentType">
              <el-input v-model="form.paymentType" placeholder="请输入默认结算方"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合作日期" prop="cooperationTime">
              <el-date-picker clearable size="small" style="width: 200px"
              v-model="form.cooperationTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择合作日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人电话" prop="contractTel">
              <el-input v-model="form.contractTel" placeholder="请输入联系人电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人手机" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系人手机" />
            </el-form-item>
          </el-col>
        </el-row> 
        <el-row>
          <el-col :span="8">
            <el-form-item  label-width="100px" label="所属省市区" prop="cityIds">
              <el-cascader
              ref="citySelect"
              v-model="cityIds"
              :options="citys"
              @change="handleCityChange"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="详情地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详情地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户名称" prop="bankName">
              <el-input v-model="form.bankName" placeholder="请输入账户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户银行" prop="bankDeposit">
              <el-input v-model="form.bankDeposit" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
        </el-row>    
        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
        </el-form-item>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态 ">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier, exportSupplier } from "@/api/oms/supplier";
import {getCitys} from "@/api/store/city";
export default {
  name: "Supplier",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 默认开票 0无 1电子发票 2增值税普通发票 3增值税专用发票字典
      invoiceTypeOptions: [],
      // 状态 字典
      statusOptions: [],
      cityIds:[],
        citys:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        type: null,
        level: null,
        cycleDay: null,
        invoiceType: null,
        overfillRate: null,
        taxRate: null,
        paymentType: null,
        fullName: null,
        mnemonic: null,
        cooperationTime: null,
        contactName: null,
        contractTel: null,
        contactPhone: null,
        cityIds: null,
        address: null,
        bankName: null,
        bankDeposit: null,
        bankAccount: null,
        status: null,
        createUserId: null,
        updateUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("oms_invoice_type").then(response => {
      this.invoiceTypeOptions = response.data;
    });
    this.getDicts("oms_supplier_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getCitys();
  },
  methods: {
    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 默认开票 0无 1电子发票 2增值税普通发票 3增值税专用发票字典翻译
    invoiceTypeFormat(row, column) {
      return this.selectDictLabel(this.invoiceTypeOptions, row.invoiceType);
    },
    // 状态 字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        type: null,
        level: null,
        cycleDay: null,
        invoiceType: null,
        overfillRate: null,
        taxRate: null,
        paymentType: null,
        fullName: null,
        mnemonic: null,
        cooperationTime: null,
        contactName: null,
        contractTel: null,
        contactPhone: null,
        cityIds: null,
        address: null,
        bankName: null,
        bankDeposit: null,
        bankAccount: null,
        remark: null,
        status: null,
        createUserId: null,
        updateUserId: null,
        createTime: null,
        updateTime: null,
        cityIds: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商管理";
      });
      
    },
    handleCityChange(value) {
          console.log(value);
          var nodes=this.$refs.citySelect.getCheckedNodes();
          this.form.cityIds=nodes[0].pathLabels[0]+"-"+nodes[0].pathLabels[1]+"-"+nodes[0].pathLabels[2];
          this.form.cityIdsStr=value.toString();
          console.log(this.form.cityIdsStr);
    },
    getCitys(){
          getCitys().then(res => {
             this.loading = false;
             this.citys=res.data;
          });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSupplier(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除供应商管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplier(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有供应商管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSupplier(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>