<!-- 往来公司模块 一览页面 -->
<template>
  <div class="box-body">
    <el-button type="primary" @click="addJump">添加</el-button>
    <el-table
      stripe
      v-loading="loadingList"
      :data="companyList"
      @cell-mouse-enter="enterMouse"
      height="58.5vh"
    >
      <el-table-column prop="no" label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{(page.currentPage - 1)*page.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="compName" label="公司名称" />
      <el-table-column prop="compTypeValue" label="公司类型" />
      <el-table-column prop="contactPerson" label="联系人">
        <template slot-scope="scope">{{ scope.row.contactPerson | empty}}</template>
      </el-table-column>
      <el-table-column prop="contactPhone" label="联系电话">
        <template slot-scope="scope">{{ scope.row.contactPhone | empty}}</template>
      </el-table-column>
      <el-table-column prop="remark" label="备注">
        <template slot-scope="scope">{{ scope.row.remark | empty}}</template>
      </el-table-column>
      <el-table-column prop label="操作" width="100" align="center">
        <yh-operaction :lable="lable" @skip="skipPage" />
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="getList"
    />

    <el-dialog
      v-loading="loadingSave"
      :title="companyTitle"
      :visible.sync="addCompanyFlag"
      width="800px"
      center
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div class="two-form">
        <el-form
          ref="addCompanyInfo"
          :rules="addCompanyInfoRules"
          :model="addCompanyInfo"
        >
          <el-form-item label="公司名称" prop="compName">
            <el-input maxlength="50" v-model="addCompanyInfo.compName" placeholder="请选择" />
          </el-form-item>
          <el-form-item label="公司简称" prop="shortName">
            <el-input maxlength="50" v-model="addCompanyInfo.shortName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="公司英文名称">
            <el-input maxlength="50" v-model="addCompanyInfo.compNameEn" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="公司类型" prop="compTypeKey">
            <el-select v-model="addCompanyInfo.compTypeKey" placeholder="请选择">
              <el-option
                v-for="item in compTypeList"
                :key="item.constKey"
                :label="item.constValue"
                :value="item.constKey"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人">
            <el-input maxlength="50" v-model="addCompanyInfo.contactPerson" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input maxlength="50" v-model="addCompanyInfo.contactPhone" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="法人">
            <el-input maxlength="50" v-model="addCompanyInfo.legalPerson" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="电子邮箱">
            <el-input maxlength="50" v-model="addCompanyInfo.email" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="邮政编码">
            <el-input maxlength="50" v-model="addCompanyInfo.zipCode" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="传真">
            <el-input maxlength="50" v-model="addCompanyInfo.fax" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="公司地址" style="width: 100%;">
            <el-input maxlength="100" v-model="addCompanyInfo.address" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="备注" style="width: 100%;">
            <el-input maxlength="100" v-model="addCompanyInfo.remark" placeholder="请输入" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveAdd('saveAgain')">保存并继续添加</el-button>
        <el-button type="primary" @click="saveAdd('save')">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import { getPageByCondition , saveContactCompany , goDelete } from '../../api/travelCompany';
  import Pagination from '../../components/Pagination';
  import yhOperaction from '../../layout/components/operaction.vue';

  export default {
    name: "travelCompanyIndex",
    components: {
      Pagination,
      yhOperaction
    },
    data() {
      return {
        addCompanyInfo:{},
        addCompanyInfoRules:{
          compName: [{ required: true, trigger: 'blur' }],
          shortName: [{ required: true, trigger: 'blur' }],
          compTypeKey: [{ required: true, trigger: 'change' }],
        },

        page:{
          pageSize:10,
          currentPage:1,
        },
        companyList:[],
        companyTitle:'',
        addCompanyFlag:false,
        loadingSave:false,
        total:0,
        msgRow:'',
      }
    },
    computed:{
      lable() {
        return [
          {
            lab: '编辑',
            click: 'editBtn',
          },
          {
            lab: '删除',
            click: 'deleteBtn',
          }
        ]
      },
      compTypeList() {
        return [
          {
            constValue: '船东及船管',
            constKey: '1'
          },
          {
            constValue: '船东',
            constKey: '2'
          },
          {
            constValue: '船管',
            constKey: '3'
          }
        ]
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 鼠标移入获取当前行信息
      enterMouse(row, column, cell, event){
        this.msgRow = row;
      },
      // 操作下拉
      skipPage(msg) {
        if(msg === 'editBtn'){
          this.getEdit(this.msgRow)
        }else{
          this.getDelete(this.msgRow)
        }
      },
      // 删除
      getDelete(row){
        this.$confirm('确认要删除改条公司信息吗？','提示',{
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            goDelete(
              this.$qs.stringify({
                id: row.id
              })
            ).then(response => {
                this.$message({
                  type: 'success',
                  message: '删除成功',
                  showClose: true
                });
                this.getList();
              }).catch(() => {});
          }).catch(() => {});
      },
      // 编辑
      getEdit(row){
        this.addCompanyInfo = JSON.parse(JSON.stringify(row));
        this.companyTitle = '编辑'
        this.addCompanyFlag = true;
      },
      cancel() {
        this.addCompanyFlag = false;
        this.loadingSave = false;
        this.$refs.addCompanyInfo.resetFields();
        this.addCompanyInfo = {};
      },
      getSelectLabel() {
        this.addCompanyInfo.compTypeValue = this.compTypeList.find(
          item => item.constKey === this.addCompanyInfo.compTypeKey
        ).constValue
      },
      saveAdd(flag){
        this.$refs.addCompanyInfo.validate(valid => {
          if (valid) {
            this.loadingSave = true
            this.getSelectLabel();
            saveContactCompany(this.addCompanyInfo).then(response=>{
              if(flag === 'save'){
                this.cancel();
                this.$message({
                  message: '保存成功',
                  type: 'success',
                  showClose: true
                })
                this.getList();
              }else{
                this.$refs.addCompanyInfo.resetFields();
                this.addCompanyInfo = {};
                this.getList();
                this.loadingSave = false;
              }
            }).catch((err)=>{
            })
          } else {
            this.$message({
              message: '带*号的为必填项！',
              type: 'error',
              showClose: true
            })
            return false
          }
        })
      },
      addJump(){
        this.companyTitle = '新增'
        this.addCompanyFlag = true;
      },
      getList(){
        this.loadingList = true;
        getPageByCondition(this.page).then(response=>{
          this.companyList = response.records;
          this.total = response.total;
          this.loadingList = false;
        })
      }
    }
  }
</script>

<style scoped>

</style>
