<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="派遣公司名称" prop="name">
          <el-input
              v-model="queryParams.name"
              clearable
              style="width: 300px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="默认账号" prop="default_account">
          <el-input
              v-model="queryParams.default_account"
              clearable
              style="width: 300px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编号" prop="base_code">
          <el-input
              v-model="queryParams.base_code"
              clearable
              style="width: 300px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="list">
        <el-table-column label="编号" align="left" prop="base_code" width="200" fixed/>
        <el-table-column label="派遣公司名称" align="left" prop="name"  width="260" fixed />
        <el-table-column label="默认账号" align="left" prop="default_account" width="180" fixed/>
        <el-table-column label="前缀" align="left" prop="prefix" width="100"/>
        <el-table-column label="规模" align="center" prop="scale" />
        <el-table-column label="联系人" align="center" prop="contracts" />
        <el-table-column label="联系电话" align="center" prop="contract_number" width="120"/>
        <el-table-column label="公司地址（省市县区 ）" align="left" prop="district" width="200"/>
        <el-table-column label="详细地址" align="left" prop="address" width="200"/>
        <el-table-column label="是否使用电子合同" align="center" prop="line_contract" width="180">
          <template #default="{row}">
            {{row.line_contract=='1'?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="统一社会信用代码" align="left" prop="id_no" width="180"/>
        <el-table-column label="电子签章应用id" align="left" prop="seal_id" width="180"/>
        <el-table-column label="电子签章应用秘钥" align="left" prop="secret_key" width="280"/>
        <el-table-column label="注册日期" align="left" prop="register_time" width="180"/>
        <el-table-column label="创建时间" align="left" prop="create_time" width="180"/>
        <el-table-column label="更新时间" align="left" prop="update_time" width="180"/>
  
        <el-table-column label="操作" width="280" align="center" class-name="small-padding fixed-width" fixed="right">
          <template #default="{row}">
            <el-button link type="primary"  @click="handleEdit(row)" icon="EditPen">编辑</el-button>
            <el-button v-if="row.line_contract==1" link type="primary"  @click="handleContract(row,'stop')" icon="TurnOff">停用电子合同</el-button>
            <el-button v-else link type="primary"  @click="handleContract(row,'start')" icon="Open">启用电子合同</el-button>
            <el-button link type="primary"  @click="handleSalaryAdvance(row)" icon="Setting">设置分成比例</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.page"
          v-model:limit="queryParams.limit"
          @pagination="getList"
      />

      <!-- 启用电子合同对话框 -->
      <el-dialog title="电子合同" v-model="open" width="500px" append-to-body>
        <el-form ref="postRef" :model="form" :rules="rules" label-width="160px" style="width: 420px;">
          <el-form-item label="统一社会信用代码" prop="id_no">
            <el-input v-model="form.id_no"  onkeyup="value=value.replace(/[\W]/g,'')" maxlength="18" :disabled=disabled />
          </el-form-item>
          <el-form-item label="电子签章应用id" prop="seal_id">
            <el-input v-model="form.seal_id" :disabled=disabled />
          </el-form-item>
          <el-form-item label="电子签章应用秘钥" prop="secret_key">
            <el-input v-model="form.secret_key" :disabled=disabled />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>

    </div>
  </template>
  
<script setup>


import {dispatchL, startContract, stopContract} from "@/api/dispatch";
  
  const { proxy } = getCurrentInstance();
  
  const list = ref([]);
  const loading = ref(true);
  const showSearch = ref(true);
  const total = ref(0);
  const open = ref(false);
  const disabled = ref(false);

  const data = reactive({
    form: {
    },
    queryParams: {
      page: 1,
      limit: 10,
    },
    rules: {
      id_no: [
        { required: true, message: "请输入统一社会信用代码", trigger: "blur" },
        { min: 18,message: "请输入18位的统一社会信用代码", trigger: "blur"}
      ],
      seal_id:[
        { required: true, message: "请输入电子签章应用id", trigger: "blur" },
      ],
      secret_key: [
        { required: true, message: "请输入电子签章应用秘钥", trigger: "blur" },
      ],
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);

  /** 查询列表 */
  function getList() {
    loading.value = true;
    dispatchL(queryParams.value).then(response => {
      list.value = response.data;
      total.value = response.count;
      loading.value = false;
    });
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.page = 1;
    queryParams.value.limit = 10;
    getList();
  }
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  /** 设置预支公司按钮操作 */
  function handleSalaryAdvance(row) {
    proxy.$router.push('dispatch/salary/advance?id='+row.id+'&code='+row.base_code)
  }

  /** 新增按钮操作 */
  function handleAdd() {
    proxy.$router.push('/dispatch/add')
  }

  /** 编辑按钮操作 */
  function handleEdit(row){
    proxy.$router.push('/dispatch/edit?id='+row.id)
  }

  /** 停用/启用电子合同操作 */
  function handleContract(row,action){
    if(action==='stop'){
      proxy.$modal.confirm('您确定要停用电子合同吗？').then(function () {
        return stopContract({'id':row.id,'name':row.name,'base_code':row.base_code})
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("停用电子合同成功");
      }).catch(() => { });
    }else {
      form.value = {
        id: row.id,
        name: row.name,
        base_code: row.base_code,
      };
      open.value = true;
      disabled.value = false;
      if(proxy.$refs["postRef"]!==undefined){
        proxy.$refs["postRef"].resetFields(); // 清除表单已有校验
      }

      if(row.id_no){
        form.value.id_no= row.id_no
        form.value.seal_id= row.seal_id
        form.value.secret_key=  row.secret_key
        disabled.value = true;
      }
    }
  }

  /** 取消按钮 */
  function cancel() {
    open.value = false;
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["postRef"].validate(valid => {
      if (valid) {
        startContract(form.value).then(response => {
          proxy.$modal.msgSuccess(response.code);
          open.value = false;
          getList();
        });
      }
    });
  }

  getList();

  </script>
  